Path: news.mathworks.com!newsfeed-00.mathworks.com!oleane.net!oleane!news.ecp.fr!feeder.eternal-september.org!eternal-september.org!not-for-mail
From: dpb <none@non.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: decimal precision
Date: Wed, 04 Nov 2009 09:01:36 -0600
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <hcs5a8$gn8$1@news.eternal-september.org>
References: <hcrtl2$9t3$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.eternal-september.org U2FsdGVkX1+LIRNoMvh1avsLtMWIqlKCYC42k8aIsuPwZoVr1+7Dbx0Gcr5bhgqLQD6sfPoVlUkoUC29JHpD1Wzlgz1BgZRjaCn5AJ/ouBr837bmfuctgA04CikfIs1eLBBY6SNI7pg=
X-Complaints-To: abuse@eternal-september.org
NNTP-Posting-Date: Wed, 4 Nov 2009 15:06:50 +0000 (UTC)
In-Reply-To: <hcrtl2$9t3$1@fred.mathworks.com>
X-Auth-Sender: U2FsdGVkX19A2ReGKwc/RK1MZdECD4iz7iDBx5Uzi00=
Cancel-Lock: sha1:SECZzP8fZpYX+pXI/xC/pkoEB0I=
User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
Xref: news.mathworks.com comp.soft-sys.matlab:582396


sujata wrote:
> Hi 
> 
> I have a question. I have the following calculation
> 
> (1+number1)/(1+number2) - 1
> 
> 
> The number 1 and number 2 are scalars with 16 decimals 
> 
> when I calculate the upper formula in both excel and matlab with the
> same number1 and number2, I get a difference on the last three decimals.
> 
> Why is this so? And what should I do that they are exact the same.

Artifact of floating point representation.

If indeed n1 == n2 precisely, the result should be 1; otoh if n1 and n2 
are obtained in different manner (such as one is typed or read in while 
the other is computed, for example) they may differ in internal 
representation in a last bit or two and so the result isn't identical.

In double precision the number of mantissa bit works out to 15/16 
decimal digits of precision and since internal representation is in 
binary fractions instead of decimal this is an approximate number and 
will differ depending on rounding to nearest representable value.  Hence 
it's probable that the "1's" in the numerator and denominator underflow 
but again depending on whether n1==n2 identically, it's possible that 
for some values of n one does and one doesn't again owing to rounding.

The general answer to the last plaintive plea is "write code that is 
robust to floating point approximations" -- the answer as to how to do 
that in general is the subject of many tomes and is "depends"...

See the ML wiki FAQ for discussion and link to a paper by Goldberg 
that's essentially required reading for computing...

--