Python 2.3.7 data type double

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 10
Reputation: l_w is an unknown quantity at this point 
Solved Threads: 0
l_w l_w is offline Offline
Newbie Poster

Python 2.3.7 data type double

 
0
  #1
Jan 29th, 2009
Python 2.3.7 has the following functions as number converters: float(num), int(num), long(num). But what happened to double(num)?
If my number is a=3.33333 and do round(_,2), the output I get is 3.3300000000000001. I was expecting to see 3.33.
What’s happening to here?
Thank you.
liliya
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: Python 2.3.7 data type double

 
0
  #2
Jan 29th, 2009
It gets weirder. Check this out
  1. a = 3.333333
  2. round(a,2)
  3. # output> 3.3300000000000001
  4. a = round(a,2)
  5. a
  6. # output> 3.3300000000000001
  7. print a
  8. # output> 3.33

What's up with that?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: l_w is an unknown quantity at this point 
Solved Threads: 0
l_w l_w is offline Offline
Newbie Poster

Re: Python 2.3.7 data type double

 
0
  #3
Jan 29th, 2009
Thanks a bunch! I'm new to the language, trying to pick it up
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is online now Online
Veteran Poster

Re: Python 2.3.7 data type double

 
0
  #4
Jan 29th, 2009
This is a well know and well documented result on PCs due to the use of binary numbers. Our base 10 system also has problems (with numbers like 1/3 or Pi). The explaination at python.org is here http://www.python.org/doc/faq/genera...-so-inaccurate and http://www.lahey.com/float.htm A google for "floating point precision" or something similiar will yield more results. Most, if not all programming languages have add-ons for exact precision. In python it is the decimal module.
  1. import decimal
  2.  
  3. x = decimal.Decimal("1")
  4. y = decimal.Decimal("3")
  5. print x / y
  6.  
  7. ## set precision to 3
  8. decimal.getcontext().prec = 3
  9.  
  10. ## set rounding type
  11. decimal.getcontext().rounding=decimal.ROUND_HALF_EVEN
  12. print x / y
Last edited by woooee; Jan 29th, 2009 at 11:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: l_w is an unknown quantity at this point 
Solved Threads: 0
l_w l_w is offline Offline
Newbie Poster

Re: Python 2.3.7 data type double

 
0
  #5
Jan 30th, 2009
I'm going to look for/get this decimal library and looking at the links you posted here right now. Thank you for the post, this helps a good deal.
l_w
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: l_w is an unknown quantity at this point 
Solved Threads: 0
l_w l_w is offline Offline
Newbie Poster

Re: Python 2.3.7 data type double

 
0
  #6
Jan 30th, 2009
I'm not finding Python 2.3 decimal module on the web. Does anybody know where I could downloaded it?
Last edited by l_w; Jan 30th, 2009 at 2:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,451
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Python 2.3.7 data type double

 
0
  #7
Jan 30th, 2009
why not go for Python 2.5 ?
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is online now Online
Veteran Poster

Re: Python 2.3.7 data type double

 
0
  #8
Jan 30th, 2009
why not go for Python 2.5 ?
Yes, decimal was first included in Python 2.4 http://www.python.org/doc/2.5.2/lib/module-decimal.html On linux, upgrading is as simple as telling the package manager to upgrade. For MS Windows, http://python.org/download/releases/2.5.4/ ActiveState also has a version. Either version is fine. Also, you want to update when new releases come out for security reasons.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 10
Reputation: l_w is an unknown quantity at this point 
Solved Threads: 0
l_w l_w is offline Offline
Newbie Poster

Re: Python 2.3.7 data type double

 
0
  #9
Jan 30th, 2009
I'd like to have decimal module on 2.3.7 version, because the project I will work on in near future is developed in this version, other versions have compatibility problems.
So there is no decimal module for 2.3 anywhere?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is online now Online
Veteran Poster

Re: Python 2.3.7 data type double

 
0
  #10
Feb 1st, 2009
This is the first hit for a Google http://www.taniquetil.com.ar/facundo...t_decimal.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC