Serious error in PHP language [duplicate]

2

I discovered a BUG in the php language and I'm looking for a logical explanation for this, I have the following code:

<?php
    echo (int) ((0.1 + 0.7) * 10);
?>

And the result shown is: 7. Why this? would it be a php bug?

Take the test: link

    
asked by anonymous 25.10.2017 / 14:02

2 answers

3

On your question, the expression ((0.1 + 0.7) * 10) should evaluate to 8.

However, the output of the expression in the script is evaluated at 7 because the PHP engine stores the value of the expression internally as 7.999999 instead of 7.

When the fractional value is converted to an integer, the PHP engine simply truncates the fractional part.

When the value is converted to int, PHP simply truncates the fractional part, resulting in a rather significant error (12.5%, to be exact).

    
25.10.2017 / 14:04
1

It's not a PHP BUG logically, but for you it's casting (int) so floating-point values are rounded to the more or less significant.

    
25.10.2017 / 14:12