Search code examples
c++floating-pointieee

IA64 doesn't correspond to IEEE standard for float numbers?


I tried this code to represent float number storing system:

float *f = new float();
int *i = new int();
i = reinterpret_cast<int *>(f);
std::bitset<32> *bs;

std::cin>>*f;
bs = new std::bitset<32>(*i);
std::cout<<*bs<<" == "<<*f<<std::endl;

But when it run with 23.5 the result is 01000001-10111100-00000000-00000000. The power is 10000001=63.
Shouldn't it be 64 due to IEEE standard?
And the mantis should be 00010111-10000000-00000000. Is that true?

Note: I compiled this code with GCC and VS both. The result was same.


Solution

  • 01000001-10111100-00000000-00000000

    0 10000011 01111000000000000000000

    sign: 0
    exponent: 10000011_binary = 131_decimal
    mantissa: 01111000000000000000000_binary
    mantissa is defined by 1.<mantissa>
    so -> 1.01111_binary
    
    exponent is defined this way: exp-127
    -> 131-127 = 4
    -> 1.01111_binary * 2^4
    -> comma shift to the right by 4 
    --> 10111.1_binary = 23.5_decimal
    

    generally:

    (-1)^sign * 1.<mantissa> * 2^(exponent-127)