Its not just x86 system that return pow(2, 1024) as INF.
On my x64 system running php complied for x64 I still find that the maximum I can do is pow(2, 1023).
Perhaps this is because of the x86_64 instructions and the limit can only be pushed further on "true" x64 systems like IA64.
pow
(PHP 4, PHP 5)
pow — 指数表現
説明
number pow
( number $base
, number $exp
)
base の exp 乗を返します。
警告
PHP 4.0.6 より前のバージョンでは、pow() は 常に float を返します。この場合、警告は発生しません。
パラメータ
- base
-
使用する基数。
- exp
-
指数。
返り値
base の exp 乗を 返します。可能な場合、この関数は、integer 型の値を 返します。累乗が計算できない場合は FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 4.0.6 以降 | 可能な場合は、結果を integer で返すようになりました。 以前は、結果を常に float で返していました。 そのため、値によっては間違った結果となることがありました。 |
| 4.2.0 以降 | PHP 値が計算できない場合に警告を発生することはなくなり、 単に FALSE を返すだけとなりました。 |
例
Example#1 pow() の例
<?php
var_dump(pow(2, 8)); // int(256)
echo pow(-1, 20); // 1
echo pow(0, 0); // 1
echo pow(-1, 5.5); // エラー
?>
pow
Lant
01-Jul-2007 01:25
01-Jul-2007 01:25
Docey
05-May-2007 02:33
05-May-2007 02:33
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+. i gues this is thus the same for all 32bit i386 systems.
adverneo at gmx dot de
20-Apr-2007 02:11
20-Apr-2007 02:11
Be aware of breaking the integer-limit. $var = pow(2,32); will produce a buffer overflow in PHP 5.0.3 (already reported)
gilthansREMOVEME at gmail dot com
16-Dec-2006 12:50
16-Dec-2006 12: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 05:27
10-May-2006 05: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);};
?>
admin at mattwilko dot com
08-Apr-2005 01:32
08-Apr-2005 01:32
Here's a function that works with negative powers:
<?php
function newpow($base, $power)
{
if ($power < 0) {
$npower = $power - $power - $power;
return 1 / pow($base, $npower);
}
else
{
return pow($base, $power);
}
}
?>
louis [at] mulliemedia.com
01-Jan-2005 01:02
01-Jan-2005 01: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 11:26
10-Nov-2004 11: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 04:03
18-Mar-2004 04: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
18-Jul-2003 12:01
18-Jul-2003 12: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.