A troubled relationship between PHP and large integers

A troubled relationship between PHP and large integers

Created:09 Feb 2020 23:23:46

Suppose, that you need to compute 25! in PHP. It is a fairly large number. PHP will tell you that it is 1.551121004E+25, nice and short. No surprises here. Let's look under the hood, though. Sometimes you need, for example to do something with each digit of the number. So, you expand the whole thing, PHP will happily do it for you. The whole number turns out to be 1551121004333098 6055303168.000000 . Now you are ready to finally get to the business of dealing with the digits. The only trouble is, that many of the trailing digits are not supposed to be there. Really? Well, navigate to wolframAlpha.com and ask them what 25! they think looks like.

The answer will be quiet but clear: 15511210043330985984000000. Which is a completely different number to what PHP spat out. A factorial function in Python will only confirm the lie PHP is trying to make you believe in.

Factorial step by step with PHP

Here is the aforementioned factorial function in PHP. One of the nicest functions I have ever written. Try running it for yourself.


function factorial($n){
  if($n === 1){
    return 1;
  }

  return $n * factorial($n - 1);
}

Play time.


factorial(25)
=> 1.551121004E+25

which is:


sprintf("%f",factorial(25))
=> 1551121004333098 6055303168.000000

The gap in the number is exactly where PHP starts rounding up.

As you go for larger factorials, things do not get any better, the other way round, actually. You cannot trust all this E+NUMBER stuff PHP is sells. In fact, smaller numbers are not correct either:


print_r(sprintf("%f",9999999999999999999));
=> 10000000000000000000.000000

Integer overflow

When PHP returns one of these E+Number numbers, it is telling you that an integer overflow has happened on your system. Also, the number has no longer integer data type, but float. Take a look at the dot towards the end of the number.

A word of warning for the end

Code carefully, perhaps with help of some large numbers library, while doing computations involving large numbers in PHP. In fact it is probably better to switch to Python for correct results. JavaScript will not hesitate to sell you a lie either. According to it, 25! is 1551121004333098 6000000000.

This post was updated on 12 Feb 2020 16:56:38

Tags:  php 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, enjoys sWWW writing computer code in PHP, JavaScript and BASH, and some other things he wrote more on on the About page of this website.

Copyrights

©Copyright, 2020 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "A troubled relationship between PHP and large integers." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/a-troubled-relationship-between-php-and-large-integers

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]


I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!