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;
}
}