numpy.power()和math.pow()不会给出相同的结果

时间:2015-05-17 11:47:45

标签: python math numpy floating-point-precision

numpy.power()是否比math.pow()更准确?

示例:

给定A = numpy.array([6.66655333e+12,6.66658000e+12,6.66660667e+12,3.36664533e+12])

我定义 result = numpy.power(A,2.5)

所以 >> result = [ 1.14750185e+32 1.14751333e+32 1.14752480e+32 2.07966517e+31]

然而:

math.pow(A[0],2.5) = 1.14750185103e+32

math.pow(A[1],2.5) = 1.14751332619e+32

math.pow(A[2],2.5) = 1.14752480144e+32

math.pow(A[3],2.5) = 2.079665167e+31

1 个答案:

答案 0 :(得分:6)

这只是一个问题如何显示数字:

>>> result[0]
1.1475018493845227e+32

>>> math.pow(A[0],2.5)
1.1475018493845227e+32

两种方式都有相同的价值:

>>> result[0] == math.pow(A[0],2.5)
True

result有自己的方法__repr__(),其显示的数字与__repr__()的标准Python float不同。

相关问题