Re:Floating Point Numbers
Quote
In article <46iunr$...@foible.pix.za>, rs000...@pixie.co.za (Russell Smith) writes:
>I need to accurately round floating point numbers in Delphi, and this
>seemingly simple task appears to be impossible!
>The routine I need will look something like this :
>Function Rounded (NumberToRound : Double; Places : Word) : Double;
>Thus Rounded (10.055, 2) returns 10.06, and more importantly
>Rounded (10.055, 2) - 10.06 returns 0 and NOT 1.021E-16 or some other
>small number.
Your second condition is virtually impossible to guarantee with most
computer implementations of floating point numbers, depending on how you
arrived at 10.055, it may actually be 10.0550000152 or some such. It's
not really a limitation of Delphi per se, you'd encounter the same
thing in other dialects of Pascal, or in C or FORTRAN.
You might be able to do something with multiplying you values, then
truncating and re-dividing, but there's still possibilities for errors
to slip in. The stand-by technique is to provide an "Epsilon" factor
against which to test your results, and if they're smaller (or larger
depending on context), you can treat the result as if it were 0.
If you really NEED that kind of absolute precision, you have to go to
fixed point numbers; hovever, most implementations of Pascal (including
Delphi) don't have such things built in; you have to create them
yourself.
HTH
-* Stephen *-
Stephen Posey
University of New Orleans
Email : S...@uno.edu
WWW : http://www.uno.edu/~slp