• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSDecimalNumber and division precision
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: NSDecimalNumber and division precision
      • From: Alastair Houghton <email@hidden>
    • Re: NSDecimalNumber and division precision
      • From: "Marcus S. Zarra" <email@hidden>
  • Prev by Date: Re: Newbie Question: Why would my App freeze when I select a row inan NSTableView? - Found word(s) Re: in the subject
  • Next by Date: Re: Newbie Question: Why would my App freeze when I select a row inan NSTableView? - Found word(s) Re: in the subject
  • Previous by thread: Re: NSArrayController content array vs arrangedObject
  • Next by thread: Re: NSDecimalNumber and division precision
  • Index(es):
    • Date
    • Thread