Tutorialspoint.dev

PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)

11. is_nan() :
This function takes any value as argument and checks whether a value is number or not. It returns TRUE (1) if the specified value is ‘not a number’, otherwise it returns FALSE/NOTHING.

Syntax :

is_nan(value);

Example :

<?php
echo is_nan(234) . " ";
echo is_nan(asin(1.1)) ;
?>

Output :

1

Note : (is_finite(value)) is equivalent to (!is_infinite(value) && (!is_nan(value)), i.e. a number can only be one of finite, infinite and NaN(not a number). You don’t need to check both is_infinite() and is_nan() to see if a number is invalid or out of range.



 

12. pow() :
This function takes base and exponent as arguments and returns base raised to the power of exponent.
Syntax :

pow(base,exponent);

Example :

<?php
echo(pow(2,4) . " ");
echo(pow(-2,-4));
?>

Output :

16
0.0625

 

13. sqrt() :
This function takes numeric value as arguments and returns the square root of value.
Syntax :

sqrt(number);

Example :

<?php
echo(sqrt(9) . " ");
echo(sqrt(0.64));
?>

Output :

3
0.8

 
14. exp() :
The function returns e raised to the power of x .’e’ is the base of the natural system of logarithms (approximately 2.718282) and x is the number passed to it.
Syntax :

exp(x);

Example :



<?php
# PHP code to calculate exponent of e.
function exponent($number){
    return (exp($number));
}
  
// Driver Code
$number = 0;
echo (exponent($number));
?>

Output :

1

 

15. log() :
This function takes any number and base as arguments and returns the natural logarithm of a number, or the logarithm of number to base.

Syntax :

log(number, base);

In this, number specifies the value to calculate the logarithm for.
base(Optional) specifies the logarithmic base to use (Default is ‘e’).

Example :

<?php
echo(log(5.987) . " ");
echo(log(1));
?>

Output :

1.7895904519432
0

 
16. log10() :
This function takes any number as argument and returns the base-10 logarithm of a number.

Syntax :

log10(number);

Here, number specifies the value to calculate the logarithm for.



Example :

<?php
echo(log10(5.987) . " ");
echo(log10(0));
?>

Output :

0.77720925814568
-INF

 

17. log1p() :
This function takes any number as argument and returns log(1+number), computed in a way that is accurate even when the value of number is close to zero.

Syntax :

log1p(number);

Here, number specifies the number to process.

Example :

<?php
echo(log1p(5.987) . " ");
echo(log1p(0));
?>

Output :

1.9440512795703
0

 

18. max() :
In this function, if the first and only parameter is an array, max() returns the highest value in that array. If at least two parameters are provided, max() returns the biggest of these values.



Syntax :

max(array_values);
or
max(value1,value2,...);

Example :

<?php
echo(max(3,4,6,12,10) . " ");
echo(max(array(48,76,83,22)));
?>

Output :

12
83

 
19. min() :
In this function, if the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values.

Syntax :

min(array_values);
or
min(value1,value2,...);

Example :

<?php
echo(min(3,4,6,12,10) . " ");
echo(min(array(48,76,83,22)));
?>

Output :

3
22

 

20. getrandmax() :
This function dosen’t take any arguments and returns the largest possible value that can be returned by rand() function.

Syntax :



getrandmax();

Example :

<?php
echo(getrandmax()); 
?>

Output :

2147483647

 
21. rand() :
If this function is called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 12 and 56 (inclusive). Example, use rand(12, 56).

Syntax :

rand();
or
rand(min,max);

Example :
Here, min Specifies the lowest number to be returned(Default is 0).
max Specifies the highest number to be returned(Default is getrandmax())

<?php
echo(rand() . " ");
echo(rand(12,100));
?>

Output :

1135079733
76

Output may vary time to time as it generates random number.

 

22. mt_rand() :
This function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the Mersenne Twister algorithm, which will produce random numbers four times faster than what the average rand() provides.
If called without the optional min, max arguments mt_rand() returns a pseudo-random value between 0 and mt_getrandmax(). If you want a random number between 12 and 56 (inclusive),Example, use mt_rand(12, 56).

Syntax :

mt_rand();
or
mt_rand(min,max);

Example :
Here, min Specifies the lowest number to be returned(Default is 0).
max Specifies the highest number to be returned(Default is getrandmax())

<?php
echo(mt_rand() . " ");
echo(mt_rand(12,100));
?>

Output :

952458556
87

Output may vary time to time as it generates random number.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter