4

So here's the code:

NSDecimalNumber *test=[[NSDecimalNumber alloc] initWithInt:65];
number3=[[NSDecimalNumber one] decimalNumberByDividingBy:[[[NSDecimalNumber alloc] initWithInt:10] decimalNumberByRaisingToPower:test.integerValue]];
number3=[number3 decimalNumberByMultiplyingBy:number3];

I expected the program to terminate with an underflow exception, but it didn't. Moreover, taking an NSLog gives number3 as some huge number. If I change "test" to anything below 64, it gives the correct output (that is, 10^(-2*test)). Does anyone know what's going on here? I mean, it's pretty easy to work around, but unless I'm missing something (which is quite possible), it seems like an error in the NSDecimalNumber class.

1 Answers1

2

This looks like a bug.

NSDecimalNumber is documented to support exponents from -128 to +127. Clearly, (10-65)2 has an exponent of -130, which is out of range. However, instead of raising an exception, it's wrapping -130 to +126.

The documented default behavior of NSDecimalNumber is that “The methods assume your need for precision does not exceed 38 significant digits and raise exceptions when they try to divide by 0 or produce a number too big or too small to be represented.”

Since it's not raising an exception, I'd say you found a bug. You can report it at http://bugreport.apple.com/.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848