bcpow

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

bcpow任意精度数字的乘方

说明

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

numexponent 次方运算。

参数

num

string 类型的底数。

exponent

string 类型的指数。必须是没有小数部分的值。指数的有效范围取决于平台,但起码支持 -21474836482147483647 的范围。

scale
此参数用于设置结果中的小数位数。如果为 null,则使用 bcscale() 设置的默认小数位数,或者回退到 bcmath.scale INI 指令的值。

返回值

返回字符串类型的结果。

错误/异常

函数在下列情况会抛出 ValueError

  • numexponent 不是格式正确的 BCMath 数字字符串
  • exponent 有小数部分
  • exponentscale 超出有效范围

如果 num0exponent 为负值,则此函数抛出 DivisionByZeroError 异常。

更新日志

版本 说明
8.4.0 0 的负幂以前返回 0,但现在会引发 DivisionByZeroError 异常。
8.0.0 exponent 有小数部分时,现在会抛出 ValueError 而不是截断。
7.3.0 现在 bcpow() 可以按想要的小数点位数返回数字。 而之前,返回的数字会忽略尾随零(trailing decimal zeroes)。

示例

示例 #1 bcpow() 示例

<?php

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

?>

注释

注意:

Before PHP 7.3.0 bcpow() may return a result with fewer digits after the decimal point than the scale parameter would indicate. This only occurs when the result doesn't require all of the precision allowed by the scale. For example:

示例 #2 bcpow() scale example

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

参见

  • bcpowmod() - Raise an arbitrary precision number to another, reduced by a specified modulus
  • bcsqrt() - 任意精度数字的二次方根