Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 8th, 2007, 10:45 AM
tomamil
Guest
 
Posts: n/a
Default Really basic problem

i know this example is stupid and useless, but that's not the answer
to my question.
here it goes:

status = 0.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:
print 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

the expected output:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

but it gives me instead:
0.1
0.2
0.4
0.5
0.6
0.7

why?

thanks,

m.

  #2  
Old October 8th, 2007, 11:05 AM
John Machin
Guest
 
Posts: n/a
Default Re: Really basic problem

On 8/10/2007 7:39 PM, tomamil wrote:
Quote:
i know this example is stupid and useless, but that's not the answer
to my question.
here it goes:
>
status = 0.0
for i in range(10):
status = status + 0.1
[snip]
0.1 can not be represented exactly as a binary floating-point number. to
see what is really happening, use repr(), e.g. like this:
Quote:
Quote:
Quote:
>>t = 0.0
>>for i in range(10):
.... t += 0.1
.... print i, t, repr(t)
....
0 0.1 0.10000000000000001
1 0.2 0.20000000000000001
2 0.3 0.30000000000000004
3 0.4 0.40000000000000002
4 0.5 0.5
5 0.6 0.59999999999999998
6 0.7 0.69999999999999996
7 0.8 0.79999999999999993
8 0.9 0.89999999999999991
9 1.0 0.99999999999999989
Quote:
Quote:
Quote:
>>>
Read this: http://docs.python.org/tut/node16.html

HTH,
John
  #3  
Old October 8th, 2007, 05:15 PM
Zentrader
Guest
 
Posts: n/a
Default Re: Really basic problem

You can use Python's decimal class if floating point arithmetic is not
exact enough

import decimal
status = decimal.Decimal( 0 )
for i in range(10):
status += decimal.Decimal( "0.1" )
if status == decimal.Decimal( "0.1" ):
print status
elif status == decimal.Decimal( "0.2" ):
print status
elif status == decimal.Decimal( "0.3" ):
print status
elif status == decimal.Decimal( "0.4" ):
print status
elif status == decimal.Decimal( "0.5" ):
print status
elif status == decimal.Decimal( "0.6" ):
print status
elif status == decimal.Decimal( "0.7" ):
print status
elif status == decimal.Decimal( "0.8" ):
print status
elif status == decimal.Decimal( "0.9" ):
print status
elif status == decimal.Decimal( "1.0" ):
print status
else:
print "status not equal -->", status

  #4  
Old October 8th, 2007, 05:25 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: Really basic problem

Zentrader wrote:
Quote:
You can use Python's decimal class if floating point arithmetic is not
exact enough
This is a misleading statement. While it's true that decimal can be more
precise in the sense that smaller fractions are representable, the
underlying problem of certain values not being representable properly &
leading to rounding errors still exists:


Quote:
Quote:
Quote:
>>import decimal
>>d = decimal.Decimal
>>d("1") / d("3")
Decimal("0.3333333333333333333333333333")
Quote:
Quote:
Quote:
>>otrd = d("1") / d("3")
>>otrd * 3
Decimal("0.9999999999999999999999999999")
Quote:
Quote:
Quote:
>>otrd * d("3")
Decimal("0.9999999999999999999999999999")
Quote:
Quote:
Quote:
>>>

Diez
  #5  
Old October 11th, 2007, 12:45 AM
=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
Guest
 
Posts: n/a
Default Re: Really basic problem

tomamil wrote:
Quote:
i know this example is stupid and useless, but that's not the answer
to my question.
here it goes:
>
status = 0.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:
print 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
>
the expected output:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
>
but it gives me instead:
0.1
0.2
0.4
0.5
0.6
0.7
>
why?
>
thanks,
>
m.
>
Replace : status = status + 0.1
with : status = round(status + 0.1, 1)


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles