Chapter 2 Using Python as a Calculator

2.9 More Mathematical Functions Using the math and cmath Modules

Several mathematical functions have been introduced so far, which include addition, subtraction, multiplication, division, modulo and power. There are more mathematical functions that a typical calculator can do, such as factorial, logarithm and trigonometric functions. These functions are supported by Python through a special module called math.

A Python module is a set of Python code that typically includes the definition of specific variables and functions. In order to use the variables and functions defined in a Python module, the module must be imported. For example,

In the example above, the math module is imported by the statement import math. The import statement is the third type of statement introduced here, with the previous two being the assignment statement and the del statement. The import statement loads the content of a specific module, and adds the name of the module in the binding table, so that content of the module can be accessed by using the name, followed by a dot (.).

Two mathematical constants pi and e, are defined in the math module, and accessed by using ‘math.’ in the above example. Both pi (π) and e are defined as floating point numbers, up to the precision supported by a computer memory limit.

Mathematical functions can be accessed in the same way as constants, by using ‘math.’. For example, the factorial function returns the factorial of the input argument.

The math module provides several classes of functions, including power and logarithmic functions, trigonometric functions and hyperbolic functions. The two basic power and logarithmic functions are math.pow(x, y) and math.log(x, y), which take two floating point arguments x, and y, and return their results.

Note the rounding off error in the last example. The functions math.pow and math.log always return floating point numbers. The function math.log can also take one argument only, in which case it returns the natural logarithm of the input argument.

There is also a handy function to calculate the base-10 logarithm of an input floating point number: math.log10(x). The function take a single floating point argument. As two other special power functions, math.exp(x) can be used to calculate the value of ex , and math.sqrt(x) can be used to calculate the square root of x.

The set of trigonometric functions that the math module provide include math.sin (x), math.cos(x), math.tan(x), math.asin(x), math.acos(x) and math.atan(x), which calculate the sine, the cosine, the tangent, the arc sine, the arc cosine, the arc tangent of x, respectively.

Note the rounding off errors in some of the examples above. All angles in the functions above are represented by radians. The math module provides two functions to convert between radians and degrees: the math.degrees function takes a single argument x , and converts x from radians to degrees; the math.radians function takes a single argument x, and converts x from degrees to radians.

The set of hyperbolic functions include math.sinh(x), math.cosh(x), math.tanh(x), math.asinh(x), math.acosh(x) and math.atanh(x), which calculate the hyperbolic sine, the hyperbolic cosine, the hyperbolic tangent, the inverse hyperbolic sine, the inverse hyperbolic cosine, and the inverse hyperbolic tangent of x, respectively.

There are more functions that the math module provides, including math.ceil(x), which returns the smallest integer that is greater than or equal to x, and math.floor(x), which returns the largest integer that is less than or equal to x . It does not make sense to remember all the functions that Python provides for the purpose of programming, although remembering a few commonly-used functions would be useful for the efficiency of programming. A good practice is to keep the Python documentation at hand, which is also easily accessible online. For example, searching for the key words ‘Python math’ using a search engine can lead to the Python documentation for the math module.

Check your understanding

© Copyright 2024 GS Ng.

Next Section - 2.10 Randoms Numbers