php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63136 pow() returns NaN where base is negative and exponent is an odd number root
Submitted: 2012-09-21 22:38 UTC Modified: 2012-09-22 07:21 UTC
From: tstobbe at sacbee dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.4.7 OS: Linux (fc17, 32-bit)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tstobbe at sacbee dot com
New email:
PHP Version: OS:

 

 [2012-09-21 22:38 UTC] 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)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-22 02:47 UTC] 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-22 07:21 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-09-22 07:21 UTC] nikic@php.net
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[.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 03:01:28 2024 UTC