# Smallest integer i stored as a float such that i+1=i

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

#include<iostream>

using namespace std;

int main(){

float i=1;
while(1<2)
{
if(i+1==i)
{cout << fixed << i << endl;
break;}
i=i+1;
}

}

• 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. – 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.