Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2005, 12:54 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Unhappy Question about list comprehension

I worked on a tutorial about list comprehension and this happened (Python v2.3) ...
# a list of approximated pi values rounded off using list comprehension
# is supposed to give:
# ['3.1', '3.14', '3.142', '3.1416', '3.14159']
# but gives:
# ['3.1000000000000001', '3.1400000000000001', '3.1419999999999999',
#  '3.1415999999999999', '3.1415899999999999']
piList = [repr(round(355/113.0, k)) for k in range(1,6)]
print piList
# this however gives the right result = 3.14159
print round(355/113.0, 5)
Any reasons?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 15th, 2005, 4:27 PM   #2
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
My first guess would be, that it has got something to do with the type of numbers you use. You could try it with long numbers...
I don't have a version of python available right now, So I can only guess
Would you mind explaining list comprehensions to me for starters???
Fred is offline   Reply With Quote
Old Mar 15th, 2005, 4:55 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Python likes its rounding errors - no idea why.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 15th, 2005, 8:58 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Quote:
Originally Posted by Fred
My first guess would be, that it has got something to do with the type of numbers you use. You could try it with long numbers...
I don't have a version of python available right now, So I can only guess
Would you mind explaining list comprehensions to me for starters???
This what the tutorial says about list comprehension, actually quite useful when working with lists.
list comprehension:
 
result = [expression for item1 in sequence1 [if condition1] 
         [for item2 in sequence2 ... for itemN in sequenceN]
         ]

is equivalent to: 

result = []
for item1 in sequence1:    
  for item2 in sequence2:
    ...
    for itemN in sequenceN:
      if (condition1) and further conditions:
        result.append(expression)
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC