PHP Conference Kansai 2026 (JAPAN)

bcpow

(PHP 4, PHP 5, PHP 7, PHP 8)

bcpow — Eleva un numero a precisione arbitraria a una potenza

Descrizione

function bcpow(string $num, string $exponent, ?int $scale = null): string

Eleva num alla potenza exponent.

Elenco dei parametri

num

La base, come stringa.

exponent

L'esponente, come stringa. Deve essere un valore senza parte frazionaria. L'intervallo valido dell'esponente dipende dalla piattaforma, ma è almeno da -2147483648 a 2147483647.

scale

Questo parametro viene utilizzato per impostare il numero di cifre dopo la virgola nel risultato. Se è null, verrà utilizzato il valore predefinito impostato con bcscale(), o in alternativa il valore della direttiva INI bcmath.scale.

Valori restituiti

Restituisce il risultato come stringa.

Errori/Eccezioni

Questa funzione lancia un ValueError nei seguenti casi:

  • num o exponent non è una stringa numerica BCMath ben formata
  • exponent ha una parte frazionaria
  • exponent o scale è fuori dall'intervallo valido

Questa funzione lancia un'eccezione DivisionByZeroError se num è 0 e exponent è un valore negativo.

Log delle modifiche

Versione Descrizione
8.4.0 Le potenze negative di 0 in precedenza restituivano 0, ma ora lanciano un'eccezione DivisionByZeroError.
8.0.0 Quando exponent ha una parte frazionaria, ora lancia un ValueError invece di troncarla.
7.3.0 bcpow() ora restituisce numeri con la scala richiesta. In precedenza, i numeri restituiti potevano omettere gli zeri decimali finali.

Esempi

Example #1 Esempio di bcpow()

<?php

echo bcpow('4.2', '3', 2); // 74.08

?>

Note

Nota:

Prima di PHP 7.3.0, bcpow() poteva restituire un risultato con meno cifre dopo la virgola di quanto indicato dal parametro scale. Questo accade solo quando il risultato non richiede tutta la precisione consentita da scale. Ad esempio:

Example #2 Esempio della scala in bcpow()

<?php
echo bcpow('5', '2', 2);     // visualizza "25", non "25.00"
?>

Vedere anche:

  • bcpowmod() - Eleva un numero a precisione arbitraria a una potenza, ridotto rispetto a un modulo specificato
  • bcsqrt() - Ottiene la radice quadrata di un numero a precisione arbitraria
  • BcMath\Number::pow()

+add a note

User Contributed Notes 3 notes

up
0
Michael Bailey (jinxidoru at byu dot net)
22 years ago
bcpow() only supports exponents less than or equal to 2^31-1.  Also, bcpow() does not support decimal numbers.  If you have scale set to 0, then the exponent is converted to an interger; otherwise an error is generated.

--
Michael Bailey
http://www.jinxidoru.com
up
-2
thomas at tgohome dot com
17 years ago
<?php

bcscale(100);

/*
 * Computes the natural logarithm using a series.
 * @author Thomas Oldbury.
 * @license Public domain.
 */
function bcln($a, $iter = 10)
{
    $result = "0.0";
    
    for($i = 0; $i < $iter; $i++)
    {
        $pow = (1 + (2 * $i));
        $mul = bcdiv("1.0", $pow);
        $fraction = bcmul($mul, bcpow(bcsub($a, "1.0") / bcadd($a, "1.0"), $pow));
        $result = bcadd($fraction, $result);
    }
    
    return bcmul("2.0", $result);
}

/*
 * Computes the base2 log using baseN log.
 * @note Requires above functions.
 * @author Thomas Oldbury.
 * @license Public domain.
 */
function bclog2($a, $iter = 10)
{
    return bcdiv(bcln($a, $iter), bcln("2", $iter));
}

/*
 * Computes the base10 log using baseN log.
 * @note Requires above functions.
 * @author Thomas Oldbury.
 * @license Public domain.
 */
function bclog10($a, $iter = 10)
{
    return bcdiv(bcln($a, $iter), bcln("10", $iter));
}


?>
up
-2
Anonymous
21 years ago
Well, if bcpow has limits, then this should work:
<?php
function bcpow_($num, $power) {
    $awnser = "1";
    while ($power) {
        $awnser = bcmul($awnser, $num, 100);
        $power = bcsub($power, "1");
    }
    return rtrim($awnser, '0.');
}
?>
Just that $power cannot have decimal digits in it.
To Top