php.net |  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
Description:
------------
---
From manual page: http://www.php.net/function.gmp-fact
---
The value returned is not correct when providing decimal input for any PHP version the function exists in

Test script:
---------------
See https://3v4l.org/fhsSZ

Expected result:
----------------
~496.6 For full result, see https://www.google.com/#q=5.8!

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-12 00:05 UTC] requinix@php.net
-Type: Bug +Type: Feature/Change Request
 [2015-11-12 00:05 UTC] requinix@php.net
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] pollita@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: pollita
 [2016-07-27 04:20 UTC] pollita@php.net
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 https://en.wikipedia.org/wiki/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] pollita@php.net
-Status: Assigned +Status: Closed
 [2016-07-27 05:05 UTC] pollita@php.net
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
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC