Mathematical Functions in Python

In this blog post, we will explore the different functions available this math module, their usage, and some examples to help you understand...
Mathematical Functions in Python
I made this using, canva.com

In Python, math function provides a various of prebuilt functions that make it easier to perform various calculations. These are very useful for developers who need to work on numbers and perform complex mathematical operations in their code.

In this blog post, we will explore the different functions available this math module, their usage, and some examples to help you understand how to use them effictively.

Number-theoretic & Reperesentation functions

The math module, provides various variety of functions for working with numbers and their representations. Here are some of the most commonly used functions in this category:

    1.    math.ceil(x): Returns the smallest integer greater than or equal to x.

    2.    math.floor(x): Returns the largest integer less than or equal to x.

    3.    math.trunc(x): Returns x with the fractional part removed, rounding towards 0.

    4.    math.fabs(x): Returns the absolute value of x.

    5.    math.factorial(n): Returns n factorial as an integer.

    6.    math.copysign(x, y): Returns a number with the magnitude of x and the sign of y.

Example:

import math

# Example usage of number-theoretic functions
num = 5.7
print(math.ceil(num))  # Output: 6
print(math.floor(num))  # Output: 5
print(math.trunc(num))  # Output: 5
print(math.fabs(-5))     # Output: 5
print(math.factorial(5)) # Output: 120

# Example usage of copysign function
x = -1.2
y = 3.4
print(math.copysign(x, y))  # Output: -1.2

Combinatorics functions

The math module also includes functions for combinatorics calculations, such as calculating combinations and permutations.

    7.    math.comb(n, k): Returns the number of ways to choose k items from n items without repetition and without order.

    8.    math.perm(n, k=None): Returns the number of ways to choose k items from n items without repetition but with order.

Example:

import math

# Example usage of combinatorics functions
n = 5
k = 2

print(math.comb(n, k))  # Output: 10
print(math.perm(n, k))   # Output: 20
print(math.perm(n))      # Output: 120

Summation and product functions

The math module provides functions for calculating sums and products of elements in iterables.

    9.     math.fsum(iterable): Returns an accurate floating-point sum of values in the iterable.

    10.   math.prod(iterable, start=1): Calculates the product of all elements in the iterable, starting with the optional start value.

    11.   math.sumprod(p, q): Returns the sum of products of values from two iterables p and q.

Example:

import math

# Example usage of summation and product functions

numbers = [1.1, 2.2, 3.3, 4.4, 5.5]

print(math.fsum(numbers))  # Output: 16.5
print(math.prod(numbers))  # Output: 1452.0

p = [1, 2, 3]
q = [4, 5, 6]

print(math.sumprod(p, q))  # Output: 32

Least Common Multiple and Greatest Common Divisor

The math module includes functions to calculate the least common multiple (LCM) and greatest common divisor (GCD) of integers.

    12.   math.gcd(*integers): Returns the greatest common divisor of the specified integer arguments.

    13.   math.lcm(*integers): Returns the least common multiple of the specified integer arguments.

Example:

import math

# Example usage of GCD and LCM functions

a = 24
b = 36

print(math.gcd(a, b))  # Output: 12

print(math.lcm(a, b))  # Output: 72

Floating-point functions

The math module provides functions for working with floating-point numbers, including functions for rounding, remainder calculation, and floating-point comparison.

    14.   math.fmod(x, y): Returns the remainder of x divided by y, using the platform C library's fmod function.

    15.   math.frexp(x): Returns the mantissa and exponent of x as a pair (m, e).

    16.   math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0): Returns True if a and b are close in value within the given tolerances.

    17.   math.isfinite(x): Returns True if x is neither an infinity nor a NaN.

    18.   math.isinf(x): Returns True if x is a positive or negative infinity.

    19.   math.isnan(x): Returns True if x is a NaN (not a number).

    20.    math.nextafter(x, y, steps=1): Returns the floating-point value steps steps after x towards y.

    21.    math.remainder(x, y): Returns the IEEE 754-style remainder of x with respect to y.

    22.    math.ulp(x): Returns the value of the least significant bit of the float x.

Example:

import math

# Example usage of floating-point functions
x = 5.7
y = 3.2

print(math.fmod(x, y))       # Output: 2.5
mantissa, exponent = math.frexp(x)
print(mantissa, exponent)  # Output: 0.95 3
print(math.isclose(x, y))   # Output: False
print(math.isfinite(x))     # Output: True
print(math.isinf(x))        # Output: False
print(math.isnan(x))        # Output: False
print(math.nextafter(x, y)) # Output: 5.699999999999999
print(math.remainder(x, y)) # Output: 2.5
print(math.ulp(x))          # Output: 5e-08

To conclude

The math module in Python provides a comprehensive set of mathematical functions that can be used to perform a wide range of calculations. 

From number-theoretic functions to floating-point operations, the module offers a convenient way to work with numbers and perform complex computations in Python code. 

By understanding the functions available in the math module and their usage, developers can easily use the power of mathematics in their Python applications efficiently.

I post, u read. Read more.

Post a Comment

Don't spam links or promote stuff in the comments. It's annoying and lowers the conversation quality. Contribute respectfully and helpfully instead.