python 使用math_Python之math模块的使用

  • Post author:
  • Post category:python


math模块的作用

math模块实现了正常情况下原生平台C库中才有的很多专用IEEE函数,可以使用浮点值完成复杂的数学运算,包括对数和三角函数运算。

1、打印常量的示例

importmathprint(‘π: {:.30f}’.format(math.pi))print(‘e: {:.30f}’.format(math.e))print(‘nan: {:.30f}’.format(math.nan))print(‘inf: {:.30f}’.format(math.inf))

math_constants.py

运行效果

π: 3.141592653589793115997963468544e:2.718281828459045090795598298428nan: nan

inf: inf

2、测试异常值

importmathprint(‘{:^3} {:6} {:6} {:6}’.format(‘e’, ‘x’, ‘x**2’, ‘isinf’))print(‘{:-^3} {:-^6} {:-^6} {:-^6}’.format(”, ”, ”, ”))for e in range(0, 201, 20):

x= 10.0 **e

y= x *xprint(‘{:3d} {:<6g} {:<6g} {!s:6}’.format(

e, x, y, math.isinf(y),

))

math_isinf.py

运行效果

e x x**2isinf— —— —— ——01 1False20 1e+20 1e+40False40 1e+40 1e+80False60 1e+60 1e+120False80 1e+80 1e+160False100 1e+100 1e+200False120 1e+120 1e+240False140 1e+140 1e+280False160 1e+160inf True180 1e+180inf True200 1e+200inf True

总结:

指数变足够大时,x的平台无法再存放于一个double中,这个值就会被记录为无穷大。

3、用浮点数计算时,数据大,会抛出OverflowError异常

x = 10.0 ** 200

print(‘x =’, x)print(‘x*x =’, x *x)print(‘x**2 =’, end=’ ‘)try:print(x ** 2)exceptOverflowError as err:print(err)

math_overflow.py

运行效果

x = 1e+200x*x =inf

x**2 = (34, ‘Result too large’)

4、一个数无穷大的时候,结果为nan

importmath

x= (10.0 ** 200) * (10.0 ** 200)

y= x /xprint(‘x =’, x)print(‘isnan(x) =’, math.isnan(x))print(‘y = x / x =’, x /x)print(‘y == nan =’, y == float(‘nan’))print(‘isnan(y) =’, math.isnan(y))

math_isnan.py

运行效果

x =inf

isnan(x)=False

y= x / x =nan

y== nan =False

isnan(y)= True

5、检查普通数还是特殊值

importmathfor f in [0.0, 1.0, math.pi, math.e, math.inf, math.nan]:print(‘{:5.2f} {!s}’.format(f, math.isfinite(f)))

math_isfinite.py

运行效果

0.00True1.00True3.14True2.72True

inf False

nan False

6、相对比较

importmath

INPUTS=[

(1000, 900, 0.1),

(100, 90, 0.1),

(10, 9, 0.1),

(1, 0.9, 0.1),

(0.1, 0.09, 0.1),

]print(‘{:^8} {:^8} {:^8} {:^8} {:^8} {:^8}’.format(‘a’, ‘b’, ‘rel_tol’, ‘abs(a-b)’, ‘tolerance’, ‘close’)

)print(‘{:-^8} {:-^8} {:-^8} {:-^8} {:-^8} {:-^8}’.format(‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘),

)

fmt= ‘{:8.2f} {:8.2f} {:8.2f} {:8.2f} {:8.2f} {!s:>8}’

for a, b, rel_tol inINPUTS:

close= math.isclose(a, b, rel_tol=rel_tol)

tolerance= rel_tol *max(abs(a), abs(b))

abs_diff= abs(a -b)print(fmt.format(a, b, rel_tol, abs_diff, tolerance, close))

math_isclose.py

运行效果

a b rel_tol abs(a-b) tolerance close——– ——– ——– ——– ——– ——–

1000.00 900.00 0.10 100.00 100.00True100.00 90.00 0.10 10.00 10.00True10.00 9.00 0.10 1.00 1.00True1.00 0.90 0.10 0.10 0.10True0.10 0.09 0.10 0.01 0.01 False #比较失败,是因为误差小于0.1,比较不了,应该使用绝对比较

7、绝对比较

importmath

INPUTS=[

(1.0, 1.0 + 1e-07, 1e-08),

(1.0, 1.0 + 1e-08, 1e-08),

(1.0, 1.0 + 1e-09, 1e-08),

]print(‘{:^8} {:^11} {:^8} {:^10} {:^8}’.format(‘a’, ‘b’, ‘abs_tol’, ‘abs(a-b)’, ‘close’)

)print(‘{:-^8} {:-^11} {:-^8} {:-^10} {:-^8}’.format(‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘),

)for a, b, abs_tol inINPUTS:

close= math.isclose(a, b, abs_tol=abs_tol)

abs_diff= abs(a -b)print(‘{:8.2f} {:11} {:8} {:0.9f} {!s:>8}’.format(

a, b, abs_tol, abs_diff, close))

math_isclose_abs_tol.py

运行效果

a b abs_tol abs(a-b) close——– ———– ——– ———- ——–

1.00 1.0000001 1e-08 0.000000100False1.00 1.00000001 1e-08 0.000000010True1.00 1.000000001 1e-08 0.000000001 True

8、nan、inf特殊的比



版权声明:本文为weixin_42334285原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。