Open In App

PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

As you know PHP is a server side script language and is used for creating dynamic web pages. PHP provides a number of built-in math functions which help in performing several operations while dealing with mathematical data. It has nice support for mathematical processing. No installation is required to use these functions. Some examples of mathematical functions in PHP are as follows: 

1. abs(): This function takes negative value as input and returns the absolute (positive) value of a integer or float number. 

Syntax :

abs(number);

In this function ‘number’ can be float or integer.

 Example : 

php




<?php
// PHP code to return absolute value.
function absolute($degree)
{
    return (abs($degree));
}
 
// Driver Code
$number = -8.4;
echo(absolute($number));
?>


Output :

8.4

 2. pi(): This function returns the value of pi. The named constant M_PI is identical to pi().

 Syntax :

pi();

Example : 

php




<?php
# PHP function to convert degree to radian value.
echo(pi());
?>


Output :

3.1415926535898

 3. deg2rad() : This function takes a degree value as input and convert it to a radian value. 

Syntax :

deg2rad(number);

Example : 

php




<?php
// PHP code to convert degree to radian value.
function deg_radian($degree)
{
    return (deg2rad($degree));
}
 
// Driver Code
$number = 180;
echo(deg_radian($number));
?>


Output :

3.1415926535898

  4. rad2deg() : This function takes a radian value as input and convert it to a degree value. 

Syntax :

rad2deg(number);

Example : 

PHP




<?php
// PHP function to convert degree to radian value.
echo(rad2deg(pi()));
?>


Output :

180

  5. fmod() : This function takes two arguments as input returns the floating point remainder (modulo) of division of arguments. 

Syntax :

fmod(x, y);

Here, x is dividend and y is divisor. The remainder (r) is defined as: x = i * y + r, for some integer i. If y is non-zero, r has the same sign as x and a magnitude less than the magnitude of y. 

Example : 

PHP




<?php
// PHP code to find modulo of x / y
function result($x, $y)
{
    return fmod($x, $y);
}
 
// Driver function
$x = 5.7;
$y = 1.3;
echo(result($x, $y));
?>


Output :

0.5

Here, $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7 (x = i * y + r)  

6. floor() : This function takes numeric value as argument and returns the next lowest integer value (as float) by rounding down value if necessary. 

Syntax :

floor($number);

Example : 

php




<?php
echo(floor(0.60)."\n");
echo(floor(5)."\n");
echo(floor(-5.9));
?>


Output :

0
5
-6

 7. ceil() : This function takes numeric value as argument and returns the next highest integer value by rounding up value if necessary. 

Syntax :

ceil($number);

Example : 

PHP




<?php
echo(ceil(0.60)."\n");
echo(ceil(-5.9));
?>


Output :

1
-5

8. round() : This function takes numeric value as argument and returns the next highest integer value by rounding up value if necessary. 

Syntax :

round(number, precision, mode);

In this, number specifies the value to round, precision specifies the number of decimal digits to round to (Default is 0) and, mode(optional) specifies one of the following constants to specify the mode in which rounding occurs :

  • PHP_ROUND_HALF_UP : (set by Default) Rounds number up to precision decimal, when it is half way there. Rounds 1.5 to 2 and -1.5 to -2
  • PHP_ROUND_HALF_DOWN : Round number down to precision decimal places, when it is half way there. Rounds 1.5 to 1 and -1.5 to -1
  • PHP_ROUND_HALF_EVEN : Round number to precision decimal places towards the next even value.
  • PHP_ROUND_HALF_ODD : Round number to precision decimal places towards the next odd value.

Example : 

php




<?php
echo(round(1.95583, 2)."\n");
echo(round(1241757, -3)."\n");
echo(round(9.5, 0, PHP_ROUND_HALF_UP)."\n");
echo(round(9.5, 0, PHP_ROUND_HALF_DOWN)."\n");
echo(round(9.5, 0, PHP_ROUND_HALF_EVEN)."\n");
echo round(9.5, 0, PHP_ROUND_HALF_ODD);
?>


Output :

1.96
1242000
10
9
10
9

 9. is_finite() : This function takes any value as argument and checks whether a value is finite or not. It returns TRUE (1) if the specified value is a finite number, otherwise it returns FALSE/NOTHING. The value is a legal finite if it is within the allowed range for a PHP float on this platform. 

Syntax :

is_finite(value);

Example : 

php




<?php
echo is_finite(234)."\n";
echo is_finite(log(0));
?>


Output :

1

 10. is_infinite() : This function takes any value as argument and checks whether a value is infinite or not. It returns TRUE (1) if the specified value is a infinite number, otherwise it returns FALSE/NOTHING. The value is a legal infinite if it is outside the allowed range for a PHP float on this platform. 

Syntax :

is_infinite(value);

Example : 

php




<?php
echo is_infinite(234)."\n";
echo is_infinite(log(0));
?>


Output :

1


Similar Reads

PHP is_finite(), is_infinite(), is_nan() Functions
Given any numerical value, it can be classified into 3 different classes such as Finite Number, Infinite Number, and Not a Number or commonly known as NaN. While developing a project that is highly dependent on User Inputs there might be many cases where the user provides inappropriate inputs while a function expects a finite numerical input thus c
3 min read
PHP deg2rad() Function
Among the methods to measure angles, the two most commonly used are degrees and radians. Students typically learn about degree while learning about radian becomes a later concern. But there are certain cases where radian is the preferred unit of measurement such as: While working with the derivatives of trigonometric functions, it is preferable to
2 min read
PHP rad2deg() Function
Measurement of Angle is one of the foundation stones of Geometry and Trigonometry, and the two most commonly used methods of measurement are Degrees and Radians. Because of it's simplicity many prefer to use degree over Radian. Some of the reasons that makes degrees more preferable are: Degrees may not be much efficient while calculating derivative
2 min read
PHP fmod() Function
fmod stands for Floating Modulo. Modulo calculates the remainder of a division which is generally noted with the '%' symbol. However, the general modulo expression expects both the divisor and dividend to be of integer type, this is a great limitation to such an important operation. In PHP the fmod() function is used to calculate the Modulo of any
2 min read
JavaScript Math abs() Method
Javascript Math.abs() method is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value. Syntax: Math.abs(value)Parameters: This method accepts a single parameter as mentioned above and described below: value: The number whose absolute value is to be found is passed as the parameter to this f
2 min read
Less.js Math abs() Function
Less (Leaner Style Sheets) is an extension to normal CSS code which is basically used to enhance the abilities of normal CSS code and provide it superpowers. The abs() function is a type of Math function in Less.js (which is basically used to perform mathematical operations). The abs() function is used to get the absolute value of the given argumen
2 min read
JavaScript Math ceil() Method
The JavaScript Math.ceil method is used to return the smallest integer greater than or equal to a given number. The ceil() is always used as Math.ceil() since it is a static method of Math. Syntax: Math.ceil(value)Parameters: This method accepts single parameters as mentioned above and described below: Value: It is the value that is to be tested fo
1 min read
Less.js Math ceil() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is an extension to normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-in Math function that can be used to customize the CSS properties, whic
3 min read
JavaScript Math.ceil( ) function
Math.ceil() function in JavaScript is used to round the number passed as a parameter to its nearest integer in an Upward direction of rounding i.e. towards the greater value. Syntax:Math.ceil(value);Parameters: This function accepts a single parameter as the number to be rounded to its nearest integer in the upward rounding method. Return Value: Re
2 min read
Less.js Math floor() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is an extension to normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-in Math function that can be used to customize the CSS properties, whic
3 min read