NSDecimalNumber and division precision
NSDecimalNumber and division precision
- Subject: NSDecimalNumber and division precision
- From: Eric MORAND <email@hidden>
- Date: Fri, 20 Jul 2007 22:50:18 +0200
Hi list,
I have some problems obtaining perfect precision when dividing numbers.
Let us say I want to compute (100 / 1.196). The result should be
83.61204013378.
I use the following code :
[...]
NSDecimalNumber * derivedUnitPriceHT = nil;
NSDecimalNumber * unitPriceTTC = [NSDecimalNumber
decimalNumberWithMantissa:100 exponent:0 isNegative:NO]; // 100
NSDecimalNumber * taxRate = [NSDecimalNumber
decimalNumberWithMantissa:1196 exponent:-1 isNegative:NO]; // 1.196
derivedUnitPriceHT = [unitPriceTTC decimalNumberByDividingBy:
taxRate]; // (100 / 1.196)
NSLog (@"derivedUnitPriceHT = %@", derivedUnitPriceHT);
[...]
Once the calculations are done, derivedUnitPriceHT is
83.612040133779264214046822742474916. But if I do the same
calculation using MacOS X Calculator, it returns the exact value
83.61204013378 !
What am I doing wrong ? I thought that using NSDecimalNumber was the
solution to obtain perfect precision but it looks like I am wrong.
Can someone help ?
Thanks by advance,
Eric.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden