Description
In IEEE 754-1985 standard, 32-bit single precision floating point number is stored as follows.
The highest bit is the sign bit, and following 8-bit is the exponent field, and 23-bit for fraction part:
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
The floating number has value v:
v= (-1)^S * 2^(E-127) * F
where S is the binary value of s.
Exponent E is an unsigned integer eeeeeeee (based 2)
Fraction F is fraction which formatted 1.mmmmmmmmmmmmmmmmmmmmmmm (based 2)
For example, consider the following 32-bit single precision floating point number:
1 10000001 01000000000000000000000
The sign bit is '1', thus the floating point number is negative.The exponent field is 129, making the exponent E=129-127=2.
The fraction part is .01(based 2)=.25(based 10), making the fraction F=1.01(based 2)=1.25(based 10). Thus, this bit pattern represents the number -1.25×2^2(based 10)= -5(based 10).
See more at http://en.wikipedia.org/wiki/IEEE_754
Input
Each line contains a string of 32 consecutive character, and only '0' or '1' will appear in the string.
It is guaranteed that no floating number in the input is greater than 10000.00 or smaller than 0.001 by its absolute value.
Output
For each line of the input, output a line containing the floating number to two decimal places.
Sample Input
11000000101000000000000000000000
01000001011000000000000000000000
01000000110001101000011100101011
Sample Output
-5.00
14.00
6.20
Source
champ
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char a[32];
while(cin>>a)
{
int i,j,e=1,t=0;
float n=0,k=1;
for(i=1;i<9;i++)
{
t=t*2+(char(a[i])-48);
}
t=t-127;
for(i=0;i<t;i++)
e=e*2;
for(i=9;i<32;i++)
{
k=k/2;
if(a[i]=='1')
n=n+k;
}
n=1+n;
n=n*e;
if(a[0]=='0')
printf("%.2f/n",n);
if(a[0]=='1')
printf("-%.2f/n",n);
}
return 0;
}