How would I go about manually changing a decimal (base 10) number into IEEE 754 single-precision floating-point format? I understand that there is three parts to it, a sign, an exponent, and a mantissa. I just don't completely understand what the last two parts actually represent.
3 Answers
Find the largest power of 2 which is smaller than your number, e.g if you start with x = 10.0 then 23 = 8, so the exponent is 3. The exponent is biased by 127 so this means the exponent will be represented as 127 + 3 = 130. The mantissa is then 10.0/8 = 1.25. The 1 is implicit so we just need to represent 0.25, which is 010 0000 0000 0000 0000 0000 when expressed as a 23 bit unsigned fractional quantity. The sign bit is 0 for positive. So we have:
s | exp [130] | mantissa [(1).25] |
0 | 100 0001 0 | 010 0000 0000 0000 0000 0000 |
0x41200000
You can test the representation with a simple C program, e.g.
#include <stdio.h>
typedef union
{
int i;
float f;
} U;
int main(void)
{
U u;
u.f = 10.0;
printf("%g = %#x\n", u.f, u.i);
return 0;
}
Take a number 172.625.This number is Base10 format.
Convert this format is in base2 format For this, first convert 172 in to binary format
128 64 32 16 8 4 2 1
1 0 1 0 1 1 0 0
172=10101100
Convert 0.625 in to binary format
0.625*2=1.250 1
0.250*2=.50 0
0.50*2=1.0 1
0.625=101
Binary format of 172.625=10101100.101. This is in base2 format 10101100*2
Shifting this binary number
1.0101100*2 **7 Normalized
1.0101100 is mantissa
2 **7 is exponent
add exponent 127 7+127=134
convert 134 in to binary format
134=10000110
The number is positive so sign of the number 0
0 |10000110 |01011001010000000000000
Explanation: The high order of bit is the sign of the number. number is stored in a sign magnitude format. The exponent is stored in 8 bit field format biased by 127 to the exponent The digit to the right of the binary point stored in the low order of 23 bit. NOTE---This format is IEEE 32 bit floating point format
A floating point number is simply scientific notation. Let's say I asked you to express the circumference of the Earth in meters, using scientific notation. You would write:
4.007516×107m
The exponent is just that: the power of ten here. The mantissa is the actual digits of the number. And the sign, of course, is just positive or negative. So in this case the exponent is 7 and the mantissa is 4.007516 .
The only significant difference between IEEE754 and grade-school scientific notation is that floating point numbers are in base 2, so it's not times ten to the power of something, it's times two to the power of something. So where you would write, say, 256 in ordinary human scientific notation as:
2.56×102 (mantissa 2.56 and exponent 2),
in IEEE754, it's
1×28 — the mantissa is 1 and the exponent is 8.