473,414 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 1043
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 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 <H1> 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.