But I get true if I inspect:
1001.0 = 1001
The representation of (1.001 * 1000) is not the same as 1001.0
I think this is a bug... Any suggestion?
Thanks,
Hernan.
This is a common problem with float data in any language. It is a "feature" of
float data that doing some math on it brings out some fuzziness. It has to do
with representing a real number (the fraction part) in binary (base 2 or 8 or
16) as opposed to base 10. Comparing two floats or a float and an integer (or
any other data type) for equality is always risky. You might try to convert the
float to an integer (asInteger) first.
For example:
(1.001 * 1000) asInteger = 1001
answers true. This is because the comparison is forced to be between two
integers. In your example, the integer, 1001 is converted to a float and there
is some noise between that conversion and (1.001 * 1000).
You might also try ScaledDecimal numbers. They give you decimal values in a
smaller range than floats but can almost always be compared safely.
For example:
(1.001s3 * 1000) = 1001
and
(1.001s3 * 1000) = 1001s3
answer true.
Lou
-----------------------------------------------------------
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon
mailto:L...@Keystone-Software.com http://www.Keystone-Software.com
Thanks again.
Hernan.
Solve this problem automatically? Not likely in any programming language.
Just how close is close enough? If the 2 floats are different in the 3rd
decimal position, are they equal? What about the 9th? If you want to
implement a solution, then you could add a closeTo: or approximatelyEqual:
method to Float that would use an epsilon value to determine just how close
the 2 numbers should be. Or, better yet, use ScaledDecimal numbers as Louis
suggested.
John O'Keefe
IBM Smalltalk
"Hernan Wilkinson" <h.wil...@mercapsoftware.com> wrote in message
news:cip7hv$7nce$1...@news.boulder.ibm.com...
Anyway, that was what I meant.
Hernan.