The task is simple: given a 32 bit integer, convert it to its floating point value as defined by the IEEE 754 (32-bit) standard.
To put it another way, interpret the integer as the bit-pattern of an IEEE binary32 single-precision float and output the numeric value it represents.
IEEE 754 single precision
Here is a converter for your reference.
Here is how the format looks, from Wikipedia's excellent article:
The standard is similar to scientific notation.
The sign bit determines whether the output is negative or positive. If the bit is set, the number is negative otherwise it is positive.
The exponent bit determines the exponent (base 2), it's value is offset by 127. Therefore the exponent is \$2^{n-127}\$ where n is the integer representation of the exponent bits.
The mantissa defines a floating point number in the range \$[1,2)\$. The way it represents the number is like binary, the most significant bit is \$\frac 1 2\$, the one to the right is \$\frac 1 4\$, the next one is \$\frac 1 8\$ and so on... A one by default is added to the value, implied by a non-zero exponent.
Now the final number is: $$\text{sign}\cdot 2^{\text{exponent}-127}\cdot \text{mantissa}$$
Test cases
1078523331 -> 3.1400001049041748046875
1076719780 -> 2.71000003814697265625
1036831949 -> 0.100000001490116119384765625
3264511895 -> -74.24919891357421875
1056964608 -> 0.5
3205496832 -> -0.5625
0 -> 0.0
2147483648 -> -0.0 (or 0.0)
For this challenge assume that cases like NaN
and inf
are not going to be the inputs, and subnormals need not be handled (except for 0.0
which works like a subnormal, with the all-zero exponent implying a leading 0 bit for the all-zero mantissa.) You may output 0
for the case where the number represented is -0
.
This is code-golf, so the shortest answer in bytes wins.
3.14000010490417
in the first case \$\endgroup\$+-0.0
. I edited, assuming that those test cases implied this was part of the challenge. If not, the OP should remove the +-0.0 test cases. The all-zero exponent encoding implies a leading 0 bit for the all-zero mantissa, so it's2^(-126) * 0.mantissa
instead of2^(-127) * 1.mantissa
. en.wikipedia.org/wiki/… . Since you don't need to handle other cases of all-zero exponent, you could special-case the whole bit-pattern, e.g.if(! x<<1) return 0;
\$\endgroup\$