array_product(array()) returns 0 instead of 1
| Bug #48484 | array_product(array()) returns 0 instead of 1 | ||||
|---|---|---|---|---|---|
| Submitted: | 2009-06-06 11:22 UTC | Modified: | 2010-12-12 20:27 UTC | ||
| From: | zhalassy at loginet dot hu | Assigned: | iliaa (profile) | ||
| Status: | Closed | Package: | Math related | ||
| PHP Version: | 5.2.9 | OS: | * | ||
| Private report: | No | CVE-ID: | None | ||
[2009-06-06 11:22 UTC] zhalassy at loginet dot hu
Description:
------------
array_product(array()) returns 0 instead of 1. Empty product is 1 (hence "1" is neutral in multiplication) in mathematics.
Example:
array_product(array(1,2)) * array_product(array(3,4)) === /* should be */ array_product(array()) * array_product(array(1,2,3,4));
example implementation:
function array_product($array){
$product = 1;
foreach($array as $element) $product *= $element;
return $product;
}
Reproduce code:
---------------
echo product_array(array());
Expected result:
----------------
It should return 1.
Actual result:
--------------
It returns 0.
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2009-06-06 11:34 UTC] zhalassy at loginet dot hu
[2009-06-09 02:21 UTC] scottmac@php.net
[2009-10-13 05:33 UTC] chrisstocktonaz at gmail dot com
Patch is of course trivial, posting it only to have no unassigned math bugs! Index: ext/standard/array.c =================================================================== --- ext/standard/array.c (revision 289602) +++ ext/standard/array.c (working copy) @@ -4020,7 +4020,7 @@ } if (!zend_hash_num_elements(Z_ARRVAL_P(input))) { - RETURN_LONG(0); + RETURN_LONG(1); } ZVAL_LONG(return_value, 1);[2010-12-12 20:27 UTC] iliaa@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: iliaa
[2010-12-12 20:27 UTC] iliaa@php.net