Math Module Python Built-In Functions

Math Module Python Built-In Functions

In the previous tutorial, you learned how to do in-built math methods in your Python program without importing the math modules in Python.

But, Now here you will learn about all mathematical functions by the math module in Python. And you also know how you can use them in your python program.

A modules named Math is already built-in Python. And this module provides you with many mathematical methods. You can use the methods of this module in your Python program.

Before we use the built-in methods of these math modules, let us know how many methods are there in the math modules of Python.

We have provided a list below. Almost all methods of math modules are there:

Python math module methods

FunctionDescription
ceil(x)Returns the smallest integer greater than or equal to x.
copysign(x, y)Returns x with the sign of y
fabs(x)Returns the absolute value of x
factorial(x)Returns the factorial of x
floor(x)Returns the largest integer less than or equal to x
fmod(x, y)Returns the remainder when x is divided by y
frexp(x)Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable)Returns an accurate floating point sum of values in the iterable
isfinite(x)Returns True if x is neither an infinity nor a NaN (Not a Number)
isinf(x)Returns True if x is a positive or negative infinity
isnan(x)Returns True if x is a NaN
ldexp(x, i)Returns x * (2**i)
modf(x)Returns the fractional and integer parts of x
trunc(x)Returns the truncated integer value of x
exp(x)Returns e**x
expm1(x)Returns e**x – 1
log(x[, base])Returns the logarithm of x to the base (defaults to e)
log1p(x)Returns the natural logarithm of 1+x
log2(x)Returns the base-2 logarithm of x
log10(x)Returns the base-10 logarithm of x
pow(x, y)Returns x raised to the power y
sqrt(x)Returns the square root of x
acos(x)Returns the arc cosine of x
asin(x)Returns the arc sine of x
atan(x)Returns the arc tangent of x
atan2(y, x)Returns atan(y / x)
cos(x)Returns the cosine of x
hypot(x, y)Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x)Returns the sine of x
tan(x)Returns the tangent of x
degrees(x)Converts angle x from radians to degrees
radians(x)Converts angle x from degrees to radians
acosh(x)Returns the inverse hyperbolic cosine of x
asinh(x)Returns the inverse hyperbolic sine of x
atanh(x)Returns the inverse hyperbolic tangent of x
cosh(x)Returns the hyperbolic cosine of x
sinh(x)Returns the hyperbolic cosine of x
tanh(x)Returns the hyperbolic tangent of x
erf(x)Returns the error function at x
erfc(x)Returns the complementary error function at x
gamma(x)Returns the Gamma function at x
lgamma(x)Returns the natural logarithm of the absolute value of the Gamma function at x
piMathematical constant, the ratio of circumference of a circle to it’s diameter (3.14159…)
emathematical constant e (2.71828…)

Tell me one more important thing. Before using the method of the Math module, the Math module has to be imported into your Python program.

We take a simple example and in this example will tell you how to import the Math module of Python into your Python program:

# Square root calculation
import math

x = math.sqrt(4)

print(x)

In the example given above, we imported the math module and also used the sqrt() method of the math module.

Ceil method in python

The math.ceil() method rounds a number upwards to the nearest integer, and returns the result.

#Import math module
import math

#Round a number upward to its nearest integer
print(math.ceil(5.2))

#output 6

Copysign method in python

The math.copysign() method returns a float consisting of the value of the first parameter and the sign(+/-) of the second parameter.

#Import math Module
import math  

#Return the first value, with the sign of the second value
print(math.copysign(5, -1))

#output -5, 0

Fabs method in python

Let’s pass a number inside this method and it returns an absolute number.

Result Size: 668 x 459
4
#Import math module
import math  
​
#Remove - sign of given number
print(math.fabs(-55.44))
​
#Output 55.44

Factorial method in python

Inside this method, we pass a positive integer number and this method gives out the factorial of that number.

#Import math Module
import math  

#factorial of a number
print(math.factorial(5))

#output 120

Floor method in python

The python math floor() method is used to round a number to nearest downword integer.

#Import math module
import math

#Round given number downward nearest integer
print(math.floor(1.3))
print(math.floor(5.8))

#Output 1, 5

fmod method in python

Python fmod() method, which is used to calculate the Module of the specified given arguments ( remainder (modulo) of x/y ).

#Import math module
import math

x = 6
y = 2

#calculate mode
print(math.fmod(x, y)) 

#output 0, bacause 6/2 == 0

frexp method in python

This method returns mantissa and exponent as a pair (m, e) of a given value x, where mantissa m is a floating point number and e exponent is an integer value. m is a float and e is an integer such that x == m * 2**e exactly.

Result Size: 668 x 508
5
#Import math Library
import math  
​
#mantissa and exponent of a given number
print(math.frexp(5))
​
​#output (0.625, 3)

fsum method in python

python fsum() method, which is used to find the sum (in float) of the values of an iterable, it accepts an iterable object like an array, list, tuple, etc (that should contain numbers either integers or floats), and returns sum in float of all values.

#import math module
import math

# iterable objects
a = range(10)   #range value

# print sum of all values of the iterable objects
print("fsum(a): ", math.fsum(a))

#output fsum(a):  45.0

isfinite methods in python

math.isfinite() method, which is used to check whether a given number is a finite number or not. And it accepts a number like integer/float, finite, infinite or NaN numbers. Returns True if the argument is a valid python number. Otherwise, return false.

#import math module
import math

x = 10
y= float('inf')

print(math.isfinite(x))
print(math.isfinite(y))

#Output
#True
#False

math.isinf method in python

The isinf method, which is used to checks a given number is infinite or not. If yes, it returns True. Otherwise, it returns False.

#import math module
import math

x = 10
y= float('inf')

print(math.isinf(x))
print(math.isinf(y))

#Output
#False
#True

isnan method in python

isnan() method, which used to check whether a given parameter is a valid number or not. If given number is a valid Python number (Positive or Negative). Then it will return False. Otherwise, return true.

#import math module
import math

x = 10
y= float('nan')

print(math.isnan(x))
print(math.isnan(y))

#output
#False
#TRUE

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *