This is what I am getting and I don't get why this output is coming. when I change float to int it is giving proper output.
int delta=99999997/(float)1;
cout<<delta<<endl;
Output: 100000000
This is what I am getting and I don't get why this output is coming. when I change float to int it is giving proper output.
int delta=99999997/(float)1;
cout<<delta<<endl;
Output: 100000000
When you divide an integer by a floating point number, the integer is converted to a floating-point number.
The reason for the weird result is that 99999997 cannot be represented exactly in a 32 bit floating point number with the precision used by C++’s float
on your architecture.
Instead, when converted to a float, this number gets rounded to the next number which is representable. And that number … happens to be 100000000.