Page 1 of 1

Why?

Posted: Sat Dec 27, 2008 4:50 pm
by jkjasonkellar
Why does this evaluate to 7?

Code: Select all

echo (int) ((0.1 + 0.7) * 10);
I was told it is because (0.1 + 0.7) * 10) is being stored as 7.99999999, and everything after the decimal is cut off by the conversion to int, but why is it being stored as that? Surely ((0.1 + 0.7) * 10) should be being stored as 8 even before the data type conversion?

Re: Why?

Posted: Sat Dec 27, 2008 5:04 pm
by cptnwinky
I don't know why either but typecasting it as float makes it work out right.

Re: Why?

Posted: Sat Dec 27, 2008 5:05 pm
by Mark Baker
jkjasonkellar wrote:Why does this evaluate to 7?

Code: Select all

echo (int) ((0.1 + 0.7) * 10);
I was told it is because (0.1 + 0.7) * 10) is being stored as 7.99999999, and everything after the decimal is cut off by the conversion to int, but why is it being stored as that? Surely ((0.1 + 0.7) * 10) should be being stored as 8 even before the data type conversion?
Nope, the values 0.1 and 0.7 are floating point values, and the result of the sum (0.8) is a floating point value. When you multiply that by 10, despite 10 being an integer, the result is also still a floating point value until it is cast to integer.

Try doing:

Code: Select all

echo round((0.1 + 0.7) * 10);

Re: Why?

Posted: Sat Dec 27, 2008 10:55 pm
by Chris Corbyn
Note that round() also returns a floating point value ;)