Functions in Python math Module

The Python math module provides various mathematical functions and constants. To use these functions, the module must be imported first using import math. Once imported, we can access its functions and constants using dot notation.

Here are some of the key functions available in the math module:

Number-Theoretic and Representation Functions:

ceil(x):

The ceil(x) function in Python returns the smallest integer greater than or equal to x. It is part of the math module and is used for rounding numbers upwards.

Example – 1:

import math
print(math.ceil(3.2))
print(math.ceil(5.9)) 

Output:

4

6

Example – 2:

import math

num = math.ceil(2.6)

print(num)

Output:

3

floor(x):

The floor(x) function in Python returns the largest integer less than or equal to x. It effectively rounds a number down to the nearest integer. 

Example – 1:

import math
print(math.floor(3.2))
print(math.floor(5.9))

Output:

3

5

Example-2:

import math

num = math.floor(2.6)

print(num)

Output:

2

fabs(x):

The fabs(x) function in Python returns the absolute value of a number x.

Example:

import math

number = -2.45

# calculating absolute value of number

absolute_value = math.fabs(number)

print(“Absolute value of”,number,”is”,absolute_value)

Output:

Absolute value of -2.45 is 2.45

factorial(x):

The factorial(x) function in Python calculates the factorial of a given number x.

Example – 1:

import math

print(math.factorial(4))

print(f”Factorial of 6: {math.factorial(6)}”)

Output:

24

Factorial of 5: 720

Example-2:

import math

num = math.factorial(5)

print(num)

Output:

120

fmod(x, y):

Returns the remainder of ‘x’ divided by ‘y’

Parameters:

x: The dividend (numerator).

y: The divisor (denominator).

Return value: 

A float representing the remainder.

Error handling:

If x or y are not valid numbers, it raises a TypeError.

If y (the divisor) is zero, it raises a ValueError (since division by zero is undefined).

Example:

import math

# Positive numbers

print(math.fmod(10, 3))   # Output: 1.0

print(math.fmod(10.5, 3.0)) # Output: 1.5

# Negative dividend

print(math.fmod(-10, 3))  # Output: -1.0

print(math.fmod(-10.5, 3.0)) # Output: -1.5

# Negative divisor (sign of result still matches dividend)

print(math.fmod(10, -3))  # Output: 1.0

print(math.fmod(-10, -3)) # Output: -1.0

modf(x):

Returns the fractional and integer parts of a number x as a two-item tuple. Both parts retain the sign of x and are represented as floats.

How it works:

Input: A single numeric expression x.

Output: A tuple (fractional_part, integer_part).

The fractional_part is the decimal portion of x.

The integer_part is the whole number portion of x.

Sign Convention: Both the fractional and integer parts will carry the same sign as the original input x.

Data Type: Even if x is an integer, both parts in the returned tuple will be floats.

Example:

import math

# Positive number

print(math.modf(10.72))

# Expected output: (0.7200000000000002, 10.0)

# Negative number

print(math.modf(-5.3))

# Expected output: (-0.3000000000000007, -5.0)

# Integer

print(math.modf(7))

# Expected output: (0.0, 7.0)

gcd():

The gcd() function in Python’s math module is used to calculate the Greatest Common Divisor (GCD) of two or more integers.

Example:

import math

print(math.gcd(12, 3))

print(math.lcm(12, 3,8,20))

Output:

3

1

lcm():

The lcm() function in Python’s math module is used to calculate the least common multiple (LCM) of two or more integers.

Example:

import math

print(math.lcm(12, 3))

print(math.lcm(12, 3,8,20))

Output:

12

120

trunc(x):

The trunc(x) function in Python is part of the math module and is used to truncate a floating-point number to its integer part. This means it removes the fractional part of the number without any rounding. The result is always an integer towards zero.

Example:

import math

print(math.trunc(3.14))   

print(math.trunc(3.99))   

print(math.trunc(-3.14))  

print(math.trunc(-3.99))  

print(math.trunc(5))   

Output:

3

3

-3

-3

5

Power and Logarithmic Functions:

sqrt(x):

To calculate the square root of a number x in Python, you use the sqrt() function from the math module.

Example -1:

import math

print(math.sqrt(9))    

Output:

3.0

Example -2:

import math

x = 16
square_root = math.sqrt(x)
print(square_root)

Output:

4.0

exp(x):

The exp(x) function in Python returns the value of e raised to the power of x, where e is Euler’s number, the base of the natural logarithm (approximately 2.71828). 

Example:

import math

# Calculate e raised to the power of 3

result1 = math.exp(3)

print(f”e^3 = {result1}”)

# Calculate e raised to the power of -1.5

result2 = math.exp(-1.5)

print(f”e^(-1.5) = {result2}”)

Output:

e^3 = 20.085536923187668

e^(-1.5) = 0.22313016014842982

log(x, base):

To calculate the logarithm of a number x to a specified base in Python, use the math.log() function. This function is part of the math module, so it must be imported before use.

Example:

import math

print(math.log(100, 10))

Output:

2.0

log10(x):

To calculate the base-10 logarithm of a number x in Python, the log10() function from the math module is used. This function takes a single argument, x. It returns the base-10 logarithm of x.

  • The argument, x must be a positive number.  If x is zero or a negative number, a ValueError will raised.
  • If is not a number, it will raise a TypeError.

Example – 1:

import math
x = 5
result = math.log10(x)
print(result)

Output:

0.6989700043360189

Example-2:

import math
x = 123.45
result = math.log10(x)
print(result)

Output:

2.091491094267951

Example -3:


x = 100
result = math.log10(x)
print(f”The base-10 logarithm of {x} is :{result}”)

Output:

The base-10 logarithm of 100 is :2.0

log2(x):

To calculate the base-2 logarithm of a number in Python, we can use the math.log2() function from the built-in math module. This function takes a single argument x (the number for which we want to calculate the logarithm) and returns its base-2 logarithm as a floating-point number.

  • The argument, x must be a positive number.  Passing a negative number or zero will raise a ValueError.
  • If x is not a number, it will raise a TypeError.

Example -1:

import math

x = 8
result = math.log2(x)
print(f”The base-2 logarithm of {x} is: {result}”)

Output:

The base-2 logarithm of 8 is: 3.0

Example -2:

import math
x = 15.5
result = math.log2(x)
print(f”The base-2 logarithm of {x} is: {result}”)

Output:

The base-2 logarithm of 15.5 is: 3.9541963103868754

Trigonometric Functions:

sin(x):

To calculate the sine of a number x in Python, you can use the math.sin() function from the built-in math module. The input x should be an angle measured in radians. 

Example:

import math

angle_radians = math.radians(90)
sine_value = math.sin(angle_radians)
print(sine_value)

Output:

1.0

Example-2:

import math

#Sine of 90 degrees (converted to radians)
print(f”Sine of 90 degrees: {math.sin(math.radians(90))}”)

Output:

Sine of 90 degrees: 1.0

cos(x):

Returns the cosine of x (where x is in radians).

tan(x):

Returns the tangent of x (where x is in radians).

Angular Conversion Functions:

degrees(x):

Converts angle x from radians to degrees.

radians(x):

Converts angle x from degrees to radians.

Hyperbolic Functions:

math.sinh(x): Returns the hyperbolic sine of x.

math.cosh(x): Returns the hyperbolic cosine of x.

math.tanh(x): Returns the hyperbolic tangent of x.

Inverse hyperbolic functions: math.asinh(), math.acosh(), math.atanh().

Constants:

math.pi:

The number 𝜋 is a mathematical constant, approximately equal to 3.14159. After importing the math module, you just need to write math.pi to access the 𝜋 number.

Example:

import math

# Value of pi

print(math.pi)

print(f”Value of pi: {math.pi}”)

Output:

3.141592653589793

Value of pi: 3.141592653589793

math.e: The mathematical constant e (Euler’s number).

math.tau: The mathematical constant tau (τ).

math.inf (Infinity):

In Python, math.inf represents positive infinity. It is a constant available within the math module.

Example:

import math

print(math.inf)

Output:

inf

math.nan (Not a number):

In Python, math.nan is a constant available within the math module that represents “Not a Number” (NaN). 

Example:

import math

print(math.nan)

Output:

nan

Leave a Comment