I am new to numpy. I tried to add two numpy.float64 numbers, however, the numpy gave me a wrong answer.
Here's my code:(a and b are generated separately by the mean of one numpy array)
print(a)
print(type(a))
print(b)
print(type(b))
c = (a b)/2
print(c)
print(type(c))
And here's what I got:
1.4617937201411304
<class 'numpy.float64'>
-1.4617937201411302
<class 'numpy.float64'>
1.1102230246251565e-16
<class 'numpy.float64'>
Clearly, the answer should be very close to 0, but what I got is far away from 0.
I guess the problem is that a b caused an overflow?
However, when I tried to double a, the answer was right:
c = (a * 2)/2
The answer is:
1.4617937201411304
<class 'numpy.float64'>
-1.4617937201411302
<class 'numpy.float64'>
1.4617937201411304
<class 'numpy.float64'>
CodePudding user response:
You missed the e-16
at the end of the print. It means the result is 1.1102230246251565 * 10^(-16)
, which is indeed almost zero.
It's a convention to print numbers with e
(the exponent) rather then 0.000000....1110