<?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: int function odd behavior in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667927#M200113</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;EM&gt;&amp;gt;And I'm still not convinced that round is a solution for all possible floating point numbers.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;My experience is that rounding always works if done appropriately. Since the error is always very small, rounding by say a billionth gets rid of it.&lt;/P&gt;
&lt;P&gt;To reuse&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;'s code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  X = 0.00005;
  Y = 0.00005 - 1e-16; 
  A=X=Y; 

  RndX = round(X, 1e-9);
  RndY = round(Y, 1e-9);

  B=RndX=RndY; 
  C=RndY=0.00005;

  putlog A= B= C=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;A=0 B=1 C=1&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Though I agree than in principle the decimal number cannot be represented, at least rounding correctly homogenises the binary values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jul 2020 03:47:36 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-07-09T03:47:36Z</dc:date>
    <item>
      <title>int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667625#M199967</link>
      <description>&lt;P&gt;Hi Friends,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ran below code&lt;/P&gt;
&lt;P&gt;data y;&lt;BR /&gt;cnt = 2.55;&lt;BR /&gt;a = cnt-int(cnt);&lt;BR /&gt;d = (a&amp;gt;0.550);&lt;BR /&gt;e = (a=0.550);&lt;BR /&gt;f = (a &amp;lt; 0.550);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;f comes out as 1, which indicates that sas is considering&amp;nbsp; 0.55 as less then 0.55. Any thoughts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 04:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667625#M199967</guid>
      <dc:creator>SP_SAS</dc:creator>
      <dc:date>2020-07-08T04:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667627#M199968</link>
      <description>&lt;P&gt;Here's the documentation of the INT function:&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1b1zd0wuyufp0n1jbqlr27p4eay.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1b1zd0wuyufp0n1jbqlr27p4eay.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is fuzzed, so this is expected behavior. Using INTZ will produce the results you are expecting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data y;
cnt = 2.55;
a = cnt-intz(cnt);
d = (a&amp;gt;0.550);
e = (a=0.550);
f = (a &amp;lt; 0.550);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV id="tinyMceEditorketpt42_0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 08 Jul 2020 04:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667627#M199968</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-08T04:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667628#M199969</link>
      <description>&lt;P&gt;Welcome to the world of storing decimal numbers using binary numbers. 0.55 and 2.55 cannot be exactly represented by binary numbers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data y;
cnt = 2.55;
a = cnt-int(cnt);
diff = (0.55-a);
d = (a&amp;gt;0.550);
e = (a=0.550);
f = (a &amp;lt; 0.550);
run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs     cnt      a        diff       d    e    f

 1     2.55    0.55    2.2204E-16    0    0    1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 05:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667628#M199969</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-08T05:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667639#M199976</link>
      <description>&lt;P&gt;It has nothing to do with the INT() function, but with the way binary math works in calculations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
x1 = 2.55 - 2;
x2 = (x1 = 0.55);
put x1= x2=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;CODE class=" language-sas" style="font-size: 13px; background-color: #f5f5f5; white-space: normal;"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;73         data _null_;
 74         x1 = 2.55 - 2;
 75         x2 = (x1 = 0.55);
 76         put x1= x2=;
 77         run;
 
 x1=0.55 x2=0&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jul 2020 06:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667639#M199976</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-08T06:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667723#M200020</link>
      <description>&lt;P&gt;Here is the best answer for my purpose.. let me know if you guys think anything can go wrong with this solution.&lt;/P&gt;
&lt;P&gt;data y;&lt;BR /&gt;cnt = 2.550;&lt;BR /&gt;intaa = int(cnt);&lt;BR /&gt;intzz = intz(cnt);&lt;BR /&gt;a = cat('0.',scan(cnt,2,'.')) *1;&lt;BR /&gt;d = (a&amp;gt;0.550);&lt;BR /&gt;e = (a=0.550);&lt;BR /&gt;f = (a &amp;lt; 0.550);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 14:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667723#M200020</guid>
      <dc:creator>SP_SAS</dc:creator>
      <dc:date>2020-07-08T14:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667725#M200022</link>
      <description>&lt;P&gt;same problem with intz too.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 14:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667725#M200022</guid>
      <dc:creator>SP_SAS</dc:creator>
      <dc:date>2020-07-08T14:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667728#M200024</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278672"&gt;@ketpt42&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Here's the documentation of the INT function:&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1b1zd0wuyufp0n1jbqlr27p4eay.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1b1zd0wuyufp0n1jbqlr27p4eay.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is fuzzed, so this is expected behavior. Using INTZ will produce the results you are expecting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data y;
cnt = 2.55;
a = cnt-intz(cnt);
d = (a&amp;gt;0.550);
e = (a=0.550);
f = (a &amp;lt; 0.550);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV id="tinyMceEditorketpt42_0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't see how INTZ helps here. Also, on my computer, when I run the above code, I get f=1.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 14:45:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667728#M200024</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-08T14:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667729#M200025</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28953"&gt;@SP_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Here is the best answer for my purpose.. let me know if you guys think anything can go wrong with this solution.&lt;/P&gt;
&lt;P&gt;data y;&lt;BR /&gt;cnt = 2.550;&lt;BR /&gt;intaa = int(cnt);&lt;BR /&gt;intzz = intz(cnt);&lt;BR /&gt;a = cat('0.',scan(cnt,2,'.')) *1;&lt;BR /&gt;d = (a&amp;gt;0.550);&lt;BR /&gt;e = (a=0.550);&lt;BR /&gt;f = (a &amp;lt; 0.550);&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think things can go wrong. Here's code that I think I can trust:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data y;
cnt = 2.550;
a = cnt-int(cnt);

d = fuzz(a-0.550)&amp;gt;0;
e = fuzz(a-0.550)=0;
f = fuzz(a-0.550)&amp;lt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jul 2020 14:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667729#M200025</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-08T14:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667730#M200026</link>
      <description>&lt;P&gt;You are not addressing the actual issue.&amp;nbsp; If you only want to compare to 4 decimal places then round the numbers.&amp;nbsp; Might want to round to 4 decimal digits and then test.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;want = round(have,0.0001);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See this paper: &lt;A href="https://www.pharmasug.org/proceedings/2014/CC/PharmaSUG-2014-CC50.pdf" target="_blank"&gt;https://www.pharmasug.org/proceedings/2014/CC/PharmaSUG-2014-CC50.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 14:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667730#M200026</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-08T14:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667736#M200031</link>
      <description>&lt;P&gt;The MOD() seems to fix this. Not sure if it does for all values or just this specific one.&lt;/P&gt;
&lt;P&gt;The real solution is to ROUND() the value(s) before comparing.&lt;/P&gt;
&lt;PRE&gt;542   data y;
543     cnt = 2.55;
544     a = cnt-int(cnt);
545     b = mod(cnt,1);
546     c = round(a,0.0001);
547     a_test = (a=0.550);
548     b_test = (b=0.550);
549     c_test = (c=0.550);
550     put (_all_) (=/);
551   run;


cnt=2.55
a=0.55
b=0.55
c=0.55
a_test=0
b_test=1
c_test=1&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jul 2020 14:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667736#M200031</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-08T14:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667738#M200033</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The real solution is to ROUND() the value(s) before comparing.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'm not convinced. Numbers rounded as above to 4 digits cannot be represented exactly in binary. So couldn't there be cases where we're back to square one, where the binary representation causes the problem at the start of this thread?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 15:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667738#M200033</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-08T15:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667741#M200036</link>
      <description>&lt;P&gt;I don't think SAS has an almost equal operator.&amp;nbsp; So you need to round, at some point, before comparing.&amp;nbsp; You could include it in the code that does the comparison.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if round(a/b,0.0001) = round(c/d,0.0001) then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The real solution is to ROUND() the value(s) before comparing.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'm not convinced. Numbers rounded as above to 4 digits cannot be represented exactly in binary. So couldn't there be cases where we're back to square one, where the binary representation causes the problem at the start of this thread?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 15:11:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667741#M200036</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-08T15:11:38Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667752#M200043</link>
      <description>&lt;P&gt;I'm late to this discussion, but a guiding principle for numerical computations is "don't perform exact comparisons on floating-point numbers." You can use &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0ifledavu3rv1n10gclzxbyy0ul.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;the COMPFUZZ function in Base SAS&lt;/A&gt; to help compare two values for equality.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For inequalities, the typical fuzzy comparison is to compare a difference with a "small value." "Small" is relative to the magnitude of the numbers. For example: [EDIT: I had signs wrong originally]&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Instead of testing x &amp;lt; y, you can test (x - y) &amp;lt; -epsilon&lt;/LI&gt;
&lt;LI&gt;Instead of x &amp;gt; y, you can test (x - y) &amp;gt; epsilon&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The article &lt;A href="https://blogs.sas.com/content/iml/2012/06/25/programming-tip-avoid-testing-floating-point-values-for-equality.html" target="_self"&gt;"Avoid testing floating-point values for equality"&lt;/A&gt; includes references and some discussion of how to choose epsilon.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 09:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667752#M200043</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-07-10T09:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667753#M200044</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I don't think SAS has an almost equal operator.&amp;nbsp; So you need to round, at some point, before comparing.&amp;nbsp; You could include it in the code that does the comparison.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if round(a/b,0.0001) = round(c/d,0.0001) then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I quibble with your statement that you "need to round". There are other choices. The FUZZ function, as I pointed out earlier, or the COMPFUZZ function (as pointed out by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;) seem to address the issue, and both functions are indeed, in layman's terms, an "almost equal operator".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I'm still not convinced that round is a solution for all possible floating point numbers.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 15:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667753#M200044</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-08T15:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667788#M200053</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I'm still not convinced that round is a solution for all possible floating point numbers.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If both values in the comparison are rounded to the same digit, and they differ by a sufficiently small epsilon, the resulting binary "error" will be identical, so the comparison will work.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 17:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667788#M200053</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-08T17:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667818#M200066</link>
      <description>&lt;P&gt;Kurt, I haven't seen explicit code from you, so perhaps I am misunderstanding, but I think in principle Paige is correct. If the two numbers are on opposite sides of a cutpoint, the rounding will take them in opposite directions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
epsilon = 1e-16;
x = 0.00005;
y = x - epsilon;
RndX = round(x, 0.0001);
RndY = round(y, 0.0001);
put RndX=;
put RndY=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jul 2020 18:50:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667818#M200066</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-07-08T18:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667840#M200078</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28953"&gt;@SP_SAS&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what happens internally in the calculation &lt;FONT face="courier new,courier"&gt;2.55-2&lt;/FONT&gt;:&lt;/P&gt;
&lt;P&gt;In the binary system (mathematically), the decimal number 2.55 looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="3"&gt;10.10&lt;STRONG&gt;0011&lt;/STRONG&gt;0011001100110011001100110011001100110011001100110011001100...&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The pattern "0011" is infinitely repeating.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The internal 64-bit floating-point representations of 2.55, 2.55−2 and 0.55 in SAS under Windows (as documented in&amp;nbsp;&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"&gt;Numerical Accuracy in SAS Software&lt;/A&gt;) can be displayed with the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=leforinforref&amp;amp;docsetTarget=p1b6ugw71lmhnpn1wixijtysezzg.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;BINARY64. format&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="3"&gt;720  data _null_;
721  do x=2.55, 2.55-2, 0.55;
722    put x binary64.;
723  end;
724  run;

010000000000&lt;FONT color="#FF0000"&gt;0&lt;FONT color="#800080"&gt;1&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#3366FF"&gt;00011001100110011001100110011001100110011001100110&lt;/FONT&gt;
001111111110&lt;FONT color="#3366FF"&gt;00011001100110011001100110011001100110011001100110&lt;STRONG&gt;00&lt;/STRONG&gt;&lt;/FONT&gt;
001111111110&lt;FONT color="#3366FF"&gt;00011001100110011001100110011001100110011001100110&lt;STRONG&gt;10
&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;You see the repeating pattern here as well. The binary digits highlighted in blue represent the fractional parts of the numbers. For 2.55 two more bits (red and purple) are needed due to the non-zero integer part: These are the two digits surrounding the point (".") in the mathematical binary representation above. When 2 is subtracted from 2.55, the integer part cancels out, these two bits are removed, the subsequent bits are shifted to the left and leave room for two bits at the end.&lt;/P&gt;
&lt;P&gt;In the result 2.55−2 these two least significant bits are simply filled with zeros (highlighted in bold) because the representation of 2.55 doesn't contain the corresponding information. The best possible representation of 0.55, however, contains non-trivial digits in these places: The exact last two digits &lt;FONT face="courier new,courier"&gt;01&lt;/FONT&gt;, followed by &lt;FONT face="courier new,courier"&gt;10011001&lt;/FONT&gt;... in the mathematical representation, are rounded up to &lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the difference between&amp;nbsp;0.55 and&amp;nbsp;2.55−2 in the internal representation is the "&lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt;" in the penultimate bit. This "&lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt;" stands for the value &lt;FONT face="courier new,courier"&gt;2**-52=2.2204...E-16&lt;/FONT&gt; (see&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s variable &lt;FONT face="courier new,courier"&gt;diff&lt;/FONT&gt;).&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 19:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667840#M200078</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-07-08T19:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667845#M200080</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The MOD() seems to fix this. Not sure if it does for all values or just this specific one.&lt;/P&gt;
&lt;P&gt;The real solution is to ROUND() the value(s) before comparing.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks for pointing this out. It's interesting that the MOD function seems to handle a number of these cases correctly, albeit in general only up to four decimals, e.g.,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;mod(2.12345,1) &lt;STRONG&gt;ne&lt;/STRONG&gt; 0.12345&lt;/FONT&gt;, whereas equality holds for decimals &lt;FONT face="courier new,courier"&gt;.0000&lt;/FONT&gt; through &lt;FONT face="courier new,courier"&gt;.9999&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess this is a result of the "&lt;SPAN&gt;extra computations, called fuzzing, to return an exact zero when the result would otherwise differ from zero because of numerical error" (&lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0ebxby8k7049en12wrfx28vyx4t.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n1icfu0gehbcw9n1jipo9hrg0khu" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;). The remarkable thing is that this involves creating correct, non-trivial binary digits of the result which are not contained in the argument, especially if the integer part is large.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
x=1414.2135;
y=mod(x,1);
z=.2135;
put x= binary64. / (y z)(+13 =binary64. /);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="3"&gt;x=010000001001&lt;FONT color="#FF0000"&gt;0110000110&lt;/FONT&gt;&lt;FONT color="#800080"&gt;&lt;STRONG&gt;001&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT color="#3366FF"&gt;10110101001111110111110011101101100100&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;
             y=001111111100&lt;FONT color="#3366FF"&gt;101101010011111101111100111011011001000&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008000"&gt;1011010000111&lt;/FONT&gt;&lt;/STRONG&gt;
             z=001111111100&lt;FONT color="#3366FF"&gt;101101010011111101111100111011011001000&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008000"&gt;1011010000111&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I've indented the (equal) values of &lt;FONT face="courier new,courier"&gt;y&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;z&lt;/FONT&gt; in order to align the parts of the mantissas which represent the fractional parts of the three numbers: For &lt;FONT face="courier new,courier"&gt;y&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;z&lt;/FONT&gt;, these are the 39 blue and the 13 green bits. The latter are not contained in the internal representation of &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; because 10 bits (red) are occupied by the integer part (1414) and 3 additional bits (purple) are needed for the fractional part (without normalization). Moreover, the least significant bit is rounded up.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I virtually always use the ROUND function (typically with rounding units such as &lt;FONT face="courier new,courier"&gt;1e-9&lt;/FONT&gt;) when comparing numeric values with a small to moderate number of decimals.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 19:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667845#M200080</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-07-08T19:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667927#M200113</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;EM&gt;&amp;gt;And I'm still not convinced that round is a solution for all possible floating point numbers.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;My experience is that rounding always works if done appropriately. Since the error is always very small, rounding by say a billionth gets rid of it.&lt;/P&gt;
&lt;P&gt;To reuse&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;'s code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  X = 0.00005;
  Y = 0.00005 - 1e-16; 
  A=X=Y; 

  RndX = round(X, 1e-9);
  RndY = round(Y, 1e-9);

  B=RndX=RndY; 
  C=RndY=0.00005;

  putlog A= B= C=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;A=0 B=1 C=1&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Though I agree than in principle the decimal number cannot be represented, at least rounding correctly homogenises the binary values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 03:47:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667927#M200113</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-09T03:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: int function odd behavior</title>
      <link>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667952#M200124</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Kurt, I haven't seen explicit code from you, so perhaps I am misunderstanding, but I think in principle Paige is correct. If the two numbers are on opposite sides of a cutpoint, the rounding will take them in opposite directions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
epsilon = 1e-16;
x = 0.00005;
y = x - epsilon;
RndX = round(x, 0.0001);
RndY = round(y, 0.0001);
put RndX=;
put RndY=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are right, that's an edge case where round() can't fix it.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 04:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/int-function-odd-behavior/m-p/667952#M200124</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-09T04:28:19Z</dc:date>
    </item>
  </channel>
</rss>

