Integer conversation in the rounding account 0.1

Asked

Viewed 441 times

3

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

Isn’t that supposed to be eight? 0.1 + 0.7 = 0.8 * 10 = 8

You are returning me 7. because?

  • 3

    People forgive me for being "repeated", but I was doubtful in this equation that I passed to you, the examples cited here as duplicates, are much more complex than mine, I am layman in, php it would be kind of complicated to understand that my problem matches these cited, thanks for all your help

3 answers

3


By default PHP uses numbers from floating point, in this specific case you can achieve the correct value by rounding, using round or use another type as: float, double.

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

It turns out that 0.1 + 0.7 is not exactly 0.8 but 7.99999... And the moment it is converted to int the generated result is 7

2

The floating point math doesn’t work perfectly as we hope, look:

console.log(0.1 + 0.7)

If you run this snippet, the result will be 0.799999....

Converting this number to integer causes rounding down:

console.log(parseInt(0.1 + 0.7))

This other excerpt simply shows 0. Because 0.79999 rounded down is 0.

However, you multiplied by 10 before converting to integer, that rounded 7,99999... down, which is 7.

If you want to know a little more about this strange behavior, you can take a look in that excellent English OS response, or in that another link. (I will search for links in English to post here.)

PS: I know his question is in PHP, but I believe that this behavior is common Javascript too, so serve as an example, correct me if they are different or if I am wrong

  • Downvoter, can comment?

  • 1

    It was probably because it was not in PHP itself. Try to avoid this if the author of the question does not mention some other language at the time. If the author had put something like "I had this problem in PHP and saw that it happens the same in JS" his answer would be valid. Since the focus was only PHP, your answer does not fit the question. I think it valid to do this, since already at the beginning of the answer you make clear that the problem is language independent, which was not the case.

  • Thank you @Andersoncarloswoss

1

I think this question is duplicate of this, which in itself is also duplicated from others, but the fact that the representation is 8, even in float, can cheat.

When you give one:

echo ((0.1 + 0.7) * 10);

You have as a result the 8, but if you do this:

if(((0.1 + 0.7) * 10) === 8e0){
    echo 'Este valor é 8';
}

You will see that this value is a lie, is not the if that is wrong, can try even using == 8, the problem is that the calculation itself does not result in 8.

If you run:

var_export(((0.1 + 0.7) * 10));

You’ll see your true value, 7.999999999999999, which is different from eight.

For that reason to do this:

echo (int)7.999999999999999

Will result in 7, because it is "equivalent" to making a floor(7.999999999999999).

This is explained, including with the same values in the documentation and also this answer.


One solution to the problem is to use Bcmath or GMP.

bcscale(2);

echo bcmul(bcadd('0.1', '0.7'), '10');

The bcadd will add 0.1 with 0.7 and the bcmul will multiply the result of bcadd (0.8) with the 10.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.