<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264959</link>
    <title>MATLAB Central Newsreader - decimal precision</title>
    <description>Feed for thread: decimal precision</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 04 Nov 2009 12:56:02 -0500</pubDate>
      <title>decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264959#692018</link>
      <author>sujata </author>
      <description>Hi &lt;br&gt;
&lt;br&gt;
I have a question. I have the following calculation&lt;br&gt;
&lt;br&gt;
(1+number1)/(1+number2) - 1&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The number 1 and number 2 are scalars with 16 decimals &lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
Why is this so? And what should I do that they are exact the same.</description>
    </item>
    <item>
      <pubDate>Wed, 04 Nov 2009 15:01:36 -0500</pubDate>
      <title>Re: decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264959#692065</link>
      <author>dpb</author>
      <description>sujata wrote:&lt;br&gt;
&amp;gt; Hi &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a question. I have the following calculation&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (1+number1)/(1+number2) - 1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The number 1 and number 2 are scalars with 16 decimals &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; when I calculate the upper formula in both excel and matlab with the&lt;br&gt;
&amp;gt; same number1 and number2, I get a difference on the last three decimals.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why is this so? And what should I do that they are exact the same.&lt;br&gt;
&lt;br&gt;
Artifact of floating point representation.&lt;br&gt;
&lt;br&gt;
If indeed n1 == n2 precisely, the result should be 1; otoh if n1 and n2 &lt;br&gt;
are obtained in different manner (such as one is typed or read in while &lt;br&gt;
the other is computed, for example) they may differ in internal &lt;br&gt;
representation in a last bit or two and so the result isn't identical.&lt;br&gt;
&lt;br&gt;
In double precision the number of mantissa bit works out to 15/16 &lt;br&gt;
decimal digits of precision and since internal representation is in &lt;br&gt;
binary fractions instead of decimal this is an approximate number and &lt;br&gt;
will differ depending on rounding to nearest representable value.  Hence &lt;br&gt;
it's probable that the &quot;1's&quot; in the numerator and denominator underflow &lt;br&gt;
but again depending on whether n1==n2 identically, it's possible that &lt;br&gt;
for some values of n one does and one doesn't again owing to rounding.&lt;br&gt;
&lt;br&gt;
The general answer to the last plaintive plea is &quot;write code that is &lt;br&gt;
robust to floating point approximations&quot; -- the answer as to how to do &lt;br&gt;
that in general is the subject of many tomes and is &quot;depends&quot;...&lt;br&gt;
&lt;br&gt;
See the ML wiki FAQ for discussion and link to a paper by Goldberg &lt;br&gt;
that's essentially required reading for computing...&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Wed, 04 Nov 2009 15:21:01 -0500</pubDate>
      <title>Re: decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264959#692074</link>
      <author>someone </author>
      <description>dpb &amp;lt;none@non.net&amp;gt; wrote in message &amp;lt;hcs5a8$gn8$1@news.eternal-september.org&amp;gt;...&lt;br&gt;
&amp;gt; sujata wrote:&lt;br&gt;
&amp;gt; &amp;gt; Hi &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have a question. I have the following calculation&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; (1+number1)/(1+number2) - 1&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The number 1 and number 2 are scalars with 16 decimals &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; when I calculate the upper formula in both excel and matlab with the&lt;br&gt;
&amp;gt; &amp;gt; same number1 and number2, I get a difference on the last three decimals.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Why is this so? And what should I do that they are exact the same.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Artifact of floating point representation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If indeed n1 == n2 precisely, the result should be 1; otoh if n1 and n2 &lt;br&gt;
&amp;gt; are obtained in different manner (such as one is typed or read in while &lt;br&gt;
&amp;gt; the other is computed, for example) they may differ in internal &lt;br&gt;
&amp;gt; representation in a last bit or two and so the result isn't identical.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In double precision the number of mantissa bit works out to 15/16 &lt;br&gt;
&amp;gt; decimal digits of precision and since internal representation is in &lt;br&gt;
&amp;gt; binary fractions instead of decimal this is an approximate number and &lt;br&gt;
&amp;gt; will differ depending on rounding to nearest representable value.  Hence &lt;br&gt;
&amp;gt; it's probable that the &quot;1's&quot; in the numerator and denominator underflow &lt;br&gt;
&amp;gt; but again depending on whether n1==n2 identically, it's possible that &lt;br&gt;
&amp;gt; for some values of n one does and one doesn't again owing to rounding.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The general answer to the last plaintive plea is &quot;write code that is &lt;br&gt;
&amp;gt; robust to floating point approximations&quot; -- the answer as to how to do &lt;br&gt;
&amp;gt; that in general is the subject of many tomes and is &quot;depends&quot;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; See the ML wiki FAQ for discussion and link to a paper by Goldberg &lt;br&gt;
&amp;gt; that's essentially required reading for computing...&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Those sources are in the answer to Q6.1 of the MATLAB FAQ at:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

