C Language Syntax Question- Casting on Constants

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C Language Syntax Question- Casting on Constants

Jump to solution
544 Views
PG1
Contributor I

I'm using Codewarrior for S08 if it matters.

 

Why would (or Why Should)

 

float foo;

(if foo==14.7)

{.....}

 

produce different results from

 

float foo;

if (foo==(float)14.7)

{...}

 

 

if the value stored in foo was identical (as viewed with hexadecimal formatting in the Debugger)

Labels (1)
Tags (1)
0 Kudos
1 Solution
326 Views
BlackNight
NXP Employee
NXP Employee

Hello,

14.7 is of type 'double'.

If you want to use it as float, then simply write 14.7f.

 

HCS08 might matter, as it offers an option to treat double as float.

Assuming you are using float with 32bits and double with 64bits, then your example will give following code.

 

if you do

foo==14.7

then left side is float, right side is double. According to ANSI the comparison is done with

(double)foo==14.7

(64bit comparison

while the one with the cast is performed on float.

 

You can easily see this from the assembly code:

 

   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  

In the first case the float variable is converted to a double, then compared.

In the second case the compiler simply can do a binary compare.

 

As a side comment: testing on equality for float/double might something to do only carefully.

 

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).

 

Hope this helps,

BK

View solution in original post

0 Kudos
1 Reply
327 Views
BlackNight
NXP Employee
NXP Employee

Hello,

14.7 is of type 'double'.

If you want to use it as float, then simply write 14.7f.

 

HCS08 might matter, as it offers an option to treat double as float.

Assuming you are using float with 32bits and double with 64bits, then your example will give following code.

 

if you do

foo==14.7

then left side is float, right side is double. According to ANSI the comparison is done with

(double)foo==14.7

(64bit comparison

while the one with the cast is performed on float.

 

You can easily see this from the assembly code:

 

   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  

In the first case the float variable is converted to a double, then compared.

In the second case the compiler simply can do a binary compare.

 

As a side comment: testing on equality for float/double might something to do only carefully.

 

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).

 

Hope this helps,

BK

0 Kudos