Python's built-in float type has double precision (it's a C double in CPython, a Java double in Jython). If you need more precision, get NumPy and use its numpy.float128.
Python's built-in float type has double precision (it's a C double in CPython, a Java double in Jython). If you need more precision, get NumPy and use its numpy.float128.
Decimal datatype
- Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem.
If you are pressed by performance issuses, have a look at GMPY
TIL a Python float is the same (precision) as a Java double
How do I convert a string to a double in Python? - Stack Overflow
Datatypes: get back 80-bit long double, 80-bit float support in general,
[deleted by user]
Videos
TL;DR in Java a "double" is a 64-bit float and a "float" is a 32-bit float; in Python a "float" is a 64-bit float (and thus equivalent to a Java double). There doesn't appear to be a natively implemented 32-bit float in Python (I know numpy/pandas has one, but I'm talking about straight vanilla Python with no imports).
In many programming languages, a double variable type is a higher precision float and unless there was a performance reason, you'd just use double (vs. a float). I'm almost certain early in my programming "career", I banged my head against the wall because of precision issues while using floats thus I avoided floats like the plague.
In other languages, you need to type a variable while declaring it.
Java: int age=30
Python: age=30
As Python doesn't have (or require?) typing a variable before declaring it, I never really thought about what the exact data type was when I divided stuff in Python, but on my current project, I've gotten in the habit of hinting at variable type for function/method arguments.
def do_something(age: int, name: str):
I could not find a double data type in Python and after a bunch of research it turns out that the float I've been avoiding using in Python is exactly a double in Java (in terms of precision) with just a different name.
Hopefully this info is helpful for others coming to Python with previous programming experience.
P.S. this is a whole other rabbit hole, but I'd be curious as to the original thought process behind Python not having both a 32-bit float (float) and 64-bit float (double). My gut tells me that Python was just designed to be "easier" to learn and thus they wanted to reduce the number of basic variable types.
>>> x = "2342.34"
>>> float(x)
2342.3400000000001
There you go. Use float (which behaves like and has the same precision as a C,C++, or Java double).
The decimal operator might be more in line with what you are looking for:
>>> from decimal import Decimal
>>> x = "234243.434"
>>> print Decimal(x)
234243.434