In this tutorial, we will learn about Python Math module and its functions. In the previous tutorial, we learned about Python Matrix.

Table of Contents

## Python Math

Python Math module provides access to the mathematical functions defined by the C standard. So, we can do many complex mathematical operations with the help of the Python Math functions. The tutorial is designed with some basic functions and examples of math module. Let’s get started.

### Python Math Functions – floor(), ceil(), fabs(x)

Python math module is part of the python installation, so we can just import it in our python program and use it.

In this section, we will discuss about these three math module functions. The `floor()`

function is used to get the floor value to the given number. Similarly `ceil()`

function is used to get the ceiling value of a given number. So, these two functions are used to round the value, either floor value or ceiling value.

`fabs()`

function is used to get the absolute value of the given number. See the example code below.

```
import math
number = -2.34
print('The given number is :', number)
print('Floor value is :', math.floor(number))
print('Ceiling value is :', math.ceil(number))
print('Absolute value is :', math.fabs(number))
```

And the output will be

```
The given number is : -2.34
Floor value is : -3
Ceiling value is : -2
Absolute value is : 2.34
```

### Python Math exp(), expm1() and log()

Math module `exp()`

function is used to get **e^x**.

`expm1()`

function returns **(e^x)-1**. For small value of x, direct calculation of `exp(x)-1`

may results in significant loss in precision while the `expm1(x)`

can produce output in full precision.

The `log()`

function is used to get the log value. See the example code.

```
import math
number = 1e-4 # small value of of x
print('The given number (x) is :', number)
print('e^x (using exp() function) is :', math.exp(number)-1)
print('e^x (using expml() function) is :', math.expm1(number))
print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))
```

And you will get the output like this

```
The given number (x) is : 0.0001
e^x (using exp() function) is : 0.0001000050001667141
e^x (using expml() function) is : 0.00010000500016667084
log(fabs(x), base) is : -3.999999999999999
```

### Python Math Trigonometric Functions

All the trigonometric functions are available in python math module, so you can easily calculate them using **sin()**, **cos()**, **tan()**, **acos()**, **asin()**, **atan()** etc functions.

Also you can convert angles from degree to radian and radian to degree. See the example code.

```
import math
angleInDegree = 45
angleInRadian = math.radians(angleInDegree)
print('The given angle is :', angleInRadian)
print('sin(x) is :', math.sin(angleInRadian))
print('cos(x) is :', math.cos(angleInRadian))
print('tan(x) is :', math.tan(angleInRadian))
```

So, in output you will get

### Python Math sqrt

We can use `sqrt(x)`

function to get the square root of x. Below is a simple example of python math sqrt function.

```
import math
x = 16
y = 10
z = 11.2225
print('sqrt of 16 is ', math.sqrt(x))
print('sqrt of 10 is ', math.sqrt(y))
print('sqrt of 11.2225 is ', math.sqrt(z))
```

Output produced by above math sqrt example is:

```
sqrt of 16 is 4.0
sqrt of 10 is 3.1622776601683795
sqrt of 11.2225 is 3.35
```

### Python Math PI

Python math module has “pi” as a constant that can be used in mathematical calculations such as area of a circle.

```
import math
print('PI value = ', math.pi)
radius = 4
print('Area of Circle with Radius 4 =', math.pi * (radius ** 2))
```

Above python example program will produce following output.

```
PI value = 3.141592653589793
Area of Circle with Radius 4 = 50.26548245743669
```

These are some of the basic functions from Python Math module. If you want to know about more functions, then see the official documentation.

Sorry for my comment about the above , I forgot completely about the negative sign. I got the explanation. Thanks!

Thanks for commenting back, no need to apologize.

I just checked on Wikipedia about floor and ceiling values of a number and those functions and as we logically conclude as a result of those names, it shows the functions like this :

Floor (2.4) = 2

ceiling (2.4) = 3

Reference :

https://en.wikipedia.org/wiki/Floor_and_ceiling_functions

You have written the exact opposite in the above mentioned example.