PHP Doku:: Potenzfunktion - function.pow.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzMathematische ErweiterungenMathematische FunktionenMathematische Funktionenpow

Ein Service von Reinhard Neidl - Webprogrammierung.

Mathematische Funktionen

<<pi

rad2deg>>

pow

(PHP 4, PHP 5)

powPotenzfunktion

Beschreibung

number pow ( number $base , number $exp )

Berechnet die Potenz von exp zur Basis base oder kurz baseexp.

Warnung

In PHP 4.0.6 und früher gab pow() immer einen float-Wert zurück und generierte keine Warnungen.

Parameter-Liste

base

Die zu potenzierende Basis

exp

Der Exponent

Rückgabewerte

base in der Potenz exp. Das Ergebnis wird als integer zurückgegeben falls möglich, ansonsten als float. Wenn die Potenz nicht berechnet werden, wird stattdessen FALSE zurückgegeben.

Changelog

Version Beschreibung
Seit 4.0.6 Die Funktion gibt nun integer-Ergebnisse zurück falls möglich, in älteren Versionen wurde immer ein float-Ergebnis zurückgegeben. Ältere Versionen geben zum Teil unsinnige Ergebnisse zurück, wenn das Ergebnis keine reelle Zahl ist.
Seit 4.2.0 PHP gibt keine Warnung mehr aus, wenn das Ergebnis nicht berechnet werden kann, es wird einfach nur noch FALSE zurückgegeben.

Beispiele

Beispiel #1 Einige pow()-Beispiele

<?php

var_dump
(pow(28)); // int(256)
echo pow(-120); // 1
echo pow(00); // 1

echo pow(-15.5); // PHP >4.0.6  NAN
echo pow(-15.5); // PHP <=4.0.6 1.#IND
?>

Siehe auch

  • exp() - Exponentialfunktion
  • sqrt() - Quadratwurzel
  • bcpow() - Potenz einer Zahl beliebiger Genauigkeit
  • gmp_pow() - Raise number into power


10 BenutzerBeiträge:
- Beiträge aktualisieren...
tagg_maiwald at yahoo dot com
2.11.2010 17:34
This function returns the value of a positive base with a signed floating point exponent:
function sf_exp( $fl_x = 1, $fl_y = 0)
{    $fl_exp = 0.0;
    if (0 > $fl_x)
    {    // Alter this logic container to enable processing of negative bases.
        $fl_exp = -1.0;
    } else
    {    $bool_neg = (0 > $fl_y);

        if ($bool_neg)
        {    $fl_y = 0 - $fl_y;
        }
   
        $fl_xlog = log10( $fl_x);
        $fl_xylog = ( $fl_xlog * $fl_y);
        $fl_exp = pow( 10, $fl_xylog);

        if ($bool_neg)
        {    $fl_exp = 1/$fl_exp;
        }
    }
   
    return $fl_exp;
}
Anonymous
25.04.2010 23:24
You can increase the 'precision' php.ini setting a little to work with larger float numbers here, but this comes at at cost of sacrificing decimal accuracy.  The default 'precision' is 14.  5 is about the threshhold that php can handle for decimal accuracy before at least some number corruption starts showing or it cannot output the actual number, and 16 for large number accuracy, as demonstrated by throwing this into the table below:
<?php
   
echo "<td>".pow(10, $i) - 1)."</td>";
?>

See the table below for an example, and adjust your php.ini 'precision' setting according to what your OS and PHP version can handle and what number size you want to work with.  Alternatively, you can use the bc math functions for more accuracy all around, and not have to rely on the 'precision' ini setting at all, but this moves out of the realm of strictly floats and into strings.

Also, PHP just prefers to display the 'E' notation of float values where possible after about 5 decimal places rather than the actual decimal number (1.0E-5 vs 0.00005).

<?php

echo "<table>";
for(
$i = 0; $i < 50; $i++) {
   
$precision = $i + 1;
   
ini_set('precision', $precision);
    echo
"<tr>";
    echo
"<td>".$precision."</td>";
    echo
"<td>".pow(10, $i)."</td>";
    echo
"<td>".pow(10, (-1 * $i))."</td>";
    echo
"<td>".bcpow('10', (string) $i, $precision)."</td>";
    echo
"<td>".bcpow('10', (string) (-1 * $i), $precision)."</td>";
    echo
"</tr>";
    }
echo
"</table>";

?>

Bottom line though is, if you're working with larger numbers or require very fine decimal precision or prefer displaying the full decimal number, use the bc math functions instead.  And, do check out PHP's considerations about the float type here: http://php.net/manual/en/language.types.float.php
Matt Dudley
17.07.2008 0:14
Calculate wind chill based on the National Weather Service formula.

$temp = 25;
$wind_speed_mph = 6;

$wind_chill = 35.74+(.6215*$temp_f)-(35.75*(pow($wind_speed_mph, 0.16)))+(.4275*$temp_f*(pow($wind_speed_mph, 0.16)));

Value only valid when the temp is 45 or below.... I used this with a weather script I wrote that reads an xml file. They don't provide wind chill.
Docey
4.05.2007 19: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+.
gilthansREMOVEME at gmail dot com
15.12.2006 16: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.05.2006 10: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.12.2004 17: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.11.2004 15: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.03.2004 8: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.07.2003 5: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)