Thread: int to float conversion

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    int to float conversion

    Hi!

    I'd like to convert 32int into float value.
    Direct cast does not work in a way I expected.
    ex:
    in hex BFFFF2E5
    singned int -1073745179

    When I cast
    Code:
    float myFloat;
    int myInt;
    
    myFloat = (float)myInt ;
    I got -1.073745e+09

    Value I expect is:
    float :-1.999600e+00 (from GHex or other hex editor)

    How to get this value in C code?

    Daniel

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Conversion of int to float does not generally do a bitwise copy. It converts the value put in. -1073754179 is approximately equal to -1.073...e+9.

    A simple way to get the result you expect (assuming int and float are the same size - not always true)
    Code:
    memcpy(&myFloat, &myInt, sizeof(int));
    There are alternatives involving pointer abuse, but those things are dangerous in hands of children.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are telling the compiler to take the number you have (large integer) and make it into a float, not to interpret the number AS float. To do that, you need to tell the compiler to read the number from that address in a different form, so this:

    Code:
    myFloat = *(float *)&myInt ;
    That means, if we take it apart, starting from the right:
    &myInt - the location in memory that holds your integer.
    (float *) - really, I want the compiler use this as a pointer to float, not whatever the compiler thinks it may be.
    * - read from the address of whatever is to the right.
    myFloat = - set this variable to whatever is to the right.

    So, you are telling the compiler: In the location of (myInt), there is a floating point number, now put that float into myFloat.

    As grumpy says: This is a "sharp weapon", and it CAN do harmful things if you use it wrong.

    --
    Mats
    Last edited by matsp; 09-25-2008 at 05:33 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    1
    Create a union containing a 32-bit integer and a float.
    The int and float are now just different ways of looking at the same bit of memory;
    Code:
    union {
        int    myInt;
        float myFloat;
    } my_union;
    
    my_union.myInt = 0x BFFFF2E5;
    
    printf("float is %f\n", my_union.myFloat);
    // float is -1.999600

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Byte order machine differences may rear its ugly head.

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Would also turn out troublesome if sizeof(int) differed from sizeof(float).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    Byte order machine differences may rear its ugly head.
    Byte order should be the same for int as for float in all modern machines. I think PDP-11 was storing floating point in "middle endian", so the low bits were in the middle of the 32-bit word. But that is a rather unusual setup, and I'm not aware of any processor architecture designed in the last 20 years that does so (someone will of course immediately pop out of the woodwork to prove that I'm not aware of ALL processors designed in the last 20 years, but that is life).

    The size problem that onionknight points out is a different story. If sizeof(int) != sizeof(float) then you have a problem with ALL of the suggested techniques, because they all assume that we load a float from the memory held by an integer value, and if they are not the same size, obviously bad things will happen.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM