Tutorialspoint.dev

PHP | bcpow() Function

The bcpow() function in PHP is an inbuilt function and is used to calculate the value of an arbitrary precision base number raised to another exponent number. This function accepts two arbitrary precision numbers as strings and returns the base number raised to exponent after scaling the result to a specified precision.

Syntax:

string bcpow ( $base, $exponent, $scaleVal )

Parameters: This function accepts three parameters as shown in the above syntax and explained below:



  • $base: This parameter is of string type and represents the base in which the power will be raised. This parameter is mandatory.
  • $exponent: This parameter is of string type and represents the exponent. This parameter is mandatory.
  • $scaleVal: This parameter is of int type and is optional. This parameter tells the number of digits that will appear after the decimal in the result of baseexponent. It’s default value is zero.

Return Value: This function returns the $base$exponent result as string.

Examples:

Input:  $base = 2, $exponent = 3 
Output: 8
Since the parameter $scaleVal is not specified so
no digits after decimal is appeared in the 
result after evaluating result

Input:  $base = 2, $exponent = 3, $scaleVal = 2
Output: 8
Note: Instead of 8.00, output of 8 is given. 
This is an exception in bc math functions.

Below programs illustrate the bcpow() function in PHP :

Program 1:

<?php
// PHP program to illustrate bcpow() function
   
// input numbers with arbitrary precision
$base = "2";
$exponent = "3"
   
// calculates the base^exponent
// the two numbers when $scaleVal is
// not specified
$res = bcpow($base, $exponent);
  
echo $res;
   
?>

Output:

2

Program 2:

<?php
// PHP program to illustrate bcpow() function
   
// input numbers with arbitrary precision
$base = "2";
$exponent = "3";
  
// scale value
$scaleVal = 4;
  
// calculates the base^exponent
// the two numbers when $scaleVal is
// specified 
$res = bcpow($base, $exponent, $scaleVal); 
  
echo $res;
?>

Output:

2

Reference:
http://php.net/manual/en/function.bcpow.php



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter