5.4.12 特殊函数
统计学中经常用到高斯误差函数(Gauss error function)。
import math
print('{:^5} {:7}'.format('x','erf(x)'))
print('{:-^5} {:-^7}'.format('',''))
for x in [-3,-2,-1,-0.5,-0.25,0,0.25,0.5,1,2,3]:
print('{:5.2f} {:7.4f}'.format(x,math.erf(x)))
对于误差函数,erf(-x) == -erf(x)。
运行结果:
补余误差函数erfc()生成等价于1 - erf(x)的值。
import math
print('{:^5} {:7}'.format('x','erfc(x)'))
print('{:-^5} {:-^7}'.format('',''))
for x in [-3,-2,-1,-0.5,-0.25,0,0.25,0.5,1,2,3]:
print('{:5.2f} {:7.4f}'.format(x,math.erfc(x)))
如果x值很小,那么在从1做减法时erfc()实现便可以避免可能的精度误差。
运行结果: