Using Python from Cocoa App via PyObjc - numbers dont match...

Michael Ash mike at mikeash.com
Thu Nov 9 04:17:27 CET 2006


In comp.lang.objective-c sapsi <saptarshi.guha at gmail.com> wrote:
> The first output in the console is 40.4 and the second
> -40.40000152587891.
> 
> If i change the NSLog(s)  to "%f",[n floatValue] (and the second
> likewise) the first is 40.400002 and the second is -40.400002.

I assume you are complaining about the fact that it's not printing exactly 
40.4, and wondering where the error is coming from. (It helps if you 
actually state this yourself, so we don't have to assume it, rather than 
just laying out the circumstances.)

The answer is that floating point numbers are inherently imprecise. Read 
through this essential resource:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

-- 
Michael Ash
Rogue Amoeba Software



More information about the Python-list mailing list