So I had an assignment which asked me to find the smallest integer $i$ which when represented as a float is such that $i+1=i$

My approach- By making a simple C++ program , we get $i=16777216$ or $i=2^{24}$ But if we want to do that theoretically, I am unable to arrive at this number.

So a float is 32 bit variable and the first bit represents sign of mantissa and the next 23 bits represent the number in Mantissa. The next bit represents sign of the exponent and the following 7 bits represent the value of exponent.

Now consider $2^{23}$. In Binary, it is represented as $1000,0000,0000,0000,0000,0000$ (the comma's are just to make things readable). Now if we add $1$ to it, it becomes $1000,0000,0000,0000,0000,0001$

Now we store these numbers as float in C++. Both the numbers $2^{23}$ and $2^{23}+1$ are stored as $0100,0000,0000,0000,0000,0000,00010111$ (the 1 in the end has to be scrapped in order to fit the number in 24 bits). So both of them are essentially the same for the computer.

But why does the computer give me answer as $2^{24}$?

The code that I used


using namespace std;

int main(){

    float i=1;
            {cout << fixed << i << endl;

  • $\begingroup$ You seems misunderstanding the IEEE 754 single-precision floating-point format. $2^{23}$ is represented as 0x4B000000 and $2^{23}+1$ is represented as 0x4B000001. $\endgroup$ – xskxzr Sep 13 '19 at 18:57

Standard “float” has a 24 bit mantissa (the highest bit doesn’t get stored because it is always 1). Therefore all integers that fit into 24 bits - all integers less than $2^{24}$ - can be represented exactly.

From $2^{24}$ onward only even numbers can be stored. So you take N = $2^{24}$, and carefully check what n+1 is according to the rounding rules.

BTW. It makes life a lot easier if you don't look at the exact bit representation of float, but just take it as "1 bit sign, 8 bit exponent, 23 bit mantissa + 1 implicit leading mantissa bit".


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.