Re: Bug in Delphi SimpleRoundTo function?



In article <46b37e47$1@xxxxxxxxxxxxxxxxxxxxxx>,
herb-sci1_AT_sbcglobal.net (John Herbster) wrote:

A good programmer should know that there is no such
thing as "simple rounding"

I disagree.

Ask any mathematician and you'll get the same answer: 0.5 rounds up.
Check it on your calculator too.

Mathematically, this is:
0 <= x < 0.5 => 0 (or [0, 0.5[ => 0)
0.5 <= x < 1.0 => 1 (or [0.5, 1.0[ => 1)

The only area of debate is whether negative values round towards or away
from zero.

Banker's Rounding is a convenience to avoid either party (buyer or seller)
gaining a financial advantage from fractional values.

That aside, when ordinary programmers get caught in
the trap of using floating *binary* point numbers,
to represent (fixed or floating) decimal fraction
numbers and have to do any kind of rounding, then
they stumble all over themselves.

This isn't a trap that can always be avoided. You may be given a database
populated with floating point values that are not 100% accurate. Any
reasonable programmer should be able to work around this without recourse
to scaled integer values or BCD representation, armed only with the
knowledge of the required precision and values that conform to that
precision.

Of course, using scaled integers or BCD does make it easier, but don't
yourself fall into the trap of believing that this is the only solution.

I am really disappointed that I am still reading
such ill informed discussion of this problem

Not ill-informed at all - and rather superior of you to suggest so. My
original post (if you refer back) was noting a failure of SimpleRoundTo()
to conform to its specification. The fact that it is being used to handle
currency is neither a mistake nor necessarily bad practice. Your library
routines may well make life easier for some, and indeed maybe for me too.
However, there are times and circumstances which do make and will
continue to make situations such as mine unavoidable. Read Kurt Godel's
hypothesis for a better understanding of why this is so.

Trevor

.



Relevant Pages

  • Re: sprintf - Rounding down
    ... expected and appears to be rounding down. ... with my formatting below. ... The %f is floating, so it rounds. ...
    (perl.beginners)
  • Re: Why not FP for Money?
    ... I had started with the assumption that the IEEE 754 rounding methods ... accounting has rules about rounding, ... Floating point arithmetic is useful for continuous quantities, ... applies here if you are dealing with actual pennies. ...
    (comp.lang.python)
  • Re: converting float to double
    ... Although I can not see a guarantee from your floating ... rounding, it does not generalise to, e.g., ...
    (comp.lang.c)
  • Re: Bug in Delphi SimpleRoundTo function?
    ... calculations also use bankers rounding. ... rounding rule that most programmers can agree on. ... the trap of using floating *binary* point numbers, ... to represent decimal fraction ...
    (borland.public.delphi.non-technical)
  • Re: converting float to double
    ... > the floating point value. ... The "rounding" required to ... What algorithm should be implemented? ... If accounting rules so specifiy, ...
    (comp.lang.c)