Hi.
I'm currently studying about binary floating point representation, and I encountered something quite confusing while writing some stuff to code.
consider the following case:
Code:
void show_bytes(byte_pointer start, int len) {
for (int i=0; i<len ; i++)
printf(" %.2x", start[i]);
printf("\n");
}
void show_float(float x) {
show_bytes((byte_pointer) &x, sizeof(float));
}
int main() {
float a=84.75;
show_float(a);
}
(I know I shouldn't be using C functions here, but the issue here isn't the programming language in particular...)
this will print out the following output:
00 80 a9 42
which is what I expected (from a little-endian machine).
but now when I'm trying the other direction:
Code:
int main() {
float a=0x42A98000;
printf("%f\n", a);
}
it'll print the wrong output: 111840608.000000
I even tried to assign the hex digits in different order, i.e:
Code:
int main() {
float a=0x0080a942;
printf("%f\n", a);
}
but that didn't work.
what's wrong here?