no integer breaking here, pow just silently switches to using floats instead of integers.
pow(2, 31) = integer value
pow(2, 32) = float value.
the manual says the limit for floats is machine dependent so i did a little loop to see how far it will go before becomming infinit. the result is 1023.
pow(2, 1023) = float
pow(2, 1024) = ifinit.
tested on php 4.4.1 under windows2000 on an AMD AthlonXP 2800+.
pow
(PHP 4, PHP 5)
pow — Exponential expression
Description
Returns base raised to the power of exp .
Warning
In PHP 4.0.6 and earlier pow() always returned a float, and did not issue warnings.
Parameters
- base
-
The base to use
- exp
-
The exponent
Return Values
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. If the power cannot be computed FALSE will be returned instead.
ChangeLog
| Version | Description |
|---|---|
| Since 4.0.6 | The function will now return integer results if possible, before this it always returned a float result. For older versions, you may receive a bogus result for complex numbers. |
| Since 4.2.0 | PHP stops to emit a warning if the value can't be computed, it will now silently return FALSE only. |
Examples
Example #1 Some examples of pow()
<?php
var_dump(pow(2, 8)); // int(256)
echo pow(-1, 20); // 1
echo pow(0, 0); // 1
echo pow(-1, 5.5); // PHP >4.0.6 NAN
echo pow(-1, 5.5); // PHP <=4.0.6 1.#IND
?>
pow
Docey
04-May-2007 01:33
04-May-2007 01:33
gilthansREMOVEME at gmail dot com
15-Dec-2006 10:50
15-Dec-2006 10:50
Note that pow(0, 0) equals to 1 on PHP 4 (only tested it there), although mathematically this is undefined.
moikboy (nospam) moikboy (nospam) hu
10-May-2006 04:27
10-May-2006 04:27
Here is a function for calculating the $k-th root of $a :
<?php
function root($a,$k){return(($a<0&&$k%2>0)?-1:1)*pow(abs($a),1/$k);};
?>
louis [at] mulliemedia.com
31-Dec-2004 11:02
31-Dec-2004 11:02
Here's a pow() function that allows negative bases :
<?php
function npow($base, $exp)
{
$result = pow(abs($base), $exp);
if ($exp % 2 !== 0) {
$result = - ($result);
}
return $result;
}
?>
janklopper .AT. gmail dot.com
10-Nov-2004 09:26
10-Nov-2004 09:26
since pow doesn't support decimal powers, you can use a different sollution,
thanks to dOt for doing the math!
a^b = e^(b log a)
which is no the 10log but the e-log (aka "ln")
so instead of: pow( $a , 0.6 ) use something like: exp( 0.6 * log($a) )
matthew underscore kay at ml1 dot net
18-Mar-2004 02:03
18-Mar-2004 02:03
As of PHP5beta4, pow() with negative bases appears to work correctly and without errors (from a few cursory tests):
pow(-3, 3) = -27
pow(-3, 2) = 9
pow(-5, -1) = -0.2
bishop
17-Jul-2003 11:01
17-Jul-2003 11:01
A couple of points on pow():
1. One of the official examples of pow(2,8) is not pragmatic; use 1 << 8 as it's substantially faster
2. When passing variables to pow(), cast them otherwise you might get warnings on some versions of PHP
3. All the rules of algebra apply: b**(-e) is 1/(b**e), b**(p/q) is the qth root of b**p
So, e.g., sqrt($x) === pow($x, .5); but sqrt() is faster.
















