User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,534 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,992 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 504 | Replies: 4
Reply
Join Date: Oct 2007
Posts: 2
Reputation: tesak is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tesak tesak is offline Offline
Newbie Poster

Question Python bug??? Or just a stupid question?

  #1  
Oct 5th, 2007
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.
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
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

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

  #2  
Oct 5th, 2007
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 ...
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!
Reply With Quote  
Join Date: Dec 2006
Posts: 468
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 65
woooee woooee is offline Offline
Posting Pro in Training

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

  #3  
Oct 5th, 2007
Look at the line following
elif status == 0.3:
And why do you have if/elif? Why not just print the value?
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: tesak is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tesak tesak is offline Offline
Newbie Poster

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

  #4  
Oct 6th, 2007
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?
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

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

  #5  
Oct 6th, 2007
Well, there are actually three issues in the code:

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

  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

  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:

  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:

  1. ...
  2. if fuzzyequals(status,0.2): # or fuzzyequals(status, 0.2, 0.01)
  3. print status
  4. ...


Hope that helps,
Jeff
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 4:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC