<?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: Applying int function to a division  results in unexpected value. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582696#M165796</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285979"&gt;@KoryeoVitamin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the cause of this phenomena...?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using the format binary64. shows what happens:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   format result expected binary64.;
   expected=32640;
   a=1632; 
   b=5;
   result=(a/b)*100;

   put expected=;
   put '  ' result=;
run;

/*
expected=0100000011011111111000000000000000000000000000000000000000000000
  result=0100000011011111110111111111111111111111111111111111111111111111
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So even without using int-function, the result does not match the expected value. Numeric precision is the culprit, see &lt;A href="https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695157.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695157.htm&lt;/A&gt; for a lengthy explanation.&lt;/P&gt;
&lt;P&gt;One solution is to use round instead of int.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Aug 2019 08:17:17 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-08-21T08:17:17Z</dc:date>
    <item>
      <title>Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582678#M165783</link>
      <description>&lt;P&gt;I have two values, A and B, which are 1632 and 5 each. And i wanna get the value of A divided by B by the unit of percent.&lt;/P&gt;&lt;P&gt;So the result should be 1632 / 5 * 100 = 32640&amp;nbsp; (multiplying 100 to express the value in % unit)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have just used the INT function like this,&lt;/P&gt;&lt;P&gt;&amp;nbsp;RATE = INT( A / B * 100);&lt;/P&gt;&lt;P&gt;however, the results is 32639, not 32640.&lt;/P&gt;&lt;P&gt;So i tried like this,&lt;/P&gt;&lt;P&gt;Rate = A / B * 100;&lt;/P&gt;&lt;P&gt;then it turns out that the result is exactly what i want, 32640.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the cause of this phenomena...?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 06:36:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582678#M165783</guid>
      <dc:creator>KoryeoVitamin</dc:creator>
      <dc:date>2019-08-21T06:36:51Z</dc:date>
    </item>
    <item>
      <title>Re: Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582684#M165787</link>
      <description>&lt;P&gt;Works fine to me?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    a=1632; b=5;
    rate=(a/b)*100;
    put rate=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2019 06:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582684#M165787</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-21T06:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582686#M165789</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; : the problem is that&lt;/P&gt;
&lt;PRE&gt;int(a / b * 100) ^= a / b * 100&lt;/PRE&gt;
&lt;P&gt;But it should be the same. It seems as if the function int applied to the result of a / b before the multiplication takes place.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 07:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582686#M165789</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-08-21T07:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582696#M165796</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285979"&gt;@KoryeoVitamin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the cause of this phenomena...?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using the format binary64. shows what happens:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   format result expected binary64.;
   expected=32640;
   a=1632; 
   b=5;
   result=(a/b)*100;

   put expected=;
   put '  ' result=;
run;

/*
expected=0100000011011111111000000000000000000000000000000000000000000000
  result=0100000011011111110111111111111111111111111111111111111111111111
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So even without using int-function, the result does not match the expected value. Numeric precision is the culprit, see &lt;A href="https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695157.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695157.htm&lt;/A&gt; for a lengthy explanation.&lt;/P&gt;
&lt;P&gt;One solution is to use round instead of int.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 08:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582696#M165796</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-08-21T08:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582698#M165797</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285979"&gt;@KoryeoVitamin&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;When you divide 1632 by 5 first, you're producing a fractional value stored as real binary with certain approximation; then when you multiply by 100, the result is also approximate between 32639 and 32640. When you "look" at the value through the mask of the BESTw. format you're using implicitly, it rounds the internally stored value up to 32640. BESTw. is not a WYSIWYG; it's WYSI&lt;STRONG&gt;&lt;EM&gt;N&lt;/EM&gt;&lt;/STRONG&gt;WIG. To see the real value, you need the RB8. (displaying the value exactly using the radix 256), HEX16. (radix 16), or BINARY64. (radix 2) format. Since RB8. includes unprintable characters, it's not too informative, so let's see what happens with radix 16:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                        
  retain a 1632 b 5 x 32639 y 32640 ;
  r = a / b * 100 ;                  
  i = int  (r) ;                    
  c = ceil (r) ;                    
  put (r i c x y ) (=hex16./) ;      
run ;                                
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;r=40DFDFFFFFFFFFFF
i=40DFDFC000000000
c=40DFE00000000000
x=40DFDFC000000000
y=40DFE00000000000
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you see, I=X and C=Y. The INT (same as FLOOR in this case) and CEIL functions act upon the real value of the computation R and round it down and up to the nearest integer accordingly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to avoid enigmas of this sort, deal with integers, i.e. multiply first to get a value divisible by the denominator exactly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                        
  retain a 1632 b 5 x 32639 y 32640 ;
  r = a * 100 / b ;                  
  i = int  (r) ;                     
  c = ceil (r) ;                     
  put (r i c x y ) (=hex16./) ;      
run;                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case, you get:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;r=40DFE00000000000
i=40DFE00000000000
c=40DFE00000000000
x=40DFDFC000000000
y=40DFE00000000000
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is, all the values except of X (which corresponds to 32639) are exactly 32640.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 08:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582698#M165797</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-21T08:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582705#M165799</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; : the problem is that&lt;/P&gt;
&lt;PRE&gt;int(a / b * 100) ^= a / b * 100&lt;/PRE&gt;
&lt;P&gt;But it should be the same. It seems as if the function int applied to the result of a / b before the multiplication takes place.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Absolutely not. It's "only" the classic "binary representation of decimal fractions" problem, confounded by the multiplication by 100.&lt;/P&gt;
&lt;P&gt;Just run this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
x1 = 1632/5;
x2 = x1 * 100;
x3 = 32640 - x2;
put x3 best32.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you will see that x2 is just slightly smaller than 32640, but the difference is big enough to exceed the fuzz factor of the int() function (which is 1E-12).&lt;/P&gt;
&lt;P&gt;It's the old bottom line: whenever you have fractions, apply the round() function in an appropriate place before continuing.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 09:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582705#M165799</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-08-21T09:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Applying int function to a division  results in unexpected value.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582708#M165802</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; : the problem is that&lt;/P&gt;
&lt;PRE&gt;int(a / b * 100) ^= a / b * 100&lt;/PRE&gt;
&lt;P&gt;But it should be the same. It seems as if the function int applied to the result of a / b before the multiplication takes place.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Absolutely not. It's "only" the classic "binary representation of decimal fractions" problem, confounded by the multiplication by 100.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I just wanted to say that the problem was not that the division without applying int-function &lt;EM&gt;didn't work&lt;/EM&gt;, but that the result differed from the result expected by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285979"&gt;@KoryeoVitamin&lt;/a&gt; - at least that was my interpretation of the initial post.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 09:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-int-function-to-a-division-results-in-unexpected-value/m-p/582708#M165802</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-08-21T09:41:14Z</dc:date>
    </item>
  </channel>
</rss>

