# Python Data Persistence – Math Module

## Python Data Persistence – Math Module

As you would expect, the math built-in module is one of the most frequently used. Various functions which are often required are defined in this module. Functions for computing all trigonometric ratios are present in the math module. All these functions calculate the respective ratio of an angle given in radians. Knowing that 30 degrees are roughly equal to 0.5236, various ratios are calculated as follows:

Example

 >>> import math >>> math.sin(0.5236) 0.5000010603626028 >>> math.cos(0.5236) 0.866024791582939 >>> math.tan(0.5236) 0.5773519017263813 >>>

The math module also has two functions to convert the measure of an angle in degrees to radians and vice versa.

Example

 >>> math.radians(30) 0.5235987755982988 >>> math.degrees(0.52398) 30.021842549264875 >>>

Two important mathematical constants are defined in this module. They are Pie (π) and Euler’s number (e)

Example

 >>> math.e 2.718281828459045 >>> math.pi 3.141592653589793 >>>

We have used sqrt ( ) function from math module. Likewise the module has pow ( ) , log ( ) , and exp ( ) functions.

Example

 >>> math . sqrt ( 25 ) 5.0 >>> math . pow ( 5 , 2 ) 25.0 >>> math.log(10) #natural logarithm using math.e as base 2.302585092994046 >>> math.loglO(100) ttstandard logarithm using 10 as base 2.0 >>>

The mod operator (%) and floor operator (//) were introduced earlier. This module contains similar functions ( remainder ( ) and floor ( ) ). Another ceil( ) function in this module returns the nearest integer of a division operation.

Example

 >>> math.remainder(10,6) #Difference between numerator and closest integer multiple of denominator -2.0 >>> math.floor(10/6) #Returns the largest integer < = given float 1 >>> math.ceil(10/6) #Returns the smallest integer >= float 2 >>>