Re: Bug in Delphi SimpleRoundTo function?
- From: smot@xxxxxxxxxxxxxxxxxxx (Trevor Toms)
- Date: Mon, 6 Aug 2007 16:49 +0100 (BST)
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
.
- Follow-Ups:
- Re: Bug in Delphi SimpleRoundTo function?
- From: Anders Isaksson
- Re: Bug in Delphi SimpleRoundTo function?
- From: John Herbster
- Re: Bug in Delphi SimpleRoundTo function?
- References:
- Re: Bug in Delphi SimpleRoundTo function?
- From: John Herbster
- Re: Bug in Delphi SimpleRoundTo function?
- Prev by Date: Re: Rich Client Applications and Codegear's Strategy
- Next by Date: Re: Get your voting shoes on
- Previous by thread: Re: Bug in Delphi SimpleRoundTo function?
- Next by thread: Re: Bug in Delphi SimpleRoundTo function?
- Index(es):
Relevant Pages
|