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 :



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

16
0.0625

 

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

sqrt(number);

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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())

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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())

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

952458556
87

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.