Python中的数学常量
在本文中,我们将学习 Python 的数学常量以及如何使用它们。
math 模块包含一些定义的常量,可用于各种数学运算。这些数学常量返回的值与其标准定义值相同。
以下是在 Python 编程语言中可用的 math 模块常量
Python math.e 常量
Python math.pi 常量
Python math.tau 常量
Python math.inf 常量
Python math.nan 常量
Python math.e 常量
math.e 常量返回欧拉数,即 **2.71828182846**。
语法
math.e
**返回值** - 它返回浮点数 2.71828182846,表示数学常数 e。
示例
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字导入 **math** 模块。
使用 math 模块的 e 常量和 (.) 运算符打印欧拉值 **e**。
示例
以下程序使用 Python 中的 math 模块返回 math.e 常量的值:
# importing math module import math # printing the Euler value e print ("The Euler value e = ", math.e)
输出
执行上述程序后,将生成以下输出:
The Euler value e = 2.718281828459045
Python math.pi 常量
math.pi 常量返回 pi 值,即 3.14159265359。它被定义为圆的周长与其直径的比率。
语法
math.pi
**返回值** - 它返回浮点数 **3.14159265359**,表示数学常数 PI。
示例
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字导入 **math** 模块。
使用 math 模块的 pi 常量和 (.) 运算符打印 **pi** 值。
示例
以下程序使用 Python 中的 math 模块返回 math.pi 常量的值:
# importing math module import math # printing the pi value print ("The pi value = ", math.pi)
输出
执行上述程序后,将生成以下输出:
The pi value = 3.141592653589793
Python math.tau 常量
math.tau 常量返回 **tau** 值 **6.283185307179586**。它被定义为圆的周长与其半径的比率。
语法
math.tau
**返回值** - 它返回浮点数 6.283185307179586,表示数学常数 **tau**。
示例
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字导入 **math** 模块。
使用 math 模块的 **tau** 常量和 (.) 运算符打印 **tau** 值。
示例
以下程序使用 Python 中的 math 模块返回 math. tau 常量的值:
# importing math module import math # printing the tau value print ("The tau value = ", math.tau)
输出
执行上述程序后,将生成以下输出:
The tau value = 6.283185307179586
Python math.inf 常量
math.inf 常量返回正无穷大的值。对于负无穷大,使用 -math.inf。
inf 常量与 float("inf") 相同。
语法
math.inf
**返回值**: 它返回表示 **正无穷大** 值的浮点数。
示例
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字导入 **math** 模块。
使用 math 模块的 inf 常量和 (.) 运算符打印正无穷大的值。
使用 math 模块的 inf 常量和 (.) 运算符打印负无穷大的值,并应用 **-(负号)**。
示例
以下程序使用 Python 中的 math 模块返回 math.inf 常量的值:
# importing math module import math # printing the value of positive infinity print ("The value of positive infinity = ", math.inf) # printing the value of negative infinity print ("The value of negative infinity = ", -math.inf)
输出
执行上述程序后,将生成以下输出:
The value of positive infinity = inf The value of negative infinity = -inf
Python math.nan 常量
math.nan 常量返回浮点 **nan**(非数字)值。这不是一个有效的数字。float("nan") 与 nan 常量类似。
语法
math.nan
**返回值**: 它返回浮点数 **nan**(非数字)。
示例
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字导入 **math** 模块。
使用 math 模块的 nan 常量和 (.) 运算符打印 nan 值。
示例
以下程序使用 Python 中的 math 模块返回 math.nan 常量的值:
# importing math module import math # printing the nan value print ("The nan value = ", math.nan)
输出
执行上述程序后,将生成以下输出:
The nan value = nan
结论
在本文中,我们学习了 Python 中可用的数学常量。我们还通过一些示例演示了如何在 Python 程序中使用这些常量。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP