php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60789 pow() uses floats if one argument is a float
Submitted: 2012-01-18 13:00 UTC Modified: 2012-03-04 19:12 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: ircmaxell at gmail dot com Assigned: cataphract (profile)
Status: Closed Package: Math related
PHP Version: trunk-SVN-2012-01-18 (SVN) OS: *nix
Private report: No CVE-ID: None
 [2012-01-18 13:00 UTC] ircmaxell at gmail dot com
Description:
------------
I ran into an issue with pow() that causes it to lose precision on 64 bit 
platforms if one or more of the arguments is a float.  

I've attached a patch that fixes this by checking if both arguments are exactly 
representable as an integer, and if so converting to an integer.

This attempts to retain as much precision as possible by letting the result be an 
exact representation where possible.

Additionally, the patch fixes a tested-for-bug on 64 bit platforms where:

pow(-9.2233720368548E+18, 9223372036854775807) == INF when it should be -INF.



Test script:
---------------
var_dump((int) (pow(2, (float) 55) - 1));

Expected result:
----------------
int(36028797018963967)

Actual result:
--------------
int(36028797018963968)

Patches

pow_float_to_int_cast.patch (last revision 2012-01-18 13:00 UTC by ircmaxell at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-18 15:09 UTC] ircmaxell at gmail dot com
Changing type to bug, as this fixes the implementation to correspond to the 
docs:

http://us3.php.net/pow

Specifically:

"base raised to the power of exp. If the result can be represented as integer it 
will be returned as type integer, else it will be returned as type float."
 [2012-01-18 15:09 UTC] ircmaxell at gmail dot com
-Type: Feature/Change Request +Type: Bug
 [2012-03-04 19:08 UTC] cataphract@php.net
Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&revision=323895
Log: - Fixed doc bug #60789: pow() uses floats if one argument is a float.
 [2012-03-04 19:12 UTC] cataphract@php.net
Fixed in SVN as a documentation bug.
 [2012-03-04 19:12 UTC] cataphract@php.net
-Status: Open +Status: Closed -Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cataphract
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Thu Feb 09 02:05:49 2023 UTC