Python

Mathematical Functions in Python

In the math module of Python, you’ll find different mathematical functions.

 

For example, the trigonometric functions sin(), cos(), and tan() as well as the inverse trigonometric functions asin(), acos(), and atan(), are there among other things.

 

Let’s consider a sample program.

 

import math

 

x = 30

xbm = math.radians(x)

print(f"Sine {x} Degree: {math.sin(xbm)}")

print(f"Cosine {x} Degree: {math.cos(xbm)}")

print(f"Tangent {x} Degree: {math.tan(xbm)}")

z = 0.5

print(f"Arcsine {z} in Degrees: {math.degrees(math.asin(z))}")

z = 0.866

print(f"Arccosine {z} in Degrees: {math.degrees(math.acos(z))}")

z = 0.577

print(f"Arctangent {z} in Degrees: {math.degrees(math.atan(z))}")

 

The following output is generated:

 

Sine 30 Degree: 0.49999999999999994

Cosine 30 Degree: 0.8660254037844387

Tangent 30 Degree: 0.5773502691896257

Arcsine 0.5 in Degrees: 30.000000000000004

Arccosine 0.866 in Degrees: 30.002910931188026

Arctangent 0.577 in Degrees: 29.984946007397852

 

After importing the math module, the sine, cosine, and tangent of the 30-degree angle are calculated first. All functions expect angles in radians. For this reason, a conversion from degrees to radians takes place beforehand using the radians() function.

 

The arcsine, arccosine, and arctangent are then calculated for specific values. These functions also provide an angle in radians. This result is then converted to degrees using the degrees() function.

 

The math module contains a range of other functions and constants. Let’s consider a sample program.

 

import math

 

print("Square root of 64:", math.sqrt(64))

print("Cube root of -27:", math.cbrt(-27))

print("Integer square root of 80:", math.isqrt(80))

print()

 

print("Natural logarithm of 33:", math.log(33))

print("e to the power of 3.5:", math.exp(3.5))

print("2 to the power of 10:", math.exp2(10))

print()

 

print("Logarithm base 10 of 0.001:", math.log10(0.001))

print("10 ** -3:", 10 ** -3)

print()

 

print("Mathematical constant pi:", math.pi)

print("Euler’s number e:", math.e)

print()

 

t1 = 3, 2, 5

t2 = 2, 4, 3

print("Product:", math.prod(t1))

print("Sum of products:", math.sumprod(t1, t2))

 

print("Factorial of 5:", math.factorial(5))

print("Greatest common divisor of 60 and 135:", math.gcd(60, 135))

print("Remainder:", math.remainder(10.9, 2.5))

print("Remainder:", math.remainder(11.9, 2.5))

print()

 

if not math.isclose(3, 2.96, rel_tol=0.01):

   print("Not close")

 

if math.isclose(3, 2.97, rel_tol=0.01):

   print("Close")

 

The following output is generated:

 

Square root of 64: 8.0

Cube root of -27: -3.0

Integer square root of 80: 8

 

Natural logarithm of 33: 3.4965075614664802

e to the power of 3.5: 33.11545195869231

2 to the power of 10: 1024.0

 

Logarithm base 10 of 0.001: -3.0

10 ** -3: 0.001

 

Mathematical constant pi: 3.141592653589793

Euler's number e: 2.718281828459045

 

Product: 30

Sum of products: 29

Factorial of 5: 120

Greatest common divisor of 60 and 135: 15

Remainder: 0.9000000000000004

Remainder: -0.5999999999999996

 

Not close

Close

 

The mathematical square root of a positive number can be calculated using the sqrt() function. Since Python 3.11, the cube root (i.e., the third root) can be calculated from a number using the cbrt() function. Since Python 3.8, you can use the isqrt() function to calculate the integer square root of a number. This result is the largest integer value that is smaller than the mathematical square root of a number.

 

The log() function calculates the natural logarithm of a positive number for the base e. The exp() function determines the mathematical inverse: the value of ex. Since Python 3.11, the exp2() function can be used to determine the value of 2x.

 

The log10() function calculates the logarithm of a positive number for the base 10. After that, the mathematical inverse is calculated, namely, the value of 10x.

 

You also have the mathematical constants pi and e.

 

Since Python 3.8, the prod() function can be used to determine the product of the elements of an iterable, in this case, a tuple. The math looks as follows: 3 x 2 x 5 = 30. Since Python 3.12, you can use the sumprod() function to calculate the sum of the products of the element pairs of two iterables in the same position. The math looks as follows: 3 x 2 + 2 x 4 + 5 x 3 = 29. Learn more about tuples in Python here.

 

The value of the factorial can be mathematically calculated only from positive integers. The factorial() function is used for this purpose.

 

The gcd() function has been available since Python 3.5. This function determines the greatest common divisor (GCD) of two integers, which is the largest number by which the two numbers can be divided without a remainder.

 

Since Python 3.7, the remainder of a division can be calculated in accordance with the Institute of Electrical and Electronics Engineers (IEEE) 754 standard using the remainder() function. This value is the difference to the next integer. What does this mean? In this example, 10.9 or 11.9 is divided by 2.5. The mathematical result lies between the two integers 4 (4 x 2.5 = 10) and 5 (5 x 2.5 = 12.5). In the case of 10.9, the value 10 is closer, so the remainder() function returns the value 0.9 (= 10.9 - 10). In the case of 11.9, the value 12.5 is closer, so the remainder() function returns the value -0.6 (= 11.9 - 12.5).

 

Since Python 3.5, you can use the isclose() function to determine whether two numbers are close to each other. The proximity of two numbers is determined using a relative tolerance, in this case, 0.01. In other words, the two figures can differ by a maximum of 1%. You can use one of two named parameters. Instead of rel_tol, you can use abs_tol for a measurement with an absolute tolerance.

 

Editor’s note: This post has been adapted from a section of the book Getting Started with Python by Thomas Theis.

Recommendation

Getting Started with Python
Getting Started with Python

If you want to program with Python, you’ve come to the right place! Take your first steps with this Python crash course that teaches you to use core language elements, from variables to branches to loops. Follow expert guidance to work with data types, functions, and modules—and learn how to manage errors and exceptions along the way. Apply Python programming to develop databases, graphical user interfaces, widgets, and more. Practice your skills with example exercises, and start developing your own applications with Python today!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments