PythonMath模块

PythonMath模块 首页 / Python3入门教程 / PythonMath模块

Python数学模块被定义为最著名的数学函数,其中包括三角函数,表示函数,对数函数等。此外,它还定义了两个数学常数,即Pie和Euler数等。

Pie(n)     -  它是众所周知的数学常数,定义为环境与圆直径的比率。其值为3.141592653589793。

Euler(e)-  它是自然对数的底数,其值为2.718281828459045。

以下是不同的数学模块:

math.log()

此方法返回给定数字的自然对数。它以e为底进行计算。

import math  
number = 2e-7  # small value of of x  
print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))  

输出:

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

math.log10()

此方法返回给定数字的以10为底的对数,称为标准对数。

import math  
x=13  # small value of of x  
print('log10(x) is :', math.log10(x))  

输出:

log10(x) is : 1.1139433523068367 

math.exp()

将e提高到给定数字后,此方法将返回浮点数。

import math  
number = 5e-2  # small value of of x  
print('The given number (x) is :', number)  
print('e^x (using exp() function) is :', math.exp(number)-1)  

输出:

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

math.pow(x,y)

此方法返回与y值相对应的x的幂。如果x的值为负或y不是整数,则它将引发ValueError。

import math
number = math.pow(10,2)
print("The power of number:",number)

输出:

The power of number: 100.0

math.floor(x)

此方法返回x的下限值。它将小于或等于的值返回给x。

import math
number = math.floor(10.25201)
print("The floor value is:",number)

输出:

The floor value is: 10

math.ceil(x)

此方法返回x的ceil值。它将大于或等于x的值返回。

import math
number = math.ceil(10.25201)
print("The floor value is:",number)

输出:

The floor value is: 11

math.fabs(x)

此方法返回x的绝对值。

import math
number = math.fabs(10.001)
print("The floor absolute is:",number)

输出:

The absolute value is: 10.001

math.factorial()

此方法返回给定数字x的阶乘。如果x不为整数,则会引发 ValueError

import math
number = math.factorial(7)
print("The factorial of number:",number)

输出:

The factorial of number: 5040

math.modf(x)

此方法返回x的小数和整数部分。它带有x的符号为浮点型。

import math
number = math.modf(44.5)
print("The modf of number:",number)

输出:

The modf of number: (0.5, 44.0)

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

B端产品经理入门课 -〔董小圣〕

AI大模型之美 -〔徐文浩〕

Spark核心原理与实战 -〔王磊〕

正则表达式入门课 -〔涂伟忠〕

软件设计之美 -〔郑晔〕

分布式技术原理与算法解析 -〔聂鹏程〕

Java性能调优实战 -〔刘超〕

深入浅出计算机组成原理 -〔徐文浩〕

算法面试通关40讲 -〔覃超〕

好记忆不如烂笔头。留下您的足迹吧 :)