Search code examples
objective-ciosnsdecimalnumber

NSDecimalNumber: summing two NaNs results in overflow?


For some odd reason, I receive an NSDecimalNumberOverflowException from the following:

NSDecimalNumber *a = [NSDecimalNumber notANumber];
NSDecimalNumber *b = [NSDecimalNumber notANumber];
NSLog(@"%@",[a decimalNumberByAdding:b]);

This seems unexpected to me since the documentation for NSDecimalNumber's notANumber states that any arithmetic operation receiving a NaN as an argument returns a NaN...

I receive the same error for 1+NaN and NaN+1...thankfully 1+1 still gives 2.

Any ideas on what's going on?


Solution

  • Per posters request:

    Could it be that it is creating a signaling NaN vs. a quiet NaN? These concepts exist in IEEE-754, not sure about NSDecimalNumber. What happens if you do [x decimalNumberByAdding: x] where x is the NSDecimalNumber created byte 1.0/0?