# Python Mathematical Function

Functions can do anything, but their primary use pattern is taking parameters and returning values . The math module provides some basic mathematical functions for working with floating-point numbers. Using Math module in python, we can access to different mathematical functions already defined by the C standard. These functions perform various arithmetic operations like calculating the floor, ceiling, or absolute value of a number using the floor(x), ceil(x), and fabs(x) functions respectively. Here is the list of all the functions and attributes defined in math module with a brief explanation of what they do. ceil(x)

Return the ceiling of x as a float, the smallest integer value greater than or equal to x.

print (math.ceil(-125.22)) print (math.ceil(620.12)) print (math.ceil(78.72)) print (math.ceil(math.pi))
output
-125 621 79 4
copysign(x, y)

Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. (New in version 2.6.)

print(math.copysign(12,10)) print(math.copysign(12,-10))
output
12.0 -12.0
fabs(x)

This function will return an absolute or positive value.

print(math.fabs(56)) print(math.fabs(-12.8)) print(math.fabs(12.8))
output
56.0 12.8 12.8
factorial(x)

Returns the factorial of x

print(math.factorial(20)) print(math.factorial(12)) print(math.factorial(17)) print(math.factorial(8))
output
2432902008176640000 479001600 355687428096000 40320
floor(x)

The method floor() returns floor of x - the largest integer not greater than x.

print (math.floor(18)) print (math.floor(-4.5)) print (math.floor(2.5))
output
18 -5 2
fmod(x, y)

This function returns x % y.

print(math.fmod(50,10)) print(math.fmod(10,5)) print(math.fmod(-40,24)) print(math.fmod(-15,7))
output
0.0 0.0 -16.0 -1.0
frexp(x)

Returns the mantissa and exponent of x as the pair (m, e)

print(math.frexp(6.8)) print(math.frexp(0)) print(math.frexp(6))
output
(0.85, 3) (0.0, 0) (0.75, 3)
fsum(iterable)

This function adds the items of an iterable and returns the sum.

num = [0.9999999, 1, 2, 3] # Sum values with fsum. val = math.fsum(num) print(val)
output
6.9999999
isfinite(x)

This function is either True or False.

print(math.isfinite(8)) print(math.isfinite(0.0)) # Python considered 0.0 as finite number print(math.isfinite(0/2)) print(math.isfinite(-100))
output
True True True True
isinf(x)

This function Returns True if x is a positive or negative infinity

print(math.isinf(8)) print(math.isinf(0.0)) # Python considered 0.0 as finite number print(math.isinf(0/2)) print(math.isinf(-100))
output
False False False False
isnan(x)

Returns True if x is a NaN

print(math.isnan(10)) print(math.isnan(0.0)) print(math.isnan(0.5)) print(math.isnan(-12))
output
False False False False
ldexp(x, i)

This function returns the result of x * (2**i) which is an inverse of the frexp() function.

print (math.ldexp(12,8)) print (math.ldexp(-4.3,4)) print (math.ldexp(2.5,-7))
output
3072.0 -68.8 0.01953125
modf(x)

This function returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.

print (math.modf(20.22)) print (math.pi)
output
(0.21999999999999886, 20.0) 3.141592653589793
trunc(x)

This function returns the truncated integer of x.

print(math.trunc(4.454))
output
4
exp(x)

This function returns exponential of x: ex.

print(math.exp(8)) print(math.exp(0.0)) print(math.exp(0.005))
output
2980.9579870417283 1.0 1.005012520859401
pow(x, y)

This function returns x raised to the power y

print(math.pow(10, 2) ) print(math.pow(200, -2)) print(math.pow(2, 2))
output
100.0 2.5e-05 4.0
sqrt(x)

Returns the square root of x

print (math.sqrt(0)) print (math.sqrt(10)) print (math.sqrt(2.5))
output
0.0 3.1622776601683795 1.5811388300841898
pi

Mathematical constant, the ratio of circumference of a circle to it's diameter (3.14159...)

print (math.pi)
output
3.141592653589793
e

mathematical constant e (2.71828...)

print (math.e)
output
2.718281828459045