Why am I losing precision when populating an NSDecimalNumber with a double?

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-03-19T16:57:00Z Indexed on 2010/03/19 17:21 UTC
Read the original article Hit count: 183

Here is a simple code that shows what I think is a bug when dealing with double numbers...

double wtf = 36.76662445068359375000;
id xxx = [NSDecimalNumber numberWithDouble: wtf];
NSString *myBug = [xxx stringValue];
NSLog(@"%.20f", wtf);
NSLog(@"%@", myBug);
NSLog(@"-------\n");

the terminal will show two different numbers

36.76662445068359375000 and

36.76662445068359168

Is this a bug or am I missing something?

if the second number is being rounded, it is a very strange rounding btw...

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about iphone