Re: NSDecimalNumber and division precision
Re: NSDecimalNumber and division precision
- Subject: Re: NSDecimalNumber and division precision
- From: Alastair Houghton <email@hidden>
- Date: Sat, 21 Jul 2007 12:16:40 +0100
On 20 Jul 2007, at 21:50, Eric MORAND wrote:
I have some problems obtaining perfect precision when dividing
numbers.
You can't obtain "perfect precision" when dividing numbers on a
computer system (except in special cases, obviously), so this is not
surprising. NSDecimalNumber provides up to 38 decimal digits of
precision.
Let us say I want to compute (100 / 1.196). The result should be
83.61204013378.
According to "bc", the result, to 200 decimal places, is
83.61204013377926421404682274247491638795986622073578595317725752508
36120401337792642140468227424749163879598662207357859531772575250836
1204013377926421404682274247491638795986622073578595317725752508361
If you look closely, you'll see that it is a recurring decimal, which
isn't exactly unexpected.
What am I doing wrong ? I thought that using NSDecimalNumber was
the solution to obtain perfect precision but it looks like I am wrong.
Indeed.
Can someone help ?
Nobody can help, because what you think you want to do simply isn't
possible. You need to accept that you will have to round the result
somehow; either that, or you will need to use *rational* arithmetic---
i.e. maintain, for each value, the numerator and denominator of a
fraction. This avoids doing divisions, which means it *is* possible
to get exact answers (provided your number library supports arbitrary
numbers of decimal places, obviously, which NSDecimalNumber does not).
Usually this is overkill, however, particularly for financial
applications (which is, I think, what you're working on here, looking
at the variable names you're using). It is perfectly normal in
accounting calculations to round results, though you may need to
consult whatever accounting rules you need to work to in order to be
certain what is and is not permissible; also, when doing tax
computations, often the tax law will define the acceptable rounding
practices (which may differ depending on circumstances, even for the
*same* tax).
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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