Python Math Module

Python Math Module

Python math module is defined as the most famous mathematical functions, which includes trigonometric functions, representation functions, logarithmic functions, etc. Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc.

Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793.

Euler’s number(e): It is defined as the base of the natural logarithmic, and its value is 2.718281828459045.

There are different math modules which are given below:

math.log()

This method returns the natural logarithm of a given number. It is calculated to the base e.

Example

Output:

log(fabs(x), base) is : -6.698970004336019

<

math.log10()

This method returns base 10 logarithm of the given number and called the standard logarithm.

Example

Output:

log10(x) is : 1.1139433523068367 

math.exp()

This method returns a floating-point number after raising e to the given number.

Example

Output:

The given number (x) is : 0.05
e^x (using exp() function) is : 0.05127109637602412

math.pow(x,y)

This method returns the power of the x corresponding to the value of y. If value of x is negative or y is not integer value than it raises a ValueError.

Example

Output:

The power of number: 100.0

math.floor(x)

This method returns the floor value of the x. It returns the less than or equal value to x.

Example:

Output:

The floor value is: 10

math.ceil(x)

This method returns the ceil value of the x. It returns the greater than or equal value to x.

Output:

The floor value is: 11

math.fabs(x)

This method returns the absolute value of x.

Output:

The absolute value is: 10.001

math.factorial()

This method returns the factorial of the given number x. If x is not integral, it raises a ValueError.

Example

Output:

The factorial of number: 5040

math.modf(x)

This method returns the fractional and integer parts of x. It carries the sign of x is float.

Example

Output:

The modf of number: (0.5, 44.0)

Python provides the several math modules which can perform the complex task in single-line of code. In this tutorial, we have discussed a few important math modules.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263673.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论