471,317 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

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.

Oct 8 '07 #1
4 977
On 8/10/2007 7:39 PM, tomamil wrote:
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:
>>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
>>>
Read this: http://docs.python.org/tut/node16.html

HTH,
John
Oct 8 '07 #2
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

Oct 8 '07 #3
Zentrader wrote:
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:
>>import decimal
d = decimal.Decimal
d("1") / d("3")
Decimal("0.3333333333333333333333333333")
>>otrd = d("1") / d("3")
otrd * 3
Decimal("0.9999999999999999999999999999")
>>otrd * d("3")
Decimal("0.9999999999999999999999999999")
>>>

Diez
Oct 8 '07 #4
tomamil wrote:
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)
Oct 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by John | last post: by
7 posts views Thread by Michael Foord | last post: by
38 posts views Thread by Martin Marcher | last post: by
3 posts views Thread by sefe dery | last post: by
131 posts views Thread by pemo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.