Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
883 views
in Technique[技术] by (71.8m points)

numpy - Floating point precision in Python array

I apologize for the really simple and dumb question; however, why is there a difference in precision displayed for these two cases?

1)

>> test = numpy.array([0.22])
>> test2 = test[0] * 2
>> test2
0.44

2)

>> test = numpy.array([0.24])
>> test2 = test[0] * 2
>> test2
0.47999999999999998

I'm using python2.6.6 on 64-bit linux. Thank you in advance for your help.

This also hold seems to hold for a list in python

>>> t = [0.22]
>>> t
[0.22]

>>> t = [0.24]
>>> t
[0.23999999999999999]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because they are different numbers and different numbers have different rounding effects.

(Practically any of the Related questions down the right-hand side will explain the cause of the rounding effects themselves.)


Okay, more serious answer. It appears that numpy performs some transformation or calculation on the numbers in an array:

>>> t = numpy.array([0.22])
>>> t[0]
0.22


>>> t = numpy.array([0.24])
>>> t[0]
0.23999999999999999

whereas Python doesn't automatically do this:

>>> t = 0.22
>>> t
0.22

>>> t = 0.24
>>> t
0.24

The rounding error is less than numpy's "eps" value for float, which implies that it should be treated as equal (and in fact, it is):

>>> abs(numpy.array([0.24])[0] - 0.24) < numpy.finfo(float).eps
True

>>> numpy.array([0.24])[0] == 0.24
True

But the reason that Python displays it as '0.24' and numpy doesn't is because Python's default float.__repr__ method uses lower precision (which, IIRC, was a pretty recent change):

>>> str(numpy.array([0.24])[0])
0.24

>>> '%0.17f' % 0.24
'0.23999999999999999'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...