<?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 the floating point addition is it a commutative ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/244024#M45439</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Please, I want to get some exemples where the floating point addition is not a commutative ?&lt;/P&gt;
&lt;P&gt;a+b is not equal to b+a&lt;/P&gt;
&lt;P&gt;Thank yo&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 17 Jan 2016 01:20:41 GMT</pubDate>
    <dc:creator>LineMoon</dc:creator>
    <dc:date>2016-01-17T01:20:41Z</dc:date>
    <item>
      <title>the floating point addition is it a commutative ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/244024#M45439</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Please, I want to get some exemples where the floating point addition is not a commutative ?&lt;/P&gt;
&lt;P&gt;a+b is not equal to b+a&lt;/P&gt;
&lt;P&gt;Thank yo&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jan 2016 01:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/244024#M45439</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2016-01-17T01:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: the floating point addition is it a commutative ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/244114#M45455</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/44053"&gt;@LineMoon﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would be surprised to see such a case (with &lt;EM&gt;two&lt;/EM&gt; summands) in SAS 9.4 on Windows. (For &lt;EM&gt;three&lt;/EM&gt; summands see &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/caluculed-in-sas-proc/m-p/243350#M45240" target="_blank"&gt;PG's post&lt;/A&gt;&amp;nbsp;in the thread where you originally brought up this question.)&amp;nbsp;However, in 2012 I encountered strange results with SAS &lt;STRONG&gt;9.2&lt;/STRONG&gt;&amp;nbsp;TS2M3 on Windows Server 2008, which came close to this (different results, in a sense, even &lt;U&gt;without&lt;/U&gt;&amp;nbsp;modifying the expression):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example 1 (log excerpt): Identical values with a non-zero difference&lt;/P&gt;
&lt;PRE&gt;12938  data _null_;
12939  a=0.1; b=0.7; c=a+b; d=a+b; e=d-c;
12940  put c hex16.;
12941  put d hex16.;
12942  put e hex16.;
12943  run;

3FE9999999999999
3FE9999999999999
3C80000000000000&lt;/PRE&gt;
&lt;P&gt;Example 2 (program and output): Identical calculations (d minus c) with slightly different results in the same data step&lt;/P&gt;
&lt;PRE&gt;data test;
a=0.1; b=0.7; c=a+b; d=0.8; e=d-c; f=d-c;
proc print;
run;

Obs     A      B      C      D          E             F

 1     0.1    0.7    0.8    0.8    8.3267E-17    1.1102E-16
&lt;/PRE&gt;
&lt;P&gt;Example 3 (log excerpt): A variable which seems to contain a different value than it has been assigned in the previous statement&lt;/P&gt;
&lt;PRE&gt;12843  data _null_;
12844  a=0.1; b=0.7; c=a+b; d=0.8; e=d-c;
12845  if e ne d-c then put '?!!';
12846  run;

?!!
NOTE: DATA statement used (Total process time):&lt;/PRE&gt;
&lt;P&gt;Example 4 (log excerpt): Two identical pairs of numerical expressions with neither LHS&amp;gt;RHS, nor LHS&amp;lt;RHS, nor LHS=RHS&lt;/P&gt;
&lt;PRE&gt;12851  data _null_;
12852  a=0.1; b=0.7; c=a+b; d=0.8;
12853  if d-c&amp;gt;1e-16 | d-c&amp;lt;1e-16 | d-c=1e-16 then put 'Ok';
12854  run;

NOTE: DATA statement used (Total process time):&lt;/PRE&gt;
&lt;P&gt;(Please note that the above examples were &lt;U&gt;not&lt;/U&gt;&amp;nbsp;written to point out the well-known fact that &lt;FONT face="courier new,courier"&gt;0.1+0.7 ne 0.8&lt;/FONT&gt; in (Windows) SAS, which is due to numeric representation error.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspected the common reason of these phenomena to be that the initial calculation of d-c gave&amp;nbsp;a different result than the second and, in fact, subsequent calculations because the latter were optimized not to repeat the calculation and the former used extended real precision (80 bits; cf. one of the standard references on numeric representation issues:&amp;nbsp;&lt;A href="http://www.albany.edu/~msz03/epi697/papers/ts654.pdf" target="_blank" rel="nofollow"&gt;SAS Technical support document TS-654&lt;/A&gt;, p. 5 -- I've replaced the URL, SAS Institute seem&amp;nbsp;to have removed the document recently from&amp;nbsp;&lt;A href="http://support.sas.com/techsup/technote/ts654.pdf" target="_blank"&gt;http://support.sas.com/techsup/technote/ts654.pdf&lt;/A&gt; and redirected the link to SAS 9.4 documentation).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, with SAS 9.4 on Windows 7 I cannot replicate any of the above results. Therefore, I assume that SAS Institute have fixed that in the meantime.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To be more confident that a+b=b+a in SAS 9.4 on Windows 7, I checked this with 1 billion random pairs (a, b) with a and b having absolute values in the range 2**-20 to 2**20, i.e., roughly from one millionth to one million:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Check a+b=b+a with randomly selected internal representations of floating-point numbers a and b */

data _null_;
call streaminit(3141592);
array x a b;
array c[2] $16;
do j=1 to 1e9; /* 1 billion pairs of random numbers with abs. values between 2**-20 and 2**20 */
  do i=1 to 2;
    e=1003+(int(40*rand('uniform')));     /* exponent -- from 1003 (i.e. -20) to 1042 (i.e. 19) */
    if rand('uniform')&amp;lt;0.5 then e=e+2048; /* negative sign */
    m1=int(16777216*rand('uniform'));     /* mantissa, first 6 hex digits */
    m2=int(268435456*rand('uniform'));    /* mantissa, last 7 hex digits */
    c[i]=cats(put(e,hex3.),put(m1,hex6.),put(m2,hex7.));
    x[i]=input(c[i],hex16.);
  end;
  if a+b ne b+a then do; 
    put '######################################## ALARM! ########################################';
    put _all_;
    put '########################################################################################';
  end;
end;
run; /* Result: No "alarm" occurred with SAS 9.4 on Windows 7. */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The following modification (extension to almost the whole range of numbers representable in Windows SAS) did not yield a surprise either with (only) 100 million samples:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;e=1+(int(2045*rand('uniform')));      /* exponent -- excluding 2046 to avoid overflow in the summation */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Please note that it would have been less likely to find an example with a+b ne b+a in this second setting because many of the pairs (a, b) combined numbers of so different orders of magnitude that, numerically, "a+b=a" or "a+b=b."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 11:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/244114#M45455</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-18T11:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: the floating point addition is it a commutative ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/246497#M46129</link>
      <description>&lt;P&gt;Thank you very much&lt;/P&gt;
&lt;P&gt;That's very kind from you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 22:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-floating-point-addition-is-it-a-commutative/m-p/246497#M46129</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2016-01-27T22:29:14Z</dc:date>
    </item>
  </channel>
</rss>

