<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: not getting expected answer while subtracting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811678#M320167</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/424734"&gt;@shashank12&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Computers tend to produce tiny rounding errors in calculations involving non-integer numbers (see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener nofollow noreferrer"&gt;Numerical Accuracy in SAS Software&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for the details or&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/forums/searchpage/tab/message?filter=location,authorId&amp;amp;q=%22numeric%20representation%22&amp;amp;noSynonym=false&amp;amp;location=forum-board:programming&amp;amp;author_id=32733&amp;amp;collapse_discussion=true" target="_blank" rel="noopener"&gt;many examples on this forum&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In most cases you can avoid such issues by using the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0tj6cmga7p8qln1ejh6ebevm0c9.htm" target="_blank" rel="noopener"&gt;ROUND function&lt;/A&gt; with an appropriate small rounding unit:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;res=&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;round(&lt;/FONT&gt;&lt;/STRONG&gt;301.73 - 60.35 - 0 - 0 - 241.38&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;, 1e-9)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 May 2022 14:43:40 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2022-05-05T14:43:40Z</dc:date>
    <item>
      <title>not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811677#M320166</link>
      <description>&lt;P&gt;I'm running below code in SAS to subtract few numbers :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data _null_;
res=301.73 - 60.35 - 0 - 0 - 241.38;
put res=;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the expected value of res variable should be 0 but I am getting "&lt;SPAN&gt;2.842171E-14".&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Please suggest ways to resolve this. Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 May 2022 14:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811677#M320166</guid>
      <dc:creator>shashank12</dc:creator>
      <dc:date>2022-05-05T14:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811678#M320167</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/424734"&gt;@shashank12&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Computers tend to produce tiny rounding errors in calculations involving non-integer numbers (see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener nofollow noreferrer"&gt;Numerical Accuracy in SAS Software&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for the details or&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/forums/searchpage/tab/message?filter=location,authorId&amp;amp;q=%22numeric%20representation%22&amp;amp;noSynonym=false&amp;amp;location=forum-board:programming&amp;amp;author_id=32733&amp;amp;collapse_discussion=true" target="_blank" rel="noopener"&gt;many examples on this forum&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In most cases you can avoid such issues by using the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0tj6cmga7p8qln1ejh6ebevm0c9.htm" target="_blank" rel="noopener"&gt;ROUND function&lt;/A&gt; with an appropriate small rounding unit:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;res=&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;round(&lt;/FONT&gt;&lt;/STRONG&gt;301.73 - 60.35 - 0 - 0 - 241.38&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;, 1e-9)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 May 2022 14:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811678#M320167</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-05-05T14:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811680#M320169</link>
      <description>This is caused by the limits of binary mathematics. Decimal fractions cannot be accurately stored with binary digits. Google „SAS numeric precision“ for the technical details.&lt;BR /&gt;&lt;BR /&gt;As a remedy, use the ROUND function to get rid of the artifacts.</description>
      <pubDate>Thu, 05 May 2022 14:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811680#M320169</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-05-05T14:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811701#M320179</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 May 2022 17:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811701#M320179</guid>
      <dc:creator>shashank12</dc:creator>
      <dc:date>2022-05-05T17:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811702#M320180</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;I wanted to ask whether 1e-9 is small enough to perform all kinds of subtractions. As this subtraction is dynamic and I might get different numbers.&lt;/P&gt;</description>
      <pubDate>Thu, 05 May 2022 17:26:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811702#M320180</guid>
      <dc:creator>shashank12</dc:creator>
      <dc:date>2022-05-05T17:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811703#M320181</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 May 2022 17:26:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811703#M320181</guid>
      <dc:creator>shashank12</dc:creator>
      <dc:date>2022-05-05T17:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811714#M320185</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/424734"&gt;@shashank12&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;I wanted to ask whether 1e-9 is small enough to perform all kinds of subtractions. As this subtraction is dynamic and I might get different numbers.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The rounding unit must be chosen small enough not to influence results, but big enough to correct rounding errors in the least significant bits.&amp;nbsp;The value of 1E-9 is suitable for a wide range of practical applications, but -- obviously -- if you subtract &lt;EM&gt;very&lt;/EM&gt; small numbers (like millionths), 1E-9 could be too large. Similarly, if you subtract very large numbers (e.g. in the millions, but with one or two decimal places)&amp;nbsp;1E-9 could be too &lt;EM&gt;small&lt;/EM&gt;. Also, the more numbers (with decimals) are involved in the calculation, the larger the rounding errors can become.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have always only up to two decimal places (like dollars and cents), you can circumvent the issues with numeric representation error by multiplying all numbers by 100 and then perform the subtraction with &lt;EM&gt;integers&lt;/EM&gt;. Note, however, that even multiplying by 100 can be prone to those tiny rounding errors, so proper rounding (here: simply to integers) is important:&lt;/P&gt;
&lt;PRE&gt;40    data _null_;
41    if 0.55*100 ne 55 then put 'Surprised?';
42    if round(0.55*100)=55 then put 'OK!';
43    run;

Surprised?
OK!
NOTE: DATA statement used (Total process time):&lt;/PRE&gt;
&lt;P&gt;Edit: Or, of course, just use 0.01 as the rounding unit in this situation.&lt;/P&gt;</description>
      <pubDate>Thu, 05 May 2022 18:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811714#M320185</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-05-05T18:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: not getting expected answer while subtracting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811715#M320186</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/424734"&gt;@shashank12&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;res=301.73 - 60.35 - 0 - 0 - 241.38;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the expected value of res variable should be 0 but I am getting "&lt;SPAN&gt;2.842171E-14".&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These "&lt;SPAN&gt;2.842171E-14" are really &lt;STRONG&gt;2**-45&lt;/STRONG&gt; (=2.8421709430404007434844970703125E-14).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Here's how SAS obtains this seemingly wrong result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First, it computes the difference&amp;nbsp;301.73 - 60.35.&lt;/SPAN&gt;&lt;SPAN&gt; Sadly, 96% (!) of the numbers with up to two decimal places have infinitely many digits in the binary system, so rounding them to a binary floating-point number with finite precision incurs a rounding error (numeric representation error). Your three non-zero numbers are not among the 4% exceptions. For example, 301.73 written in the binary system,&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;100101101.1011101011100001010001
            11101011100001010001
            1&lt;FONT color="#00FF00"&gt;1&lt;/FONT&gt;101011100001010001...&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;is a periodic fraction with the 20-digit pattern "11101011100001010001" repeated infinitely often. On Windows and Unix platforms the 53rd binary digit -- highlighted above in green -- would be the last that could fit into the internal 64-bit representation. The subsequent digits "101011100001010001..." are &lt;FONT color="#3366FF"&gt;rounded up&lt;/FONT&gt;. So, the resulting binary number in the computer's memory&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;100101101.10&lt;EM&gt;11101011100001010001&lt;/EM&gt;111010111000010100&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;1000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;is already slightly too large (by 1.28 * 2**-46 = 1.8189...E-14), i.e., the computer sees what we (humans) would write in the decimal system as&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;301.7300000000000&lt;FONT color="#FF0000"&gt;1818989403545856475830078125&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;which is where the problems with your calculation start. (Similarly, the 60.35 is stored as the binary equivalent of 60.35000000000000142108547152020037174224853515625.)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; The first part of the calculation using the rounded internal binary representations (which you can see with the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/p1b6ugw71lmhnpn1wixijtysezzg.htm" target="_blank" rel="noopener"&gt;BINARY64. format&lt;/A&gt;) basically looks like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt; 1.001011011011101011100001010001111010111000010100&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;1000&lt;/FONT&gt;&lt;/STRONG&gt;000*2**8
&lt;U&gt;-0.0011110001011001100110011001100110011001100110011001101*2**8&lt;/U&gt;
=0.1111000101100001010001111010111000010100011110101110011*2**8
= 1.111000101100001010001111010111000010100011110101110011*2**7&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Second, SAS subtracts 241.38 (more precisely: 241.3799999999999954525264911353588104248046875) from the above result in the same manner (the subtraction of the two zeros is redundant)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt; 1.111000101100001010001111010111000010100011110101110011*2**7
&lt;U&gt;-1.111000101100001010001111010111000010100011110101110000*2**7&lt;/U&gt;
=0.000000000000000000000000000000000000000000000000000011*2**7&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, the result is rounded to 52 mantissa bits: Note that the result has 54 binary digits after the period, so the "...011" (52nd - 54th digit) at the end are rounded up to "...&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;FONT color="#999999"&gt;00&lt;/FONT&gt;", leaving only &lt;STRONG&gt;1&lt;/STRONG&gt; * 2**-52 * 2**7 = &lt;STRONG&gt;2**-45&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 May 2022 18:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/not-getting-expected-answer-while-subtracting/m-p/811715#M320186</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-05-05T18:18:02Z</dc:date>
    </item>
  </channel>
</rss>

