This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Invalid answer when casting from float to int

The following code, simple as it is, fails to work. I suspect it's a brain short
on my part, and willingly accept the public humiliation. Perhaps it's an actual
problem that I am just now uncovering. I am new to the toolset, so all bets are off.
The tool is a bit on the dated side. Perhaps there is an update that I'm missing.

TIA for any help.

John

IDE-Version:
µVision V4.72.10.0
Copyright (C) 2013 ARM Ltd and ARM Germany GmbH. All rights reserved.

Tool Version Numbers:
Toolchain: MDK-ARM Standard Cortex-M only Version: 4.72.1.0
Toolchain Path: C:\Keil\ARM\ARMCC\bin\
C Compiler: Armcc.Exe V5.03.0.76
Assembler: Armasm.Exe V5.03.0.76
Linker/Locator: ArmLink.Exe V5.03.0.76
Librarian: ArmAr.Exe V5.03.0.76
Hex Converter: FromElf.Exe V5.03.0.76
CPU DLL: SARMCM3.DLL V4.72.1.0
Dialog DLL: DARMP1.DLL V1.22.0.11
Target DLL: UL2CM3.DLL V1.150.11.0
Dialog DLL: TARMP1.DLL V1.22.0.11

        float           gain;
        float           fTemp;
        uint32_t        lTemp;
        uint16_t        temp;

        //      On entry, gain is 27.893
        fTemp = gain * 1000.0;        //  gain - preserve three decimal places
        //      fTemp now 27893.0
        lTemp = (uint32_t)fTemp;
        //      lTemp now 0x6C5F (27743, while I expect 27893)
        temp = lTemp & 0xFFFF;
        //      temp now 27743
        //      WTH?

Parents Reply Children
  • I think float is enough for your case, double is unnecessary. But I am not so sure.
    The below code runs on Windows:

    #include <stdio.h>
    #include <stdint.h>
    
    int main(void)
    {
        float           gain = 27.893;
        float           fTemp;
        uint32_t        lTemp;
        uint16_t        temp;
    
        fTemp = gain * 1000.0;
        lTemp = (uint32_t)fTemp;
        temp = lTemp & 0xFFFF;
    
        printf("%f, %d, %d", fTemp, lTemp, temp);
    }
    

    And the result is:

    27893.000000, 27893, 27893
    Process returned 26 (0x1A)   execution time : -0.000 s
    Press any key to continue.
    

  • Thanks, all. I used the double when float was clearly broken, and that fixed the problem. That it works on Windows is irrelevant, insofar as I am not targeting a Windows platform. But I agree - it should work.

  • Seems to work fine on 4.71a, 4.73 and 4.74. both with and without FPU on a Cortex-M4. Don't have 4.72 installed.

    I'd recommend including math.h and using the f suffix for float constants, but either way John's code functions.

  •         //      On entry, gain is 27.893
            fTemp = gain * 1000.0;        //  gain - preserve three decimal places
            //      fTemp now 27893.0
            lTemp = (uint32_t)fTemp;
            //      lTemp now 0x6C5F (27743, while I expect 27893)
    

    27893=0x6CF5
    27743=0x6C5F
    F and 5 are swapped.
    Maybe the magic is at (uint32_t)?