Reputation: 10153
In the following C program the usage of hexadecimal floating-point constants is demonstrate
double d;
d = 2;
printf("Ex 1: 2 in hex: %a\n\n",d);
d = 256;
printf("Ex 2: 2^8 in hex: %a\n\n",d);
d = 0.015625; //= 2^-6
printf("Ex 3: 2^-6 in hex: %a\n\n",d);
d = 0.857421875;
printf("Ex 4: 0.857421875 in hex: %a\n\n",d);
the results are:
Ex 1: 2 in hex: 0x1p+1
Ex 2: 2^8 in hex: 0x1p+8
Ex 3: 2^-6 in hex: 0x1p-6
Ex 4: 0.857421875 in hex: 0x1.b7p-1
I don`t understand how one get the result for ex 4?
Upvotes: 4
Views: 1628
Reputation: 62048
The mantissa in the floating point number is typically represented with a number bigger or equal than 1 but smaller than 2. It's exactly equal to 1 in all examples but the last one.
So, what do you do with the last one? Rerepresent 0.857421875 as 0.857421875 * 2/2, that is, as 1.71484375*2-1. Now, you want the mantissa in hex. Double has 53 bits in the mantissa (including the implied 1.), so 1.71484375 in those 52(53) bits is effectively represented as an integer equal to 1.71484375*252 = 7722969673498624 = 0x1B700000000000. So, there you have it, 0x1.b7p-1.
Upvotes: 7