PHP: error with bigint number

I have a strange mistake. See Code

    $val1 = pow(2, 64);
    $val2 = $val1 - 1;
    echo number_format($val1, 0, '', '') . "<br/>";
    echo number_format($val2, 0, '', '') . "<br/>";

and his way out

18446744073709551616
18446744073709551616

Why does $ val2 have a value of 18446744073709551616 when should it be 18446744073709551615?

+4
source share
1 answer

This is because $ val1 has become floating, and floating precision is not so high. Look at the accuracy of the float. Just look at the example from the manual, it floor((0.1+0.7)*10)should probably be 8, but on my PC it also returns 7 (as in the manual)

If you want to use such large numbers, you should probably use dedicated PHP or BCMath libraries

+1
source

Source: https://habr.com/ru/post/1547300/


All Articles