# Lesson-12: Python Math Module (math library)

## Python Math Module (math library)

With this library, which is ready to be installed in the Python program, we can quickly perform mathematical operations. There are many functions and variables in it. Now let’s look at these functions in sequence.

### math.ceil():

Converts a given decimal number to a higher number. If the number is an integer, it uses the ceil function.

```>>> math.ceil(32.05)
33
>>> math.ceil(2.98)
3
```

### math.copysign()

Gives the sign of the second of the two parameters it receives to the first.

`>>> math.copysign(25,-12)-25.0>>> math.copysign(-12,-15)-12.0>>> math.copysign(-245,54)245.0`

### math.fabs()

Gets the absolute value of the given value. It has a small difference from embedded functions to abs. It returns its output as a decimal number, not an integer.

```>>> math.fabs(-28)
28.0
>>> abs(-28)
28
```

### math.factorial()

Returns the factor of the given number. If the given value is not a positive integer, ValueError returns an error.

`>>> math.factorial(5)120>>> math.factorial(-5)Traceback (most recent call last):File "<pyshell#10>", line 1, in <module>    math.factorial(-5)ValueError: factorial() not defined for negative values`

### math.floor()

it has the opposite function of the ceil function. Returns an integer one below the given decimal number. If the number is an integer, it uses the floor function. the difference from the int function occurs in negative numbers.

```>>> math.floor(25.42)
25
>>> math.floor(-12.25)
-13
>>> int(-12.25)
-12
```

### math.fmod()

The first parameter you give finds the rest from the second parameter section. the difference from the % operator occurs in negative numbers.

```>>> math.fmod(45,2)
1.0
>>> math.fmod(45,14)
3.0
>>> math.fmod(45,-14)
3.0
>>> 45%-14
-11
```

### math.frexp()

This function allows you to find the M and e parameters of the following process. the absolute value of M takes a value between 0.5 and 1.

```x = m * 2 ** e

>>> math.frexp(1)
(0.5, 1)
>>> math.frexp(8)
(0.5, 4)
```

### math.fsum()

it’s very similar to the sum function. it closes a gap in the sum function. the sum function can cause some problems when working with decimal numbers.

```>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0
```

### math.gcd()

It gives GCD (greatest common divisor) of the given two numbers.

```>>> math.gcd(45,70)
5
>>> math.gcd(24,-24)
24
>>> math.gcd(36,12)
12
```

### math.trunc()

it does the same job with the int function.

```>>> math.trunc(15.12)
15
>>> math.trunc(-15.12)
-15
>>> math.trunc(0.24)
0
>>> int(0)
0
>>> int(-15.12)
-15```

## math.e

a variable that holds the euler constant. Value: 2.718281…

## math.pi

a variable that holds the number pi. Value: 3.141592….

## math.tau

the variable that holds the tau constant. It’s worth twice the number of pi.

```>>> math.pi*2==math.tau
True
```

## math.exp()

takes the force of the euler constant. So this is what he does:math.e * * x

```>>> math.exp(2)
7.38905609893065
```

## math.expm1()

math.the only difference from the work done by the exp function is that it subtracts 1 from the result.

```>>> math.exp(12)
162754.79141900392
>>> math.expm1(12)
162753.79141900392
```

## math.log()

Calculates the logarithm of the first value relative to the second value.

```>>> math.log(10,10)
1.0
>>> math.log(25,5)
2.0
>>> math.log(5,25)
0.5
```

## math.log1p()

Calculates the logarithm of a given number relative to base e.

```>>> math.log1p(0)
0.0
>>> math.log1p(2)
1.0986122886681098
```

## math.log2()

Calculates the logarithm of the given number at Base 2.

```>>> math.log2(2)
1.0
>>> math.log2(42)
5.392317422778761
```

## math.log10()

Calculates the logarithm of the given number at base 10

```>>> math.log10(1000)
3.0
>>> math.log10(20)
1.3010299956639813
```

## math.pow()

** and does the same job as pow from embedded functions. In other words, it takes the strength of the first number relative to the second number.

```>>> math.pow(2,5)
32.0
>>> math.pow(2,0)
1.0
>>> pow(2,5)
32
>>> pow(2,0)
1
```

## math.sqrt()

Calculates the square root of the given number.

```>>> math.sqrt(16)
4.0
>>> math.sqrt(225)
15.0
```

## math.degrees()

Converts the given number from radians to degrees.

```>>> math.degrees(1.5707963267948966)
90.0
```

Converts the given number from degree to Radian.

```>>> math.radians(90)
1.5707963267948966
```

## math.sin()

Calculates the sine of a given number in radians

```>>> math.sin(math.radians(60))
0.8660254037844386
```

## math.cos()

Calculates the cosine of the given parameter in radians.

## math.tan()

`Calculates the tangent of the given parameter in radians.`

## math.asin()

`Returns an angle in the genus Radian from the given sine value.`

## math.acos()

`Returns an angle in radians from the given cosine value.`

## math.atan()

`Returns an angle in radians from the given tangent value.`

## math.atan2()

`If we say the first value y to the second Value x, it returns the result of the following Operation: atan (y / x)`

## math.hypot()

İlk değere x ikinci değere y dersek şu işlemin sonucunu döndürür: sqrt(x*x+y*y).

## math.cosh()

Returns the hyperbolic cosine of the given value.

## math.sinh()

`Returns the hyperbolic sine of the given value.`

## math.tanh()

`Returns the hyperbolic tangent of the given value.`

## math.acosh()

`Returns the inverse of the given hyperbolic cosine value.`

## math.asinh()

`Returns the inverse of the given hyperbolic sine value.`

## math.atanh()

`Returns the inverse of the given hyperbolic tangent value.`

## math.gamma()

`This function is very similar to the factorial function. One of its differences is that it calculates the factorial of a few of the given number. But the real difference comes when the number grows.`
```>>> math.factorial(12)==math.gamma(13)
True
>>> math.factorial(12)
479001600
>>> math.gamma(13)
479001600.0
>>> math.factorial(35)==math.gamma(36)
False
>>> math.factorial(35)
10333147966386144929666651337523200000000
>>> math.gamma(36)
1.0333147966386145e+40
```

## math.lgamma()

`This function combines two functions that we have learned before.`
```>>> math.lgamma(45)==math.log(math.gamma(45))
True
>>> math.log(math.gamma(45))
125.3172711493569
>>> math.lgamma(45)
125.31727114935```