Math module

The math module provides mathematical functions defined by c standard. The most commonly used functions are explained below.

ceil

Syntax:

ceil(x)

Description:

It returns the smallest integer greater than or equal to x.

Example: 1

math.ceil(5.3)

Output

6

 

Example: 2

math.ceil(-5.3)

Output

-5

copysign

Syntax:

Copysign(x,y)

Description

It returns x with the sign of y

Example: 1

x = 2

y = 2

 

math.copysign(x, -y)

Output

-2.0

Example: 2

x = 2

y = -2

math.copysign(x, -y)

Output

2.0

​​​​​​​fabs

Syntax:

fabs(x)

                              

Description

It returns the absolute value of x

Example: 1

math.fabs(2.0)

Output

2.0

Example: 2

math.fabs(-2.0)

Output

2.0

 

factorial

Syntax:

factorial(x)

Description

 It returns the factorial of x

Example: 1

math.factorial(5)

Output

120

Example: 2

math.factorial(-5)

Output

ValueError: factorial() not defined for negative values

floor

Syntax:

floor(x)

Description

It returns the largest integer less than or equal to x

Example: 1

math.floor(10.2)

Output

10

Example: 2

math.floor(-10.2)

Output

- 11

 

fmod

Syntax:

fmod(x)

Description

It returns the remainder when x is divided by y

Example: 1

math.fmod(5, 2)

Output

1

Example: 2

math.fmod(5, -2)

Output

1

                               

frexp(x)         

Syntax:

frexp(x)

Description

It returns the mantissa and exponent of x as the pair (m, e)

Example: 1

math.frexp(5)

 

Output

(0.625, 3)

 

fsum       

Syntax:

fsum(x)

Description

It returns floating point sum of values in an iterable

 Example: 1

math.fsum([1.21, 3.14, 5.98])

Output

10.33

                                               

Example: 2

math.fsum((1.21, -7.67565,3.14, 5.98))

Output

2.6543500000000004

isfinite           

Syntax:

isfinite(x)

Description

It returns True if x is neither an infinity nor a NaN (Not a Number)

Example: 1

math.isfinite(123456789123456789)

Output

True

Example: 2

math.isfinite(float("nan"))

Output

True

 

isnan                

Syntax:

isnan(x)

Description

It returns True if x is a NaN

Example: 1

math.isnan( float('nan'))

Output

True

Example: 2

math.isnan(5.0)

Output

False

 

modf              

Syntax:

modf(x)

Description

It returns the fractional and integer parts of x

Example: 1

math.modf(2)

Output

(0.0, 2.0)

 

Example: 2

math.modf(5.5)

Output

(0.5, 5.0)

trunc     

Syntax:

trunc(x)

Description

It returns the truncated integer value of x

Example: 1

 math.trunc(10.5)

Output

10

 

Example: 2

math.trunc(-10.5)

Output

-10

exp                 

Syntax:

exp(x)

Description

It returns e**x

Example: 1

math.exp(2)

Output

7.38905609893065

Example: 2

math.exp(-2)

Output

0.1353352832366127

log

Syntax:

log(x,[base])

Description

It returns the logarithm of x to the base (default value of  base is e)

Example: 1

math.log(1024,2)

Output

10.0

Example: 2

math.log(1000,10)

Output

2.9999999999999996

 

log10(x)         

Syntax:

log10(x)

                             

Description

It returns the base-10 logarithm of x

Example: 1

math.log10(1000)

Output

3.0

 

Example: 2

math.log10(90)

Output

1.954242509439325

 

pow(x, y)                

Syntax:

pow(x,y)

Description

It returns x raised to the power y

Example: 1

math.pow(2,5)

Output

32.0

sqrt                

Syntax:

sqrt(x)

​​​​​​​Description

It returns the square root of x

Example: 1

math.sqrt(25)

Output

5.0

Example: 2

math.sqrt(-5)

Output

ValueError: math domain error

 

Other Math Module functions

ldexp(x, i)

It returns x * (2**i)

expm1(x)

It returns e**x - 1

log1p(x)

It returns the natural logarithm of 1+x

log2(x)

It returns the base-2 logarithm of x

acos(x)

It returns the arc cosine of x.

asin(x)

It returns the arc sine of x.

atan(x)

It returns the arc tangent of x.

atan2(y, x)

It returns atan(y / x).

cos(x)

It returns the cosine of x.

hypot(x, y)

It returns the Euclidean norm, sqrt(x*x + y*y).

sin(x)

It returns the sine of x.

tan(x)

It returns the tangent of x.

degrees(x)

Converts angle x from radians to degrees.

radians(x)

Converts angle x from degrees to radians.

acosh(x)

It returns the inverse hyperbolic cosine of x.

asinh(x)

It returns the inverse hyperbolic sine of x.

atanh(x)

It returns the inverse hyperbolic tangent of x.

cosh(x)

It returns the hyperbolic cosine of x.

sinh(x)

It returns the hyperbolic cosine of x.

tanh(x)

It returns the hyperbolic tangent of x.

erf(x)

It returns the error function at x.

erfc(x)

It returns the complementary error function at x.

gamma(x)

It returns the Gamma function at x.

lgamma

It returns the natural logarithm of the absolute value of the Gamma.

 

Constant in Math Module

 

pi

Mathematical constant  (3.14159...)

 

e

Mathematical constant e (2.71828...)