|
pow
Exponential expression
(PHP 4, PHP 5)
Example 1156. Some examples of pow()<?php Related Examples ( Source code ) » pow Examples ( Source code ) » Exp Examples ( Source code ) » Using the arbitrary-precision functions for exact integer arithmetic Examples ( Source code ) » Get the Power Examples ( Source code ) » Create two child classes Examples ( Source code ) » Method override for Rectangle class Examples ( Source code ) » Use foreach, while and list to loop through associate array Examples ( Source code ) » Overriding the Function of a Base Class Examples ( Source code ) » Calling an Overridden Function Examples ( Source code ) » Math Function Library Code Examples / Notes » powjanklopper .at. gmail dot.com
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) ) gilthansremoveme
Note that pow(0, 0) equals to 1 on PHP 4 (only tested it there), although mathematically this is undefined.
docey
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. lant
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. louis
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; } ?> admin
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); } } ?> moikboy nospam moikboy nospam hu
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);}; ?> adverneo
Be aware of breaking the integer-limit. $var = pow(2,32); will produce a buffer overflow in PHP 5.0.3 (already reported)
matthew underscore kay
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
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. |