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…
= = = = = = = = = =
I am editing the original question to include one more WTF bug…
try this:
modify the original number and truncate it on 10 decimal digits… so…
double wtf = 36.76662445068359375000;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:10];
NSString *valueX = [formatter stringFromNumber:[NSDecimalNumber numberWithDouble:wtf]];
[formatter release];
NSLog(@"%@", valueX);
the answer now is 36.7666244507
now the value is a string with 10 decimal digits… now lets convert it back to double
double myDoubleAgain = [valueX doubleValue];
NSLog(@"%.20f", myDoubleAgain);
the answer is 36.76662445070000018177 ??????
myDoubleAgain has now more digits!!!!
Normally, I’m the one who comes in and explains to people that the number they entered is not representable as a floating-point number, and where the rounding errors are, blah blah blah.
This question is much more fun than what we usually see, and it illustrates exactly what’s wrong with the crowd wisdom of “floating point is inexact, read ‘what every computer scientist should know…‘ lolz”.
36.76662445068359375is not just any 19-digit decimal number. It happens to be a 19 digit decimal number that is also exactly representable in double precision binary floating point. Thus, the initial conversion implicit in:is exact.
wtfcontains exactlyb100100.11000100010000011, and no rounding has occurred.The spec for NSDecimalNumber says that it represents numbers as a 38 digit decimal mantissa and a decimal exponent in the range [-127,128], so the value in
wtfis also exactly representable as an NSDecimalNumber. Thus, we may conclude thatnumberWithDoubleis not delivering a correct conversion. Although I cannot find documentation that claims that this conversion routine is correctly rounded, there is no good reason for it not to be. This is a real bug, please report it.I note that the string formatters on iPhoneOS seem to deliver correctly rounded results, so you can probably work around this by first formatting the double as a string with 38 digit precision and then using
decimalNumberWithString. Not ideal, but it may work for you.