Math module of Python
In this tutorial, you are going to learn about the math module of python. Math module is a built-in module of python. This module helps us to do mathematical computation by importing this module.
Different functions of the math module in Python
- ceil(n)
The ceiling of x returns the next integer which is not less than x.
If the value of x is an integer value then it returns the x else if x has the float value then it return the next integer.import math # taking integer value print("The ceil value of 4 is:", math.ceil(4)) # taking float value print("The ceil value of 4.1 is:", math.ceil(4.1))
Output:-
The ceil value of 4 is: 4 The ceil value of 4.1 is: 5
- factorial(n)
It returns the factorial of the integer number and gives the error message if the number is not an integer.import math # Factorial of a integer number print("The factorial of 3 is:", math.factorial(3)) # gives error because of the negative number print("The factorial of negative is:", math.factorial(-3))
Output:-
The factorial of 3 is: 6 Traceback (most recent call last): File "C:\Users\user\Desktop\rev.py", line 3, in <module> print(" The factorial of negative is:", math.factorial(-3)) ValueError: factorial() not defined for negative values
Also see: Catalan Number in Python – Iterative Approach (Factorial)
- floor(n)
It returns the integer value of the given number.import math # floor value of float number print("The floor value of 5.87678 is:", math.floor(5.87678)) # floor value of int number print("The floor value of 4 is:", math.floor(4))
Output:-
The floor value of 5.87678 is: 5 The floor value of 4 is: 4
- fabs(n)
It converts the integer number into the float number and if the given number has the floating value, then it returns it as it is.
Ex: fabs(3) returns 3.0import math # Fabs value of int number print("The fabs value of 4 is:", math.fabs(4)) # fabs value of float number print("The fabs value of 4.657 is:", math.fabs(4.657))
Output:-
The fabs value of 4 is: 4.0 The fabs value of 4.657 is: 4.657
- exp(n)
It returns the natural logarithm e raised to the given number.import math # use of exp() print(" The exponential of 1 is:", math.exp(1))
Output:-
The exponential of 1 is: 2.718281828459045
- sqrt(n)
It returns the square root of the given number for x>0.import math # Square root of int number print(" The sqaure root of 4 is:", math.sqrt(4)) # Square root of float value print(" The sqaure root of 4.4 is:", math.sqrt(4.4))
Output:-
The sqaure root of 4 is: 2.0 The sqaure root of 4.4 is: 2.0976176963403033
- log(n, baseto)
It returns the logarithm of the number to the given base. If the base is not specified, then return the logarithm with base e.import math # use of log() print(" The log value of 100 with base 10 is:", math.log(100,10)) print(" The log value of 2 with base e is:", math.log(2))
Output:-
The log value of 100 with base 10 is: 2.0 The log value of 2 with base e is: 0.6931471805599453
- copysign(x,y)
Return a float with the magnitude (absolute value) of x but the sign of y.
Ex: copysign(1.0, -0.0) returns -1.0 because it copies the value of x and allots the sign of y.
import math # use of copysign() print(" Copysign of 4 is:", math.copysign(4,-5))
Output:-
Copysign of 4 is: -4.0
- modf(n)
It returns the fractional and integral part of the given number.
Ex: modf(3.456) returns (0.456, 3.0)import math # modf() of positive number print(" The modf of 6.7585 is:", math.modf(6.7585)) # modf() of negative number print(" The modf of -6.7585 is:", math.modf(-6.7585))
Output:-
The modf of 6.7585 is: (0.7584999999999997, 6.0) The modf of -6.7585 is: (-0.7584999999999997, -6.0)
- remainder(x,y)
It returns x-n*y where n*y is the closest integer multiple of y.
Ex: remainder(17, 5) returns 2 because 17-3*5 (i.e., 15 is the closest multiple of 5 to 17)
remainder(11,3) returns -1 because 11-4*3 (i.e., 12 is the closest multiple of 3 to 11)import math # use of remainder() print(" The remainder value of 17 is:", math.remainder(17,5)) print(" The remainder value of 11 is:", math.remainder(11,3))
Output:-
The remainder value of 17 is: 2.0 The remainder value of 11 is: -1.0
Go and check other tutorials on python:
Leave a Reply