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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Sun May 05 04:01:32 2024 UTC