Xiangyang Ren

Hi, I have a problem when I tried to convert the data typ...

Discussion created by Xiangyang Ren on Jun 17, 2008
Latest reply on Jun 18, 2008 by Xiangyang Ren
Hi, I have a problem when I tried to convert the data type. the processor is MCF5235CVM150 and the compiler is diab 5.2 from windriver (vxworks5.5.1). I have written a very simple function to test the type conversion. But the results were not as expected.

void typwad()
{
    float fv = 2147470000.0;/*2147483646.0;*/
    int iv;
   
    printf("sizeof(float) = %d, fv = %f\n",sizeof(fv),fv);

    iv = (int)fv;
    printf("int iv = %d\n", iv);

    return;
}

and the results are:
sizeof(float) = 4, fv = 2147469952.000000
int iv = 2147469952

when I changed the fv value to 2147483646.0, the results are:
sizeof(float) = 4, fv = 2147483648.000000
int iv = -2147483648

I don't understand whether this is a precision problem or something else caused by compiler.  I really appreciate it if somebody can give me a hint.

I paste also the dissembled code here just for convenience.
void typwad()
{
    0x209330       _typwad:                LINK  .W    A6,#0xfff8
    0x209334       +0x004:                 MOVE  .L    D7,-(A7)
    float fv = 2147470000.0;/*2147483646.0;*/
    0x209336       +0x006:                 MOVE  .L    #0x4effff95,D0
    0x20933c       +0x00c:                 MOVE  .L    D0,(0xfffc,A6)
    int iv;
   
    printf("sizeof(float) = %d, fv = %f\n",sizeof(fv),fv);
    0x209340       +0x010:                 MOVE  .L    (0xfffc,A6),D0
    0x209344       +0x014:                 JSR         fltodb
    0x20934a       +0x01a:                 MOVE  .L    D1,-(A7)
    0x20934c       +0x01c:                 MOVE  .L    D0,-(A7)
    0x20934e       +0x01e:                 MOVEQ       #0x4,D0
    0x209350       +0x020:                 MOVE  .L    D0,-(A7)
    0x209352       +0x022:                 PEA         _str_cmp + 0xca
    0x209358       +0x028:                 JSR         _printf
    0x20935e       +0x02e:                 LEA         (0x10,A7),A7

    iv = (int)fv;
    0x209362       +0x032:                 MOVE  .L    (0xfffc,A6),D0
    0x209366       +0x036:                 JSR         fltol
    0x20936c       +0x03c:                 MOVE  .L    D0,D7
    printf("int iv = %d\n", iv);
    0x20936e       +0x03e:                 MOVE  .L    D7,-(A7)
    0x209370       +0x040:                 PEA         _str_cmp + 0xe7
    0x209376       +0x046:                 JSR         _printf
    0x20937c       +0x04c:                 ADDQ  .L    #0x8,A7

    return;
}
    0x20937e       +0x04e:                 MOVE  .L    (0xfff4,A6),D7
    0x209382       +0x052:                 UNLK        A6
    0x209384       +0x054:                 RTS        
    0x209386       +0x056:                 NOP        



Outcomes