6.1 numbers--数值抽象基类

6. 数值和数学库

本库主要提供了数值和数学相关的函数处理以及数据类型。numbers模块定义了抽象的数值类型,mathcmath模块主要包括不同的数学函数,比如用来计算浮点数和复数。decimal模块支持十进制数值表示,可以用任何精度进行运算。

6.1 numbers--数值抽象基类

本模块主要按文档(PEP 3141)定义了数值抽象基类。

class numbers.Number 

数值类的基类,如果你只是想检查一个参数x是否是Number类型,可以使用函数isinstance(x, Number)来检查。

 

6.1.1 数值的基类

class numbers.Complex 

本类是复数的抽象类,包括了内置类型复数的操作。

real

复数的实部。

imag

复数的虚部。

conjugate()

复数的共轭复数。

 

class numbers.Real 

复数类型里的实部类,主要针对实数的操作运算。

比如转换floatmath.trunc()round()math.floor()math.ceil()divmod()//%<<=>>=

 

class numbers.Rational 

Real类型的子类。

numerator

抽象基类。

denominator 

抽象基类。

 

class numbers.Integral 

Rational的子类型,增加转换int的功能。

 


6.2 math--数学函数

本模块总是可以使用的,因为它主要从C标准来定义的数学函数库转换过来。但这些函数不能使用来处理复数,如果需要处理复数,要使用cmath模块里相同的函数。分成两个模块的原因,就是有很多人不需要学习复数,就使用math模块就可以了。在本模块里除了说明返回值的类型之外,否则所有返回值的类型都是float浮点数。

6.2.1 数值理论和数值表示

math.ceil(x) 

返回大于等于x的最小整数。如果x不是浮点数,直接使用x.__ceil__()函数返回整数值。

例子:

#python 3.4

import math

 

n = math.ceil(2.5)

print(n)

n = math.ceil(3)

print(n)

n = math.ceil(3.0)

print(n)

n = math.ceil(3.1)

print(n)

结果输出如下:

3

3

3

4

 

math.copysign(x, y) 

返回一个浮点数,绝对值等于x,正负号取决于y的正负号。

例子:

#python 3.4

import math

 

n = math.copysign(2.5, -0.0)

print(n)

n = math.copysign(-3, 0.0)

print(n)

n = math.copysign(3.0, -2.0)

结果输出如下:

-2.5

3.0

-3.0

4

 

math.fabs(x) 

返回x的绝对值。

例子:

#python 3.4

import math

 

n = math.fabs(2.5)

print(n)

n = math.fabs(-3)

print(n)

n = math.fabs(-3.8)

print(n)

结果输出如下:

2.5

3.0

3.8

 

math.factorial(x) 

返回x的阶乘。如果x不是一个整数或负数,会抛出异常ValueError

例子:

#python 3.4

import math

 

n = math.factorial(2)

print(n)

n = math.factorial(5)

print(n)

n = math.factorial(8)

print(n)

结果输出如下:

2

120

40320

 

math.floor(x) 

返回比x小于或者等于的最大整数。如果x不是一个浮点数,直接返回一个整数。

例子:

#python 3.4

import math

 

n = math.floor(2.1)

print(n)

n = math.floor(2.9)

print(n)

n = math.floor(2)

print(n)

结果输出如下:

2

2

2

 

math.fmod(x, y) 

返回C库里的xy取模。与Python里的x % y的值是有可能存在差异。因此本函数主要使用在浮点数的取模,而Python里 x % y一般用来是整数的取模运算。

例子:

#python 3.4

import math

 

n = math.fmod(2.1, 2)

print(n)

n = math.fmod(6.9, 2)

print(n)

n = math.fmod(8, 3)

print(n)

结果输出如下:

0.10000000000000009

0.9000000000000004

2.0

 

math.frexp(x) 

把一个数分解成尾数和指数(m, e)。等同于x = m* 2**e

例子:

#python 3.4

import math

 

n = math.frexp(2)

print(n)

n = math.frexp(3)

print(n)

n = math.frexp(4)

print(n)

结果输出如下:

(0.5, 2)

(0.75, 2)

(0.5, 3)

 

math.fsum(iterable) 

统计迭代器iterable里有浮点数值,计算和。

例子:

#python 3.4

import math

 

l = [2, 3, 4, 5, 6]

n = math.fsum(l)

print(n)

结果输出如下:

20.0

 

math.isfinite(x) 

判断x是否有限大的值。比如无穷大的值或Nan返回False,否则返回True

例子:

#python 3.4

import math

 

n = math.isfinite(1.0)

print(n)

n = math.isfinite(float('NaN'))

print(n)

结果输出如下:

True

False

 

math.isinf(x) 

测试x是否无限大的值。

例子:

#python 3.4

import math

 

n = math.isinf(1.0)

print(n)

n = math.isinf(float('inf'))

print(n)

结果输出如下:

False

True

 

math.isnan(x) 

判断x是否一个非数字的数。

例子:

#python 3.4

import math

 

n = math.isnan(1.0)

print(n)

n = math.isnan(float('inf'))

print(n)

n = math.isnan(float('NaN'))

print(n)

结果输出如下:

False

False

True

 

math.ldexp(x, i) 

计算值x乘以2的次幂的值。等同于 x* (2**i)

例子:

#python 3.4

import math

 

n = math.ldexp(1.0, 2)

print(n)

n = math.ldexp(2.0, 2)

print(n)

结果输出如下:

4.0

8.0

 

math.modf(x) 

分解x的值为整数和小数部分。

例子:

#python 3.4

import math

 

n = math.modf(6.0)

print(n)

n = math.modf(3.14)

print(n)

结果输出如下:

(0.0, 6.0)

(0.14000000000000012, 3.0)

 

math.trunc(x) 

返回实数x的整数部分。

例子:

#python 3.4

import math

 

n = math.trunc(6.9)

print(n)

n = math.trunc(3.14)

print(n)

结果输出如下:

6

3

6.2.2 幂与对数的操作函数

math.exp(x) 

返回e**x的值。

例子:

#python 3.4

import math

 

n = math.exp(6)

print(n)

n = math.exp(3.14)

print(n)

结果输出如下:

403.4287934927351

23.103866858722185

 

math.expm1(x)

返回e**x - 1的值。

例子:

#python 3.4

import math

 

n = math.expm1(6)

print(n)

n = math.expm1(3.14)

print(n)

结果输出如下:

402.4287934927351

22.103866858722185

 

math.log(x[, base]) 

一个参数输入时,直接返回x的自然对数。如果两个参数输入,则返回以base为底的对数。

例子:

#python 3.4

import math

 

n = math.log(6)

print(n)

n = math.log(3.14, 10)

print(n)

结果输出如下:

1.791759469228055

0.4969296480732149

 

math.log1p(x) 

计算1+x的自然对数。

例子:

#python 3.4

import math

 

n = math.log1p(6)

print(n)

n = math.log1p(3.14)

print(n)

结果输出如下:

1.9459101490553132

1.420695787837223

 

math.log2(x) 

返回2为底的x对数值。

例子:

#python 3.4

import math

 

n = math.log2(6)

print(n)

n = math.log2(3.14)

print(n)

结果输出如下:

2.584962500721156

1.6507645591169022

 

math.log10(x) 

返回以10为底的x对数值。

例子:

#python 3.4

import math

 

n = math.log10(6)

print(n)

n = math.log10(3.14)

print(n)

结果输出如下:

0.7781512503836436

0.49692964807321494

 

math.pow(x, y) 

返回以x为底的y次幂的值。比较特别的是pow(1.0, x)pow(x, 0.0)都是返回1.0。如果是其它没有定义的值,就抛出异常ValueError

例子:

#python 3.4

import math

 

n = math.pow(6, 2)

print(n)

n = math.pow(3.14, 2)

print(n)

结果输出如下:

36.0

9.8596

 

math.sqrt(x)

返回x的平方根。

例子:

#python 3.4

import math

 

n = math.sqrt(6)

print(n)

n = math.sqrt(16)

print(n)

结果输出如下:

2.449489742783178

4.0

 

6.2.3 三角的操作函数

math.acos(x) 

返回反余弦函数,单位是弧度。

例子:

#python 3.4

import math

 

n = math.acos(-1.0)

print(n)

n = math.acos(0.5)

print(n)

结果输出如下:

3.141592653589793

1.0471975511965979

 

math.asin(x) 

返回反正弦函数,单位为弧度。

例子:

#python 3.4

import math

 

n = math.asin(-1.0)

print(n)

n = math.asin(0.5)

print(n)

结果输出如下:

-1.5707963267948966

0.5235987755982989

 

math.atan(x) 

返回正切函数的值,单位为弧度。

例子:

#python 3.4

import math

 

n = math.atan(-1.0)

print(n)

n = math.atan(0.5)

print(n)

结果输出如下:

-0.7853981633974483

0.4636476090008061

 

math.atan2(y, x) 

atan2(y,x)所表达的意思是坐标原点为起点,指向(x,y)的射线在坐标平面上与x轴正方向之间的角的角度。

例子:

#python 3.4

import math

 

n = math.atan2(1.0, 1.0)

print(n)

n = math.atan2(1.0, 0)

print(n)

结果输出如下:

0.7853981633974483

1.5707963267948966

 

math.cos(x)

返回余弦函数的值,x的单位为弧度。

例子:

#python 3.4

import math

 

n = math.cos(3.14/2)

print(n)

n = math.cos(3.14/4)

print(n)

结果输出如下:

0.0007963267107332633

0.7073882691671998

 

math.hypot(x, y) 

返回原点到指定坐标点(x, y)的距离。

例子:

#python 3.4

import math

 

n = math.hypot(3, 4)

print(n)

n = math.hypot(5, 5)

print(n)

结果输出如下:

5.0

7.0710678118654755

 

math.sin(x) 

返回正弦函数的值。

例子:

#python 3.4

import math

 

n = math.sin(3.14/4)

print(n)

n = math.sin(3.14/2)

print(n)

结果输出如下:

0.706825181105366

0.9999996829318346

 

math.tan(x) 

返回正切函数的值。

例子:

#python 3.4

import math

 

n = math.tan(3.14/4)

print(n)

n = math.tan(3.14/2)

print(n)

结果输出如下:

0.9992039901050427

1255.7655915007897

 

6.2.4 角度转换函数

math.degrees(x) 

转换弧度到角度。

例子:

#python 3.4

import math

 

n = math.degrees(3.14/4)

print(n)

n = math.degrees(3.14/2)

print(n)

结果输出如下:

44.97718691776962

89.95437383553924

 

math.radians(x) 

转换角度为弧度。

例子:

#python 3.4

import math

 

n = math.radians(90)

print(n)

n = math.radians(45)

print(n)

结果输出如下:

1.5707963267948966

0.7853981633974483

6.2.5 双曲线的函数

math.acosh(x) 

返回反双曲余弦函数的值。

例子:

#python 3.4

import math

 

n = math.acosh(90)

print(n)

n = math.acosh(45)

print(n)

结果输出如下:

5.192925985263684

4.499686190671499

 

math.asinh(x) 

返回反双曲线正弦函数的值。

例子:

#python 3.4

import math

 

n = math.asinh(90)

print(n)

n = math.asinh(45)

print(n)

结果输出如下:

5.192987713658941

4.49993310426429

 

math.atanh(x) 

返回反双曲正切函数的值。

例子:

#python 3.4

import math

 

n = math.atanh(0.9)

print(n)

n = math.atanh(0.8)

print(n)

结果输出如下:

1.4722194895832204

1.0986122886681098

 

math.cosh(x) 

返回双曲余弦值。

例子:

#python 3.4

import math

 

n = math.cosh(0.9)

print(n)

n = math.cosh(0.8)

print(n)

结果输出如下:

1.4330863854487743

1.3374349463048447

 

math.sinh(x) 

返回双曲正弦函数的值。

例子:

#python 3.4

import math

 

n = math.sinh(0.9)

print(n)

n = math.sinh(0.8)

print(n)

结果输出如下:

1.0265167257081753

0.888105982187623

 

math.tanh(x) 

返回双曲正切函数的值。

例子:

#python 3.4

import math

 

n = math.tanh(0.9)

print(n)

n = math.tanh(0.8)

print(n)

结果输出如下:

0.7162978701990245

0.664036770267849

 

6.2.6 一些特别的函数

math.erf(x) 

返回误差函数的值。

例子:

#python 3.4

import math

 

n = math.erf(0.9)

print(n)

n = math.erf(10)

print(n)

结果输出如下:

0.796908212422832

1.0

 

math.erfc(x) 

返回余补误差函数的值。

例子:

#python 3.4

import math

 

n = math.erfc(0.9)

print(n)

n = math.erfc(10)

print(n)

结果输出如下:

0.20309178757716795

2.0884875837625433e-45

 

math.gamma(x) 

伽玛函数(Gamma Function)作为阶乘的延拓,是定义在复数范围内的亚纯函数。

例子:

#python 3.4

import math

 

n = math.gamma(0.9)

print(n)

n = math.gamma(5)

print(n)

结果输出如下:

1.068628702119319

24.0

 

math.lgamma(x) 

返回xgamma函数的绝对值,然后对绝对值取自然对数的值。

例子:

#python 3.4

import math

 

n = math.lgamma(0.9)

print(n)

n = math.lgamma(5)

print(n)

结果输出如下:

0.0663762397347431

3.178053830347945

6.2.7 数学常用的常量

math.pi

返回圆周率常量的值。

例子:

#python 3.4

import math

 

n = math.pi

print(n)

结果输出如下:

3.141592653589793

 

math.e 

返回数学上常量e的值。

例子:

#python 3.4

import math

 

n = math.e

print(n)

结果输出如下:

2.718281828459045



蔡军生  QQ:9073204  深圳

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值