Why does std::cout << 1.00000001;
print 1
to the console?
It Works fine with 1.0001
, Its only after it exceeds a certain precision when it stops printing it properly.
These numbers are hard coded, so this question is not a duplicate. In that question, an irrational number was produced because two floating point numbers were added, which means the numbers could not be stored properly, but in my case the numbers are hard coded, so what is the reason for not being able to print (or store maybe) a float explicitly initialised as 1.00000001
?.
Is it just cout
failing to print the value or is it really an int
?
Consider the following program
int main()
{
float num = 1.0000001f;
float num2 = num * 2;
std::cout << num;
std::cout << num2;
}
num2
prints 2
but the complier does not give a warning about conversion from float
to int
in line 2
, so I think that num
must be a float
. Does that mean it's just a problem with cout
? Am I safe to assume that internally num
is actually 1.0000001f
even though it prints 1
to the console?
My complier is visual studio 2019's inbuilt one.