1

I am trying to print out the floating point values 0x40a00000 and 0xc0200000. But the values that I print out and the correct values according to the IEEE-754 Floating Point Converter (https://www.h-schmidt.net/FloatConverter/IEEE754.html) are completely different:

The values I should get according to the IEEE converter:

0x3F800000 = 5.00
0xBF000000 = -2.50

The values I get when running:

float tmp1 = 0x40a00000;
float tmp2 = 0xc0200000;

printf("tmp1 = %.2f\n", tmp1);
printf("tmp2 = %.2f\n", tmp2);

is

tmp1 = 1084227584.00
tmp2 = 3223322624.00

For some reason my C code isn't formatting the bits of the floating point values according to IEEE standards and is instead formatting them as if it were an int with a decimal point

Coder Motor
  • 123
  • 1
  • 4
  • 2
    When you assign an integer to a floating point variable, the compiler converts the actual integer to the same number but as floating point. So `float tmp3 = 0x10;` is equal to `float tmp3 = 16;` which initializes the variable `tmp3` with the value `16.0f`. To "convert" an integer bitwise representation of a floating point number you need to do [*type punning*](https://en.wikipedia.org/wiki/Type_punning) using either byte buffers, pointers, or unions. – Some programmer dude Aug 08 '19 at 19:14
  • The code you have there is setting them to the integer value represented by those literals. It seems like you're trying to essentially set the value of the memory to the hex value. To do that you can do something like `uint32_t mval = 0x40a00000; float tmp1 = *((float*)(&mval));` - I'm not up on the C specs, this may be ub. – Shawn Aug 08 '19 at 19:16
  • 1
    Good read [assign a hex value to float returns a wrong value](https://stackoverflow.com/questions/24066717/assign-a-hex-value-to-float-returns-a-wrong-value) and [hexadecimal floating constant in C](https://stackoverflow.com/questions/4825824/hexadecimal-floating-constant-in-c) – Achal Aug 08 '19 at 19:20

4 Answers4

5

These are assigning the float representation of the hexadecimal numbers to the floats.

Instead, do this:

int i1 = 0x40a00000;
int i2 = 0xc0200000;
float tmp1, tmp2;
memcpy(&tmp1, &i1, sizeof(int));
memcpy(&tmp2, &i2, sizeof(int));

Print them:

printf("tmp1 = %.2f\n", tmp1);
printf("tmp2 = %.2f\n", tmp2);

Output:

tmp1 = 5.00
tmp2 = -2.50

Full example:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int i1 = 0x40a00000;
    int i2 = 0xc0200000;
    float tmp1, tmp2;
    memcpy(&tmp1, &i1, sizeof(int));
    memcpy(&tmp2, &i2, sizeof(int));
    printf("tmp1 = %.2f\n", tmp1);
    printf("tmp2 = %.2f\n", tmp2);
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
1

These aren't doing what you think they do:

float tmp1 = 0x40a00000;
float tmp2 = 0xc0200000;

You are simply using the hexadecimal representation of the decimal integers that are getting printed; they do not shove these bytes in so they can be interpreted as floats.

It sounds like what you want to do is (somehow) get the bytes you want somewhere, get the address of that, and cast it to be a pointer to a float, which when dereferenced will be interpreted as a float.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Those values are not addresses, those are the actual values I want for the float. But I was hoping that C would have those bits in the proper format for me. :( – Coder Motor Aug 08 '19 at 19:23
  • I didn't say they were addresses. – Scott Hunter Aug 08 '19 at 19:42
  • 1
    Those are the actual values you got for the float - 0x40a00000 is 0x205<<21, exactly representable in e.g. IEEE single precision float, and was printed as 1084227584 in decimal. These literals are numbers, not raw memory storage bits. – Yann Vernier Aug 24 '19 at 08:27
0
union
{
   int i;
   float f;
}k1,k2;

k1.i = 0x40a00000;
k2.i = 0xc0200000;

printf("f1 = %.2f\n", k1.f);
printf("f2 = %.2f\n", k2.f);
-1
int i1 = 0x40a00000;
int i2 = 0xc0200000;

float f1 = *(float*)&i1;
float f2 = *(float*)&i2;

printf("f1 = %.2f\n", f1);
printf("f2 = %.2f\n", f2);