I want to compare the real value in memory between float c and float d.
So I use the code printf("%x %x\n", d, c);
, but the result is wrong.
I only use two unsigned int pointers f, g, and point to the c and d. Printing the value byprintf("%x %x\n", *f, *g);
, the result is right.
My total code is:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
float a = 0.1f, b = 0.2f, c = 0.3f;
float d = a + b;
unsigned int* f = (unsigned int*)&c, * g = (unsigned int*)&d;
printf("%x %x\n", d, c);
printf("%x %x\n", *f, *g);
return 0;
}
The output is:
40000000 3fd33333
3e99999a 3e99999a
The size of float and unsigned int both are 4 bytes, why printf
function doesn't handle the float value as an unsigned int value
I want to know what causes the different output