Re: Pure newbie :(
Re: Pure newbie :(
- Subject: Re: Pure newbie :(
- From: Scott Ellsworth <email@hidden>
- Date: Tue, 11 Nov 2003 14:03:33 -0800
On Nov 11, 2003, at 11:54 AM, David Blanton wrote:
On 11/11/03 12:07 PM, "Tony S. Wu" <email@hidden> wrote:
try using double.
never compare two floats.
Why never compare two floats?
In binary, 0.1 is a repeating decimal, and what is stored in the
computer is a truncated approximation to that actual rational number.
As a result, if you subtract nearly equal numbers, the noise in the
bottom few bits will show up as a remainder, without a warning that
there are only one or two sig figs. Direct comparisons will not always
get what you think as a result.
For example, 0.3-0.1-0.1-0.1 is almost certainly not zero, though it
sure looks like it to use. Even more exciting, the exact remainder
depends on the path you take:
0.3-0.3 != 0.3-0.1-0.1-0.1.
See a numerical analysis text for the details.
The short solution: if two numbers are the same to within a factor of
1e-15 or so, they are the same. If you really want to be accurate,
stay with integers, or with rationals. There are bignum classes out
there in the java space that will handle this, so I suspect there are
ones in ObjC as well.
Scott
email@hidden
Java, Cocoa, WebObjects and Database consulting
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.