#48484 [Com]: array_product(array()) returns 0 instead of 1
| From: | chrisstocktonaz at gmail dot com | Date: | Tue, 13 Oct 2009 05:33:40 +0000 |
| Subject: | #48484 [Com]: array_product(array()) returns 0 instead of 1 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-143513@lists.php.net to get a copy of this message | ||
ID: 48484
Comment by: chrisstocktonaz at gmail dot com
Reported By: zhalassy at loginet dot hu
Status: Verified
Bug Type: Math related
Operating System: *
PHP Version: 5.2.9
New Comment:
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);
Previous Comments:
------------------------------------------------------------------------
[2009-06-09 02:21:49] scottmac@php.net
The current behavior was set in bug #35014, pretty sure we should
change it as you suggest.
Confirming for now.
------------------------------------------------------------------------
[2009-06-06 11:34:20] zhalassy at loginet dot hu
http://en.wikipedia.org/wiki/Identity_element
An identity with respect to addition is called an additive identity
(often denoted as 0) and an identity with respect to multiplication is
called a multiplicative identity (often denoted as 1). The distinction
is used most often for sets that support both binary operations, such as
rings. The multiplicative identity is often called the unit [...]
http://en.wikipedia.org/wiki/Unital
In mathematics, an algebra is unital (some authors say unitary) if it
contains a multiplicative identity element (or unit), i.e. an element 1
with the property 1x = x1 = x for all elements x of the algebra.
http://en.wikipedia.org/wiki/Empty_product
------------------------------------------------------------------------
[2009-06-06 11:22:13] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48484&edit=1