The bcpowmod() function in PHP is an inbuilt function and is used to raise an arbitrary precision base number to another exponent number reduced by a specified modulus. This function accepts three arbitrary precision numbers as strings and returns the base number raised to exponent modulo under a number after scaling the result to a specified precision.
Syntax:
string bcpowmod ( $base, $exponent, $mod, $scaleVal )
Parameters: This function accepts four parameters as shown in the above syntax and explained below:
- $base: This parameter is of string type and represents the left operand or the number which is the bas in which the power will be raised. This parameter is mandatory.
- $exponent: This parameter is of string type and represents the right operand or one of the number which represent the exponent. This parameter is mandatory.
- $mod: This parameter is of string type and accepts a operand or a number which represents the modulos. 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)%mod. It’s default value is zero.
Return Value: This function returns the ($base$exponent) % $mod result as string.
Examples:
Input: $base = 2, $exponent = 3 $mod = 3 Output: 2 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, $mod = 3, $scaleVal = 2 Output: 2.00
Below programs illustrate the bcpowmod() function in PHP :
Program 1:
<?php // PHP program to illustrate bcpowmod() function // input numbers with arbitrary precision $base = "2" ; $exponent = "3" ; $mod = "3" ; // calculates the base^exponent % mod // when $scaleVal is not specified $res = bcpowmod( $base , $exponent , $mod ); echo $res ; ?> |
Output:
2
Program 2:
<?php // PHP program to illustrate bcpowmod() function // input numbers with arbitrary precision $base = "2" ; $exponent = "3" ; $mod = "3" ; // scale value $scaleVal = 4; // calculates the base^exponent % mod // when $scaleVal is specified $res = bcpowmod( $base , $exponent , $mod , $scaleVal ); echo $res ; ?> |
Output:
2.0000
Reference:
http://php.net/manual/en/function.bcpowmod.php
leave a comment
0 Comments