| | |
Python 2.3.7 data type double
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 148
Reputation:
Solved Threads: 32
It gets weirder. Check this out
What's up with that?
python Syntax (Toggle Plain Text)
a = 3.333333 round(a,2) # output> 3.3300000000000001 a = round(a,2) a # output> 3.3300000000000001 print a # output> 3.33
What's up with that?
•
•
Join Date: Dec 2006
Posts: 1,056
Reputation:
Solved Threads: 298
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.
Python Syntax (Toggle Plain Text)
import decimal x = decimal.Decimal("1") y = decimal.Decimal("3") print x / y ## set precision to 3 decimal.getcontext().prec = 3 ## set rounding type decimal.getcontext().rounding=decimal.ROUND_HALF_EVEN print x / y
Last edited by woooee; Jan 29th, 2009 at 11:00 pm.
•
•
Join Date: Dec 2006
Posts: 1,056
Reputation:
Solved Threads: 298
•
•
•
•
why not go for Python 2.5 ?
•
•
Join Date: Dec 2006
Posts: 1,056
Reputation:
Solved Threads: 298
This is the first hit for a Google http://www.taniquetil.com.ar/facundo...t_decimal.html
![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- Read Binary data from file (Python)
- My first Python program, constructive critisism anyone? (Python)
- noob: What and when you would use Decimal vs Float Type (Python)
Other Threads in the Python Forum
- Previous Thread: Export to dbf from ms access mdb
- Next Thread: Folder Structure
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color converter dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






