“Sean O’Dell” sean@celsoft.com writes:
raise “false” if ((625.91 + 900.00 + 22.00) != 1547.91)
How old is this bug? When is it
getting fixed?
It’s about 40 years old, and unlikely to be fixed. Floating point
numbers are not represented exactly inside computers, and so floating
point comparisons are routinely deprecated in books on programming.
Certain values cannot ever be expressed in floating point
representation.
If you want exact, fractional, math, you should probably use the
‘rational’ library and investigate ‘mathn’.
Funny though, if I set a variable to 1547.91 exactly, it is
represented just fine. A value during the arithmetic must be a value
which can’t be represented.
Even the value 1547.91 might not be stored exactly as that value in its
internal floating point representation. Doing the arithmentic isn’t the
only way that “exact” values to become inexact. Even the mere storing
the number internally in a floating point value might cause it it lose
accuracy. Doing arithmetic simply causes these errors to accumulate,
and therefore be more likely to occur in a result.
For example, this number looks exact, but actually, it’s an infinitely
repeating fraction in binary: 0.1
This is true for many “exact” values.
Therefore, you will lose accuracy simply by referencing many decimal
numbers in floating point.
I had to massage my floating point math in C/C++ a lot many years ago,
but I always assumed it was a flaw in the MS compiler, and once I got
in the habit of doing it, I forgot about it.
You have a basic misunderstanding of floating point math if you consider
its rounding and truncation characteristics to be a compiler bug.
As other people have mentioned, there is a BigDecimal class in Ruby
that behaves closer to the way that you want.
Also, consider this: if you always want the computer to give you exact
results, what should these exact results be for each of the following
arithmetic evaluations? …
a = 1/3
b = Math.atan2(1.0, 1.0)
c = Math.sqrt(5)
How many decimal points should they go to. Where should they round?
···
On Wednesday 12 May 2004 10:18, Dave Thomas wrote:
On May 12, 2004, at 12:07, Sean O’Dell wrote:
On Wednesday 12 May 2004 09:53, ts wrote:
Thanks for the info. I’m far less bitter now about having to round the
numbers for comparison. =)
Sean O’Dell
–
Lloyd Zusman
ljz@asfast.com
God bless you.