472,989 Members | 2,855 Online

# 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

0.1
0.2
0.4
0.5
0.6
0.7

why?

thanks,

m.

Oct 8 '07 #1
4 1032
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
>>>

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 10 by: John | last post by: I have a problem, it's not with any code I have because... there is no code. When I run a blank visual basic 6 form, it opens up just fine. When I add a text box, a caption, and a button... it... 7 by: Michael Foord | last post by: #!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #... 72 by: Mel | last post by: Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like

etc. and although there are tools... 4 by: Tat | last post by: Hello, I have a weird show stopper here. I created a Windows application (.NET 1.1). My dev. machine is not hooked up to the Internet, neither it has firewall. I installed the app on a... 38 by: Martin Marcher | last post by: Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more... 59 by: Alan Silver | last post by: Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at... 3 by: sefe dery | last post by: hi ng, i try to create a asp.net 1.0 website on windows server 2003(Servername: ServerX) with iis 6.0. PROBLEM: The user should login with his windows credentials in basic.aspx and... 131 by: pemo | last post by: Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write... 1 by: bruce | last post by: hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given... 4 by: ICPooreMan | last post by: I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 4 by: NeoPa | last post by: Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :... 3 by: NeoPa | last post by: Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all... 3 by: nia12 | last post by: Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of... 0 by: NeoPa | last post by: Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it... 0 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike... 4 by: GKJR | last post by: Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other... 3 by: SueHopson | last post by: Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...