Jump to content

Why?


Mark W

Recommended Posts

Why does this evaluate to 7?

 

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?

Link to comment
https://forums.phpfreaks.com/topic/138404-why/
Share on other sites

some kind of bug?

 

echo (int) ((0.1 + 0.7) * 10); // output: 7

echo (int) ((.1 + .7) * 10); // output: 7

echo (int) ((1.1 + 0.7) * 10); // output: 18

echo (int) ((0.1 + 1.7) * 10); // output: 18

echo (int) ((1.1 + .7) * 10); // output: 18

echo (int) ((.1 + 1.7) * 10); // output: 18

echo (int) (10 * (0.1 + 0.7));  // output: 7

echo (int) 10 * (0.1 + 0.7); // output: 8

 

seems to be casting the first number as int and then operating.

Link to comment
https://forums.phpfreaks.com/topic/138404-why/#findComment-723668
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.