1

Me and my colleges were discussing the following problem, and although we came up with some theories we are still seeing it very odd...

<?php

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

?>

outputs 7

whilst

<?php

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

?>

outputs 8 (as expected and as would output if you do it with a calculator)

Could the (int) be causing the issue here? Anyone have an idea?

Thanks a lot and Good Day!

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36

1 Answers1

1

The cast to integer (and related rounding error) occurs at a different point in the formula

Calculate 0.1 plus 0.7, multiply by 10 and cast to integer; 

with the expected rounding error, should result in 7

against

Calculate 0.1 plus 0.7, cast to integer, and multiply by 10

casting 0.1 + 0.7 (ie 0.8) to integer should give 0, which multiplied by 10 is still 0

Mark Baker
  • 209,507
  • 32
  • 346
  • 385