AppDividend
Latest Code Tutorials

Python Math Functions Example | Math Functions In Python

0

Python math is an inbuilt standard module that used to work with complex scientific calculations. If we want to use mathematical functions like evaluate some complex mathematical operations such, trigonometric operations, logarithmic operations, etc. under the math module then use the Python math functions.

Most Used Python Math Functions

  1. fabs(x): It returns the absolute value of x.
  2. ceil():- It returns the smallest integer value greater than or equal to x.
  3. floor():- It returns the largest integer less than or equal to x.
  4. factorial():- It returns the factorial of x.
  5. gcd():- This function is used to compute the greatest common divisor of 2 numbers mentioned in its arguments. This function works in python 3.5 and above.

Python Math Functions Example

To use the Math module in your Python application, first, you need to import the module using import math statements in your program. After that, you can call any method of that module. In python, some mathematical operations can be performed with ease of the math module.

The math module does not support complex data types. For that, you can use the cmath module is the complex counterpart.

Let’s take some examples of the Math module.

# app.py

import math

data = 21.6
print('The floor of 21.6 is:', math.floor(data))

See the output.

Python Math Functions Tutorial With Example

The floor() function returns the highest integer value smaller than the number. If the number is already the integer, then the same number is returned.

Let’s see the value of PI.

# app.py

import math

print('The value of PI:',math.pi)

Okay, let’s see the output below.

Python Math Functions Tutorial

#Python Power Function

Let’s use the Power function of the Math module. Python offers to compute the power of a number and hence can make the task of calculating the power of a number easier.  See the following syntax of Power.

pow(x,y,z)

The pow() method returns the value of x to the power of y (xy).

xA number, the base
yA number, the exponent
zOptional. A number, the modulus

 

See the example below.

# app.py

import math

print(math.pow(2, 3))

The output is the following.

Python Power Function

#Python ceil() function

The ceil() function returns the smallest integer value greater than the number. If the number is already an integer, then the same number is returned. 

The method ceil() returns ceiling value of x – the smallest integer which is not less than x.

# app.py

import math

data = 21.6
print(math.ceil(21.6))

See the output below.

Python ceil() function

Let’s see more examples.

# app.py

import math

number = -21.19

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))

The output is the following.

Python Number ceil() Method

#Python Math exp() and log() method

Let’s see an example of Python Math exp() and log() function.

Math module exp() method is used to get e^x

The Python log() function is used to get the log value. See the example below.

# app.py

import math

number = 1e-4
print('The given number (x) is :', number)
print('e^x (using exp() function) is :', math.exp(number)-1)
print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))

See the output below.

Python Math exp() and log() method

#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.

Als,o you can convert angles from degree to radian and radian to degree. See the example code.

# app.py

import math

angleInDegree = 90
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))

See the output below.

Python Math Trigonometric Functions

#All the Python Math Functions Table

See the below table, which has every Python Math module’s function with description.

FunctionDescription
ceil(x)Returns the smallest integer value greater than or equal to x.
copysign(x, y)Returns x with a sign of y
fabs(x)Returns the absolute value of x
factorial(x)Returns the factorial of x
floor(x)Returns the largest integer less than or equal to x
fmod(x, y)Returns a remainder when x is divided by y
frexp(x)Returns a mantissa and exponent of x as the pair (m, e)
fsum(iterable)Returns the accurate floating-point sum of values in the iterable
isfinite(x)Returns True if x is neither infinity nor a NaN (Not a Number)
isinf(x)Returns True if x is the positive or negative infinity
isnan(x)Returns True if x is the NaN
ldexp(x, i)Returns x * (2**i)
modf(x)Returns a fractional and integer parts of x
trunc(x)Returns a truncated integer value of x
exp(x)Returns e**x
expm1(x)Returns e**x – 1
log(x[, base])Returns the logarithm of x to the base (defaults to e)
log1p(x)Returns the natural logarithm of 1+x
log2(x)Returns the base-2 logarithm of x
log10(x)Returns the base-10 logarithm of x
pow(x, y)Returns x raised to the power y
sqrt(x)Returns the square root of x
acos(x)Returns the arc cosine of x
asin(x)Returns the arc sine of x
atan(x)Returns the arc tangent of x
atan2(y, x)Returns atan(y / x)
cos(x)Returns the cosine of x
hypot(x, y)Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x)Returns the sine of x
tan(x)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)Returns the inverse hyperbolic cosine of x
asinh(x)Returns the inverse hyperbolic sine of x
atanh(x)Returns the inverse hyperbolic tangent of x
cosh(x)Returns the hyperbolic cosine of x
sinh(x)Returns the hyperbolic cosine of x
tanh(x)Returns the hyperbolic tangent of x
erf(x)Returns the error function at x
erfc(x)Returns the complementary error function at x
gamma(x)Returns the Gamma function at x
lgamma(x)Returns the natural logarithm of the absolute value of the Gamma function at x
piMathematical constant, the ratio of the circumference of a circle to its diameter (3.14159…)
emathematical constant e (2.71828…)

 

Finally, Python Math Functions Example Tutorial is over.

Recommended Posts

Python Sqrt Example

Python abs() Example

Python eval() Example

Python complex() Example

Python pow() Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.