<?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>CodeWarrior for MCU中的主题 Re: C Language Syntax Question- Casting on Constants</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/C-Language-Syntax-Question-Casting-on-Constants/m-p/172223#M5359</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;14.7 is of type 'double'.&lt;/P&gt;&lt;P&gt;If you want to use it as float, then simply write 14.7f.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HCS08 might matter, as it offers an option to treat double as float.&lt;/P&gt;&lt;P&gt;Assuming you are using float with 32bits and double with 64bits, then your example will give following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you do&lt;/P&gt;&lt;P&gt;foo==14.7&lt;/P&gt;&lt;P&gt;then left side is float, right side is double. According to ANSI the comparison is done with&lt;/P&gt;&lt;P&gt;(double)foo==14.7&lt;/P&gt;&lt;P&gt;(64bit comparison&lt;/P&gt;&lt;P&gt;while the one with the cast is performed on float.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can easily see this from the assembly code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;   6: int bar1(void) { return foo==14.7;}bar1:00000000 450000   LDHX   #foo00000003 CD0000   JSR    DLONG00000006 95       TSX    00000007 CD0000   JSR    DCMP_RC0000000A 40       NEGA   0000000B 2D66     BMS    *+104       ;abs = 0x00730000000D 6666     ROR    102,X0000000F 6666     ROR    102,X00000011 66A7     ROR    167,X00000013 082703   BRSET  4,0x27,*+6       ;abs = 0x001900000016 8C       CLRH   00000017 5F       CLRX   00000018 81       RTS    00000019 AE01     LDX    #0x010000001B 8C       CLRH   0000001C 81       RTS        7: int bar2(void) { return foo==(float)14.7;}bar2:0000001D 320002   LDHX   foo00000020 653333   CPHX   #0x333300000023 2608     BNE    *+10       ;abs = 0x002D00000025 320000   LDHX   foo00000028 65416B   CPHX   #0x416B0000002B 2703     BEQ    *+5       ;abs = 0x00300000002D 8C       CLRH   0000002E 5F       CLRX   0000002F 81       RTS    00000030 AE01     LDX    #0x0100000032 8C       CLRH   00000033 81       RTS  &lt;/PRE&gt;&lt;P&gt;In the first case the float variable is converted to a double, then compared.&lt;/P&gt;&lt;P&gt;In the second case the compiler simply can do a binary compare.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a side comment: testing on equality for float/double might something to do only carefully.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the question is not if the the float representation of 14.7 is the same, but if 14.7f is represented the same way as 14.7 (double).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;P&gt;BK&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:20:42 GMT</pubDate>
    <dc:creator>BlackNight</dc:creator>
    <dc:date>2020-10-29T09:20:42Z</dc:date>
    <item>
      <title>C Language Syntax Question- Casting on Constants</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/C-Language-Syntax-Question-Casting-on-Constants/m-p/172222#M5358</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm using Codewarrior for S08 if it matters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why would&amp;nbsp;(or Why Should)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new,courier';"&gt;float foo;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new,courier';"&gt;(if foo==14.7)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new,courier';"&gt;{.....}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;produce different results from&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new,courier';"&gt;float foo;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new,courier';"&gt;if (foo==(float)14.7)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new,courier';"&gt;{...}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if the value stored in foo was identical (as viewed with hexadecimal formatting in the Debugger)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jun 2012 00:31:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/C-Language-Syntax-Question-Casting-on-Constants/m-p/172222#M5358</guid>
      <dc:creator>PG1</dc:creator>
      <dc:date>2012-06-15T00:31:03Z</dc:date>
    </item>
    <item>
      <title>Re: C Language Syntax Question- Casting on Constants</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/C-Language-Syntax-Question-Casting-on-Constants/m-p/172223#M5359</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;14.7 is of type 'double'.&lt;/P&gt;&lt;P&gt;If you want to use it as float, then simply write 14.7f.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HCS08 might matter, as it offers an option to treat double as float.&lt;/P&gt;&lt;P&gt;Assuming you are using float with 32bits and double with 64bits, then your example will give following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you do&lt;/P&gt;&lt;P&gt;foo==14.7&lt;/P&gt;&lt;P&gt;then left side is float, right side is double. According to ANSI the comparison is done with&lt;/P&gt;&lt;P&gt;(double)foo==14.7&lt;/P&gt;&lt;P&gt;(64bit comparison&lt;/P&gt;&lt;P&gt;while the one with the cast is performed on float.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can easily see this from the assembly code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;   6: int bar1(void) { return foo==14.7;}bar1:00000000 450000   LDHX   #foo00000003 CD0000   JSR    DLONG00000006 95       TSX    00000007 CD0000   JSR    DCMP_RC0000000A 40       NEGA   0000000B 2D66     BMS    *+104       ;abs = 0x00730000000D 6666     ROR    102,X0000000F 6666     ROR    102,X00000011 66A7     ROR    167,X00000013 082703   BRSET  4,0x27,*+6       ;abs = 0x001900000016 8C       CLRH   00000017 5F       CLRX   00000018 81       RTS    00000019 AE01     LDX    #0x010000001B 8C       CLRH   0000001C 81       RTS        7: int bar2(void) { return foo==(float)14.7;}bar2:0000001D 320002   LDHX   foo00000020 653333   CPHX   #0x333300000023 2608     BNE    *+10       ;abs = 0x002D00000025 320000   LDHX   foo00000028 65416B   CPHX   #0x416B0000002B 2703     BEQ    *+5       ;abs = 0x00300000002D 8C       CLRH   0000002E 5F       CLRX   0000002F 81       RTS    00000030 AE01     LDX    #0x0100000032 8C       CLRH   00000033 81       RTS  &lt;/PRE&gt;&lt;P&gt;In the first case the float variable is converted to a double, then compared.&lt;/P&gt;&lt;P&gt;In the second case the compiler simply can do a binary compare.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a side comment: testing on equality for float/double might something to do only carefully.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the question is not if the the float representation of 14.7 is the same, but if 14.7f is represented the same way as 14.7 (double).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;P&gt;BK&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:20:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/C-Language-Syntax-Question-Casting-on-Constants/m-p/172223#M5359</guid>
      <dc:creator>BlackNight</dc:creator>
      <dc:date>2020-10-29T09:20:42Z</dc:date>
    </item>
  </channel>
</rss>

