Really basic problem

T

tomamil

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.
 
J

John Machin

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.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
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

tomamil said:
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)
 
Z

Zentrader

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
 
D

Diez B. Roggisch

Zentrader said:
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:




Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top