Bug #63136 [Opn->Nab]: pow() returns NaN where base is negative and exponent is an odd number root
| From: | nikic@php.net | Date: | Sat, 22 Sep 2012 07:21:23 +0000 |
| Subject: | Bug #63136 [Opn->Nab]: pow() returns NaN where base is negative and exponent is an odd number root | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-173626@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63136&edit=1
ID: 63136
Updated by: nikic@php.net
Reported by: tstobbe at sacbee dot com
Summary: pow() returns NaN where base is negative and
exponent is an odd number root
-Status: Open
+Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux (fc17, 32-bit)
PHP Version: 5.4.7
Block user comment: N
Private report: N
New Comment:
Real-exponent powers are not defined for negative numbers, see http://en.wikipedia.org/wiki/Exponentiation#Real_exponents_with_negative_bases.
You can see this very easily if you remember that a^b is defined as exp(b*ln(a)) and the logarithm
is only defined in the range ]0,inf[.
Previous Comments:
------------------------------------------------------------------------
[2012-09-22 02:47:21] tstobbe at sacbee dot com
It should be noted that all subsequent fractional pow() issues are related to this. For example:
pow(-[any number], 1/5) vs pow(-[any number], 2/5). Likewise I suspect that most other exponent
based functions (such as log with an arbitrary base) are probably suspect, but this hasn't been
verified.
------------------------------------------------------------------------
[2012-09-21 22:38:15] tstobbe at sacbee dot com
Description:
------------
Odd roots (3, 5, etc) of negative numbers passed to pow() result in NaN rather
than the actual result. While this makes sense for even numbered roots (sqrt and
what-not), it wrecks havoc on any system that actually uses arbitrary roots of
negative numbers.
Example: -2*-2*-2 = -8; pow(-8, 1/3) should return -2
Test script:
---------------
var_dump(pow(-8, 1/3));
Expected result:
----------------
float(-2)
Actual result:
--------------
float(NAN)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63136&edit=1