Re: more float rounding info
Re: more float rounding info
- Subject: Re: more float rounding info
- From: Thomas Lachand-Robert <email@hidden>
- Date: Sun, 30 Nov 2003 12:17:01 +0100
Le 30 nov. 03, ` 07:39, Jay Rimalrick a icrit :
thanks to everybody who has helped with this problem. I now
understand why
the error occurs but is there a simple way to get around this rounding
error?
Basically you can't. These things may seem strange because we are used
to decimal notation, so it seems to us that truncating a decimal number
like 0.615 multiplied by 100 should give a consistent result. This is
plain false for computers because they count in base 2. Compare with
the case of fractional numbers like 1/3: you learned in school that 1/3
= 0.3333 but that's not true, because really there is an infinite
numbers of digits.
This is the same for 0.615 in base 2: there is an infinite number of
digits, so there is no way to have an exact representation with float
or double.
Now the solution depends on your needs:
If you just want to display these numbers, that's easy with printf.
If you need more precise floating-point computations, use double.
If you need exact computations for decimal numbers, use specialized
libraries, like NSDecimalNumber for Cocoa.
If you need exact computations for any sort of rational numbers, you
need something like the GMP library: I'm currently in the process of
putting a Cocoa wrapper around it for my project, so tell me if you are
interested.
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
_______________________________________________
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.