|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70896 Invalid result with decimals
Submitted: 2015-11-11 22:56 UTC Modified: 2016-07-27 05:05 UTC
From: xavier at pandawan-technology dot com Assigned: pollita (profile)
Status: Closed Package: GNU MP related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
 [2015-11-11 22:56 UTC] xavier at pandawan-technology dot com
From manual page:
The value returned is not correct when providing decimal input for any PHP version the function exists in

Test script:

Expected result:
~496.6 For full result, see!

Actual result:
120 (correct value for input = 5)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-12 00:05 UTC]
-Type: Bug +Type: Feature/Change Request
 [2015-11-12 00:05 UTC]
So what you're saying is you want gmp to extend supporting factorials of real numbers, even though factorial is traditionally only defined for integers?
 [2015-11-12 00:19 UTC] xavier at pandawan-technology dot com
That would be great yeah
 [2016-07-27 04:20 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: pollita
 [2016-07-27 04:20 UTC]
I disagree.  Factorial calculation *is* only valid for non-negative integers, the reason Google is able to give you an answer is, quite simply, because it's cheating.

The Gamma Function when provided with 1-shifted non-negative integers happens to produce the same results as the Factorial Function, however while the Gamma Function provides results for non-integer input, the Factorial Function does not.

libGMP side-steps this problem by only accepting integer inputs.  PHP has a bug in that it doesn't complain about receiving non-integer inputs, simply truncating them instead.

Moreover, since PHP's implementation of the GMP extension doesn't support floating point numbers, the only way this function could *appear* to work would be to first add support for foating point GMP values (no small task when you factor in the complications of which numeric type should and shouldn't be converted), THEN implement the Gamma Function ourselves.

The correct fix for this, IMO, is to raise a warning on receiving invalid input.
 [2016-07-27 05:05 UTC]
-Status: Assigned +Status: Closed
 [2016-07-27 05:05 UTC]
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at

 For Windows:
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 07:01:30 2024 UTC