Hello All:
After the Xcode 10.1 (or perhaps ios 12.1), i noticed one of my tests were failing. Basically NSDecimalDivide is giving a bogus answer when the dividend is large and the divisior isn't. Here's a bit of code that shows the error:
int main(int argc, constchar * argv[])
{
@autoreleasepool
{
NSDecimalNumber *xReg = [[NSDecimalNumber alloc] initWithString:@"200000000000000000000000000000000000000000000000000"]; // 2^50
NSDecimalNumber *yReg = [[NSDecimalNumber alloc] initWithString:@"3.14159265358979323846264338327950288419"];
NSDecimal xNum = [xReg decimalValue];
NSDecimal yNum = [yReg decimalValue];
NSCalculationError calcError = NSDecimalDivide(&result, &xNum, &yNum, NSRoundPlain);
NSLog(@"NSDecimalDivide %@. Calc Error is: %ld", NSDecimalString(&result, nil), (long)calcError);
calcError = NSDecimalDivide(&result, &yNum, &xNum, NSRoundPlain);
NSLog(@"NSDecimalDivide %@. Calc Error is: %ld", NSDecimalString(&result, nil), (long)calcError);
}
return 0;
}
Here's the output:
2018-11-11 12:55:47.113276-0500 NSDecimalDivideTest[37786:1866739] NSDecimalDivide 0. Calc Error is: 0
2018-11-11 12:55:47.113556-0500 NSDecimalDivideTest[37786:1866739] NSDecimalDivide 0.0000000000000000000000000000000000000000000000000157079632679489661923132169163975144209. Calc Error is: 0
Program ended with exit code: 0
The first divide is wrong. Last time I checked, the answer should be: 6.36619^49. The second divide is correct: 1.5707963^-50.
With something simple, setting xReg to 25 and yReg to 5, the results are correct: 5 and 0.2.
Is NSDecimalString having a problem with large values (doesn't seem to have a problem with small)? Has some behaviour changed? Any constructive comments would be appreciated.
Thanks in advance for your time...
John