| | |
Python bug??? Or just a stupid question?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
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.
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?
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)
status = 0 for i in range(10): status = status + 0.1 if status == 0.1: print status elif status == 0.2: print status elif status == 0.3: status elif status == 0.4: print status elif status == 0.5: print status elif status == 0.6: print status elif status == 0.7: print status elif status == 0.8: print status elif status == 0.9: print status elif status == 1.0: print status
>>>
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?
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 ...
You can make this work by giving it a range you can compare within ...
Python Syntax (Toggle Plain Text)
if 0.79999 < status < 0.80001: ... do something`
Last edited by vegaseat; Oct 5th, 2007 at 10:22 pm.
May 'the Google' be with you!
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Well, there are actually three issues in the code:
(1) is a matter of efficiency, which woooee already raised.
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
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:
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:
Hope that helps,
Jeff
(1) is a matter of efficiency, which woooee already raised.
Python Syntax (Toggle Plain Text)
status = 0 for i in range(10): status = status + 0.1 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)
elif status == 0.3: 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)
def fuzzyequals(a,b,epsilon = 0.0001): 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)
... if fuzzyequals(status,0.2): # or fuzzyequals(status, 0.2, 0.01) print status ...
Hope that helps,
Jeff
![]() |
Similar Threads
- return array [newbie question] (Python)
- n00b question linked to bits :rolleyes: (Assembly)
- Stupid question about java.exe (Java)
- I know I'll sound stupid but... (Geeks' Lounge)
- a stupid question!!! (Windows Software)
- Another photoshop question! (Graphics and Multimedia)
- This may be a stupid question.... (C++)
- Stupid Newbie Question (C)
- Ive got a question that needsa answerin. (Search Engine Optimization)
Other Threads in the Python Forum
- Previous Thread: Graphics help...
- Next Thread: Default arguments - what is Python doing?
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book change code color converter countpasswordentry dan08 dictionary dynamic edit editing enter examples excel file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pyqt pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






