4

I've got a strange error. See the code

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

and it`s output

18446744073709551616
18446744073709551616

Why $val2 has value 18446744073709551616 when it must be 18446744073709551615?

dex07
  • 137
  • 5

1 Answers1

1

It's because $val1 became float and floating precision isn't that high. Look at Float precision Just look at example from manual floor((0.1+0.7)*10) should be rather 8 but on my PC it also return 7 (as in manual)

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

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291