944,196 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1026
  • Python RSS
Oct 5th, 2007
0

Python bug??? Or just a stupid question?

Expand Post »
Hello,
I am a newbie in Python and maybe this is a stupid question, but when I run this code I receive a very strange output.
Python Syntax (Toggle Plain Text)
  1. status = 0
  2. for i in range(10):
  3. status = status + 0.1
  4.  
  5. if status == 0.1:
  6. print status
  7. elif status == 0.2:
  8. print status
  9. elif status == 0.3:
  10. status
  11. elif status == 0.4:
  12. print status
  13. elif status == 0.5:
  14. print status
  15. elif status == 0.6:
  16. print status
  17. elif status == 0.7:
  18. print status
  19. elif status == 0.8:
  20. print status
  21. elif status == 0.9:
  22. print status
  23. elif status == 1.0:
  24. print status
At first I supposed that I will receive something like this:
>>>
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
>>>

But finally I was really surprised. Output looks like this:
>>>
0.1
0.2
0.4
0.5
0.6
0.7
>>>

Could somebody tells me what I am doing wrong?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tesak is offline Offline
2 posts
since Oct 2007
Oct 5th, 2007
0

Re: Python bug??? Or just a stupid question?

This happens with many computer languages. You are trying to compare floating point values directly. Your 0.8 might be more like 0.799999999999999 and the compare flunks!

You can make this work by giving it a range you can compare within ...
Python Syntax (Toggle Plain Text)
  1. if 0.79999 < status < 0.80001:
  2. ... do something`
Last edited by vegaseat; Oct 5th, 2007 at 10:22 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 5th, 2007
0

Re: Python bug??? Or just a stupid question?

Look at the line following
elif status == 0.3:
And why do you have if/elif? Why not just print the value?
Reputation Points: 741
Solved Threads: 694
Nearly a Posting Maven
woooee is offline Offline
2,314 posts
since Dec 2006
Oct 6th, 2007
0

Re: Python bug??? Or just a stupid question?

Thanks for an answer. It was just an example which really surprise me. But is there a way how to compare two float numbers without unpredictable results?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tesak is offline Offline
2 posts
since Oct 2007
Oct 6th, 2007
0

Re: Python bug??? Or just a stupid question?

Well, there are actually three issues in the code:

(1) is a matter of efficiency, which woooee already raised.

Python Syntax (Toggle Plain Text)
  1. status = 0
  2. for i in range(10):
  3. status = status + 0.1
  4. print status

does what you wanted to do, and is cleaner to read. One payoff of this approach is that...

(2) ...in your original code, you didn't get 0.3 in the output because you left out the print statement. The lines

Python Syntax (Toggle Plain Text)
  1. elif status == 0.3:
  2. status

have the effect of evaluating, but not printing, status. And then throwing away the result, because you aren't doing anything with it.

(3) Finally, comparing floating points is a common source of bugs. Even if we worked with BCD, so that we didn't have to work about base-2 to base-10 errors, still and all, our decimal approximations would still lead to imprecision. For example, the test π == 3.14159265358979 is False.

Vega gives a very good approach to this by selecting a range. My own approach is to define a function that is in my code base:

Python Syntax (Toggle Plain Text)
  1. def fuzzyequals(a,b,epsilon = 0.0001):
  2. return abs(a-b) < epsilon

This function returns True if a is within the "halo" of b; that is, if b-epsilon < a < b+epsilon.

You can then use it like this:

Python Syntax (Toggle Plain Text)
  1. ...
  2. if fuzzyequals(status,0.2): # or fuzzyequals(status, 0.2, 0.01)
  3. print status
  4. ...


Hope that helps,
Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Graphics help...
Next Thread in Python Forum Timeline: Default arguments - what is Python doing?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC