PHP – How to raise an arbitrary precision number to another using bcpow() function?


In PHP, bcpow() function is used to raise an arbitrary precision base number to another exponent number. It takes two arbitrary precision numbers as strings and gives the base number raised to the power exponent after scaling the result to the listed precision.

Syntax

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

Parameters

The bcpow() function in PHP takes three different parameters: $base, $exponent and $scale.

  • $base - It represents the base in which power will be raised and it is the string type parameter.

  • $exponent - It represents the exponent and it is the string type parameter.

  • $scale - It indicates the number of digits that appear after the decimal in the result of exponent of a base exponent. Its default value is 0 and it is an integer type parameter.

Return Value

The bcpow() function returns the value of (Base)Exponent .

Example1 - bcpow() PHP function without using scale parameter

<?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;
?>

Output

The output is: 78125

Example 2 - bcpow() PHP function using scale parameter

Let us now take the same input values with a scale of 3 and check the output.

<?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

Output with scale value: 8.000

Updated on: 21-Aug-2021

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements