Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.power() and math.pow() don't give the same result

Is numpy.power() less accurate then math.pow()?

Example:

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

I define result = numpy.power(A,2.5)

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

However:

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

like image 915
farhawa Avatar asked Feb 11 '23 03:02

farhawa


1 Answers

This is just a questions how the numbers are displayed:

>>> result[0]
1.1475018493845227e+32

and:

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

Both ways lead to same value:

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

result has its own method __repr__() that shows numbers differently than the standard Python __repr__() for float.

like image 190
Mike Müller Avatar answered Feb 13 '23 12:02

Mike Müller