Jump to content

Float to Int and Int to Float... Can you explain how these two functions work?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Can anyone explain this to me? I don't fully understand why these functions work differently than a cast.

The function l2f turns an int into a float with the same bit pattern. The function f2l turns a float into an int with the same bit pattern. This is a different result than you get with a cast: l2f(x) != (float)x .

float l2f(int l)

/* convert long integer to float with same bit pattern */

 {

  union {

    float f;

    int ll; 

  } a;

  a.ll = l;

  return a.f;

}

int f2l(float f)

/* convert float to long integer with same bit pattern */

 {

  union {

    float ff; 

    int l; 

  } a;

  a.ff = f;

  return a.l;

}


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is a fairly simple use of a union, it would be of benefit to read the documentation pertaining to their use:
Union Declarations ©

Heed the note:

MSDN said:

If a union of two types is declared and one value is stored, but the union is accessed with the other type, the results are unreliable. For example, a union of float and int is declared. A float value is stored, but the program later accesses the value as an int. In such a situation, the value would depend on the internal storage of float values. The integer value would not be reliable.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
I understand a union and know what the code does, but it was my impression that a cast would have done the same thing. Does a cast from int to float or the other way around manipulate the bits?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is early for me, I had misunderstood!

Floats and integers tend to be the same size but store their data differently (exponent/mantissa rather than plain base2), compiler casting from float to int will truncate the number to an integer type in a predefined manner, i.e, 3.14 -> 3, but using a union will literally allow access to either type as either type without this truncation as these data types are homogeneous. It can lead to malformed numbers, depending on how the float is stored in memory.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Ok, so if I understand what you are saying correctly when casting from a float to an int some small manipulations go on in the bits to rearrange them into a readable int form? I understand the bit representations of both ints and floats and maybe that is why I am confused... I always assumed that casting simply changed the interpretation of a set bit pattern and did nothing to actually manipulate the bits.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The cast will reinterpret the bytes as a base 2 fixed point integer, truncating the mantissa, significant bytes, exponent which are part of the float type. A simple example can be defined:
    float somefloat = 20.1;
    printf("As float: %f \n", *((float*)&somefloat));
    printf("As integer: %d \n", *((uint32_t*)&somefloat));
    printf("As casted integer: %d", (uint32_t) somefloat);
The top two will do the same as your union code, simply displaying the memory located at somefloat interpreted as a specific type, the result of "as integer" will be a nonsense result in consequence, i.e. 1101057229 which is the same bit pattern as the float. Casting will form the wanted 20.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Ah excellent... thanks for the info.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users