非常教程

Python参考手册

数字与数学 | Numeric & Mathematical

math

该模块始终可用。它提供对由C标准定义的数学函数的访问。

这些功能不能用于复数; cmath如果您需要支持复杂数字,请使用模块中相同名称的功能。支持复数的功能和不支持的功能之间的区别是由于大多数用户不想学习理解复数所需的太多数学。接收一个异常而不是一个复杂的结果,可以更早地检测出用作参数的意外复数,这样程序员就可以确定首先产生的方式和原因。

以下功能由该模块提供。除非另外明确指出,否则所有返回值都是浮点数。

1.数论和表示函数

math.ceil(x)

返回x的上限作为float,最小的整数值大于或等于x

math.copysign(x, y)

y的符号返回x。在支持带符号的零的平台上,返回-1.0copysign(1.0, -0.0)

2.6版本中的新功能。

math.fabs(x)

返回x的绝对值。

math.factorial(x)

返回x阶乘。ValueError如果x不是整数或者是负数,则引发。

2.6版本中的新功能。

math.floor(x)

x的底部作为浮点数返回,最大的整数值小于或等于x

math.fmod(x, y)

返回fmod(x, y),由平台C库定义。请注意,Python表达式x % y可能不会返回相同的结果。C标准的意图fmod(x, y)是精确地(数学上的;以无限的精度)等于x - n*y某个整数n,使得结果的符号与x和幅度小于abs(y)。Python的x % y返回结果会带有y的符号,并且对于float参数可能不是完全可计算的。例如,fmod(-1e-100, 1e100)是的-1e-100,但Python的结果-1e-100 % 1e1001e100-1e-100,它不能完全表示为一个浮点数,并且令人惊讶1e100。出于这个原因,功能fmod()在使用浮点数时一般首选,而x % y在使用整数时首选Python 。

math.frexp(x)

返回x的尾数和指数作为一对(m, e)m是一个浮点数,e是一个整数x == m * 2**e。如果x为零,则返回(0.0, 0),否则返回0.5 <= abs(m) < 1。这用于以便携方式“分离”浮点的内部表示。

math.fsum(iterable)

在迭代器中返回一个精确的浮点和值。通过跟踪多个中间部分和来避免精度损失:

>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0

该算法的精度取决于IEEE-754算术保证和舍入模式为半均匀的典型情况。在一些非Windows版本中,底层C库使用扩展精度加法,偶尔会使中间和加倍,导致它在最低有效位中关闭。

有关进一步的讨论和两种替代方法,请参阅ASPN食谱配方以获得准确的浮点总和。

2.6版本中的新功能。

math.isinf(x)

检查float x是正值还是负值无穷大。

2.6版本中的新功能。

math.isnan(x)

检查float x是否是NaN(不是数字)。有关NaN的更多信息,请参阅IEEE 754标准。

2.6版本中的新功能。

math.ldexp(x, i)

返回x * (2**i)。这基本上是函数的反函数frexp()

math.modf(x)

返回x的小数部分和整数部分。两个结果都带有x的符号并且是浮点数。

math.trunc(x)

将截断的Realx返回Integral(通常是一个长整数)。使用该__trunc__方法。

2.6版本中的新功能。

注意,frexp()modf()具有比它们的C当量的不同调用/返回的模式:它们采取单参数和返回的一对值,而不是通过“输出参数”返回其第二返回值(有在Python没有这样的事情)。

对于ceil()floor()modf()功能,请注意,所有的足够大的幅度的浮点数字是准确的整数。Python浮点运算的精度通常不超过53位(与平台C的双精度类型相同),在这种情况下,任何有必要的浮点数xabs(x) >= 2**52都没有小数位。

2.功率和对数函数

math.exp(x)

返回e**x

math.expm1(x)

返回e**x - 1。对于小浮点数x,减法exp(x) - 1可导致精度的显着损失; 该expm1()函数提供了一种以完全精确的方式计算此数量的方法:

>>> from math import exp, expm1
>>> exp(1e-5) - 1  # gives result accurate to 11 places
1.0000050000069649e-05
>>> expm1(1e-5)    # result accurate to full precision
1.0000050000166668e-05

2.7版本的新功能。

math.log(x[, base])

用一个参数,返回x的自然对数(以e为底)。

用两个参数,将x的对数返回给定的基数,计算为log(x)/log(base)

在版本2.3中更改:添加了基本参数。

math.log1p(x)

返回1 + x的自然对数(基数e)。计算结果的方式对于接近零的x是准确的。

2.6版本中的新功能。

math.log10(x)

返回x的基数为10的对数。这通常比......更准确log(x, 10)

math.pow(x, y)

返回x提升到权力y。例外情况应尽可能遵循C99标准的附件'F'。特别是,pow(1.0, x)pow(x, 0.0)总是返回1.0,即使x是零或为NaN。如果两个xy是有限的,x是负的,而y不是整数然后pow(x, y)是未定义的,并提高ValueError

与内置**运算符不同,math.pow()将其两个参数都转换为类型float。使用**或内置pow()函数来计算精确的整数幂。

在版本2.6中更改:结果1**nannan**0未定义。

math.sqrt(x)

返回x的平方根。

3.三角函数

math.acos(x)

返回x的反余弦,单位为弧度。

math.asin(x)

以弧度返回x的反正弦。

math.atan(x)

以弧度返回x的反正切。

math.atan2(y, x)

返回atan(y / x),以弧度表示。结果在-pi和之间pi。从原点到点的平面中的矢量(x, y)与正X轴形成此角度。重点atan2()是两个输入的符号都是已知的,所以它可以计算角度的正确象限。例如,atan(1)并且atan2(1, 1)都是pi/4,但atan2(-1, -1)就是-3*pi/4

math.cos(x)

返回x弧度的余弦。

math.hypot(x, y)

返回欧几里得规范,sqrt(x*x + y*y)。这是从原点到点的矢量长度(x, y)

math.sin(x)

返回x弧度的正弦值。

math.tan(x)

返回x弧度的切线。

4.角度转换

math.degrees(x)

将角度x从弧度转换为度数。

math.radians(x)

将角度x从度数转换为弧度。

5.双曲函数

math.acosh(x)

返回x的反双曲余弦。

2.6版本中的新功能。

math.asinh(x)

返回x的反双曲正弦。

2.6版本中的新功能。

math.atanh(x)

返回x的反双曲正切。

2.6版本中的新功能。

math.cosh(x)

返回x的双曲余弦。

math.sinh(x)

返回x的双曲正弦。

math.tanh(x)

Return the hyperbolic tangent of x.

6. Special functions

math.erf(x)

Return the error function at x.

New in version 2.7.

math.erfc(x)

Return the complementary error function at x.

New in version 2.7.

math.gamma(x)

Return the Gamma function at x.

New in version 2.7.

math.lgamma(x)

Return the natural logarithm of the absolute value of the Gamma function at x.

New in version 2.7.

7. Constants

math.pi

The mathematical constant π = 3.141592..., to available precision.

math.e

The mathematical constant e = 2.718281..., to available precision.

CPython implementation detail: The math module consists mostly of thin wrappers around the platform C math library functions. Behavior in exceptional cases follows Annex F of the C99 standard where appropriate. The current implementation will raise ValueError for invalid operations like sqrt(-1.0) or log(0.0) (where C99 Annex F recommends signaling invalid operation or divide-by-zero), and OverflowError for results that overflow (for example, exp(1000.0)). A NaN will not be returned from any of the functions above unless one or more of the input arguments was a NaN; in that case, most functions will return a NaN, but (again following C99 Annex F) there are some exceptions to this rule, for example pow(float('nan'), 0.0) or hypot(float('nan'), float('inf')).

Note that Python makes no effort to distinguish signaling NaNs from quiet NaNs, and behavior for signaling NaNs remains unspecified. Typical behavior is to treat all NaNs as though they were quiet.

Changed in version 2.6: Behavior in special cases now aims to follow C99 Annex F. In earlier versions of Python the behavior in special cases was loosely specified.

See also

Module cmath Complex number versions of many of these functions.

 © 2001–2017 Python Software Foundation

Licensed under the PSF License.

https://docs.python.org/2.7/library/math.html

数字与数学 | Numeric & Mathematical相关

Python

Python 是一种面向对象的解释型计算机程序设计语言,由荷兰人 Guido van Rossum 于1989年发明,第一个公开发行版发行于1991年。 Python 是纯粹的自由软件, 源代码和解释器 CPython 遵循 GPL 协议。Python 语法简洁清晰,特色之一是强制用空白符( white space )作为语句缩进。

主页 https://www.python.org/
源码 https://github.com/python/cpython
版本 2.7
发布版本 2.7.13

Python目录

1.内置常量 | Built-in Constants
2.内置例外 | Built-in Exceptions
3.内置函数 | Built-in Functions
4.内置类型 | Built-in Types
5.编译器 | Compiler
6.加密 | Cryptography
7.数据压缩 | Data Compression
8.数据持久性 | Data Persistence
9.数据类型 | Data Types
10.调试和分析 | Debugging & Profiling
11.开发工具 | Development Tools
12.文件和目录访问 | File & Directory Access
13.文件格式 | File Formats
14.构架 | Frameworks
15.输入 | Importing
16.输入/输出 | Input/ouput
17.国际化 | Internationalization
18.网络 | Internet
19.网络数据 | Internet Data
20.翻译 | Interpreters
21.语言 | Language
22.记录 | Logging
23.Mac OS
24.MS Windows
25.多媒体 | Multimedia
26.联网 | Networking
27.数字与数学 | Numeric & Mathematical
28.操作系统 | Operating System
29.可选操作系统 | Optional Operating System
30.限制执行 | Restricted Execution
31.运行 | Runtime
32.SGI IRIX
33.软件包装与分销 | Software Packaging & Distribution
34.字符串 | String
35.结构化标记 | Structured Markup
36.Tk
37.Unix
38.Python 简介
39.Python pass 语句
40.Python 循环嵌套
41.Python 运算符
42.Python log10() 函数
43.Python log() 函数
44.Python floor() 函数
45.Python fabs() 函数
46.Python exp() 函数
47.Python cmp() 函数
48.Python ceil() 函数
49.Python abs() 函数
50.Python Number(数字)
51.Python pow() 函数
52.Python modf() 函数
53.Python min() 函数
54.Python max() 函数
55.Python asin() 函数
56.Python acos() 函数
57.Python uniform() 函数
58.Python shuffle() 函数
59.Python seed() 函数
60.Python random() 函数
61.Python randrange() 函数
62.Python choice() 函数
63.Python sqrt() 函数
64.Python round() 函数
65.Python radians() 函数
66.Python degrees() 函数
67.Python tan() 函数
68.Python sin() 函数
69.Python hypot() 函数
70.Python cos() 函数
71.Python atan2() 函数
72.Python atan() 函数
73.Python 元组
74.Python 列表(List)
75.Python 字符串
76.Python 字典(Dictionary)
77.Python 日期和时间
78.Python 函数
79.Python 模块
80.Python capitalize()方法
81.Python center()方法
82.Python count() 方法
83.Python expandtabs()方法
84.Python endswith()方法
85.Python encode()方法
86.Python decode()方法
87.Python find()方法
88.Python index()方法
89.Python 异常处理
90.Python isspace()方法
91.Python isnumeric()方法
92.Python islower()方法
93.Python isdigit()方法
94.Python isalpha()方法
95.Python isalnum()方法
96.Python isupper()方法
97.Python istitle()方法
98.Python min()方法
99.Python max()方法
100.Python maketrans()方法