<?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 A strange behaviour in casting... in LabWindows/CVI</title>
    <link>https://ni.lithium.com/t5/LabWindows-CVI/A-strange-behaviour-in-casting/m-p/4956#M366</link>
    <description>Look at this sample code:&lt;BR /&gt;&lt;BR /&gt;int        b, d;&lt;BR /&gt;double    c, coef;&lt;BR /&gt;&lt;BR /&gt;coef = 0.6;&lt;BR /&gt;c = 100.0 * coef;&lt;BR /&gt;b = (int) c;&lt;BR /&gt;d = (int)(100.0 * coef);&lt;BR /&gt;printf ("DOUBLE:\t\t%0.f\n", 100.0 * coef);        // Print the integer&lt;BR /&gt;portion of double&lt;BR /&gt;printf ("INT:\t\t%d\n", b);            // Print double value casted as&lt;BR /&gt;integer&lt;BR /&gt;printf ("INT (cast):\t\t%d\n", d);    // The same but casting the result of&lt;BR /&gt;a calculus&lt;BR /&gt;&lt;BR /&gt;And look at the output generated:&lt;BR /&gt;&lt;BR /&gt;DOUBLE:    60&lt;BR /&gt;INT:    60&lt;BR /&gt;INT (cast):    59&lt;BR /&gt;&lt;BR /&gt;Can anyone explain me that strange behaviour in converting double values?&lt;BR /&gt;TIA&lt;BR /&gt;Roberto &lt;BR /&gt;</description>
    <pubDate>Thu, 12 Oct 2000 14:32:05 GMT</pubDate>
    <dc:creator>Roberto</dc:creator>
    <dc:date>2000-10-12T14:32:05Z</dc:date>
    <item>
      <title>A strange behaviour in casting...</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/A-strange-behaviour-in-casting/m-p/4956#M366</link>
      <description>Look at this sample code:&lt;BR /&gt;&lt;BR /&gt;int        b, d;&lt;BR /&gt;double    c, coef;&lt;BR /&gt;&lt;BR /&gt;coef = 0.6;&lt;BR /&gt;c = 100.0 * coef;&lt;BR /&gt;b = (int) c;&lt;BR /&gt;d = (int)(100.0 * coef);&lt;BR /&gt;printf ("DOUBLE:\t\t%0.f\n", 100.0 * coef);        // Print the integer&lt;BR /&gt;portion of double&lt;BR /&gt;printf ("INT:\t\t%d\n", b);            // Print double value casted as&lt;BR /&gt;integer&lt;BR /&gt;printf ("INT (cast):\t\t%d\n", d);    // The same but casting the result of&lt;BR /&gt;a calculus&lt;BR /&gt;&lt;BR /&gt;And look at the output generated:&lt;BR /&gt;&lt;BR /&gt;DOUBLE:    60&lt;BR /&gt;INT:    60&lt;BR /&gt;INT (cast):    59&lt;BR /&gt;&lt;BR /&gt;Can anyone explain me that strange behaviour in converting double values?&lt;BR /&gt;TIA&lt;BR /&gt;Roberto &lt;BR /&gt;</description>
      <pubDate>Thu, 12 Oct 2000 14:32:05 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/A-strange-behaviour-in-casting/m-p/4956#M366</guid>
      <dc:creator>Roberto</dc:creator>
      <dc:date>2000-10-12T14:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: A strange behaviour in casting...</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/A-strange-behaviour-in-casting/m-p/4957#M367</link>
      <description>I think you are seeing the effects of floating point math, where the processor&lt;BR /&gt;may actually represent 60.0 as 59.9999....  I tried your code in CVI, and&lt;BR /&gt;got the same results.  It looks like what you expect to be 60.0 might actually&lt;BR /&gt;59.9999... on the right side of an '=', or before it gets formatted in a&lt;BR /&gt;printf() statement.  The (int) cast should truncate anything to the right&lt;BR /&gt;of the decimal point, so it seems that is why the expression (int)(100.0&lt;BR /&gt;* coef) may actually be (int)(59.9999...) which gets truncated to 59 before&lt;BR /&gt;being assigned to d.  If I try&lt;BR /&gt;&lt;BR /&gt;printf("%10.4e\n", 60.0-(100.0 * coef));&lt;BR /&gt;&lt;BR /&gt;I get 2.2204e-15, which means to me that the value of (100.0 * coef) is actually&lt;BR /&gt;very slightly less than 60.  Hope this helps.&lt;BR /&gt;&lt;BR /&gt;Dave&lt;BR /&gt;&lt;BR /&gt;"Roberto" &lt;WALLABEE&gt; wrote:&lt;BR /&gt;&amp;gt;Look at &lt;BR /&gt;this sample code:&amp;gt;&amp;gt;int        b, d;&amp;gt;double    c, coef;&amp;gt;&amp;gt;coef = 0.6;&amp;gt;c&lt;BR /&gt;= 100.0 * coef;&amp;gt;b = (int) c;&amp;gt;d = (int)(100.0 * coef);&amp;gt;printf ("DOUBLE:\t\t%0.f\n",&lt;BR /&gt;100.0 * coef);        // Print the integer&amp;gt;portion of double&amp;gt;printf ("INT:\t\t%d\n",&lt;BR /&gt;b);            // Print double value casted as&amp;gt;integer&amp;gt;printf ("INT (cast):\t\t%d\n",&lt;BR /&gt;d);    // The same but casting the result of&amp;gt;a calculus&amp;gt;&amp;gt;And look at the&lt;BR /&gt;output generated:&amp;gt;&amp;gt;DOUBLE:    60&amp;gt;INT:    60&amp;gt;INT (cast):    59&amp;gt;&amp;gt;Can anyone&lt;BR /&gt;explain me that strange behaviour in converting double values?&amp;gt;TIA&amp;gt;Roberto&amp;gt;&amp;gt; &lt;BR /&gt;&lt;/WALLABEE&gt;</description>
      <pubDate>Thu, 19 Oct 2000 12:43:48 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/A-strange-behaviour-in-casting/m-p/4957#M367</guid>
      <dc:creator>Dave Auyer</dc:creator>
      <dc:date>2000-10-19T12:43:48Z</dc:date>
    </item>
  </channel>
</rss>

