PHP – 如何使用 bcpow() 函式將任意精度數字提升到另一個數字?


在 PHP 中,**bcpow()** 函式用於將任意精度的基數提升到另一個指數。它將兩個任意精度的數字作為字串,並將基數提升到指數冪後,將結果縮放到列出的精度。

語法

String bcpow($base, $exponent, $scale)

引數

PHP 中的 **bcpow()** 函式接受三個不同的引數:**$base**、**$exponent** 和 **$scale**。

  • **$base -** 它表示將提升到冪的基數,它是字串型別引數。

  • **$exponent -** 它表示指數,它是字串型別引數。

  • **$scale -** 它表示基數指數結果小數點後出現的數字位數。其預設值為 0,它是整數型別引數。

返回值

**bcpow()** 函式返回 **(基數)**指數 的值。

示例 1 - 不使用 scale 引數的 bcpow() PHP 函式

<?php
   // input base and exponent numbers
   $base = "5";
   $exponent = "7";

   // calculates the value
   //number without scale value
   $result = bcpow($base, $exponent);

   //used equal parameters
   echo "The output is: ", $result;
?>

輸出

The output is: 78125

示例 2 - 使用 scale 引數的 bcpow() PHP 函式

現在讓我們使用 3 的 scale 來獲取相同的輸入值,並檢查輸出。

<?php
   // input base and exponent numbers
   $base = "2";
   $exponent = "3";

   //used scale value two
   $scaleval = 3;

   // calculates the value
   //number without scale value
   $result = bcpow($base, $exponent, $scaleval);

   //used equal parameters
   echo "Output with scale value: ", $result;
?>

輸出

Output with scale value: 8.000

更新於:2021年8月21日

139 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告