Top.Mail.Ru
Python
? ?
LiveJournal for python.
View:Personal Journal.
View:Friends.
View:Calendar.
Missed some entries? Then simply jump to the previous day or the next day.

Friday, October 26th, 2007

Subject:Floating-point incrementing issue
Time:7:50 pm.
Hi all,

I've got a semi-urgent (I'd love to know by Sunday) question that hopefully somebody here can answer:

I've written a program that, at one point, does the following:

value = 0
for i in range(12):
value += 1.2
return value

The problem is, instead of the expected 14.4, it returns 14.399999999999997.
This isn't just an issue with my program...the interactive Python prompt does the same thing.

It reminds me of something I saw in a class I took in C, a sort of "fix the bugs" assignment...but I didn't exactly do well in that class, so I haven't got a clue what the problem is.

For reference, I'm using Python 2.5 on Mac OS X (Intel). If anyone has any thoughts and/or manages to duplicate the bug, please let me know.

Edit: That should in fact be an indent in that 'for' loop, but apparently LJ deletes line-initial whitespace?
Comments: Read 14 orAdd Your Own.

LiveJournal for python.

View:User Info.
View:Friends.
View:Calendar.
View:Memories.
Missed some entries? Then simply jump to the previous day or the next day.

Image