Are there data types with better precision than float?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
May be you need Decimal
Floating Point Arithmetic
Python's built-in
float
type has double precision (it's a Cdouble
in CPython, a Javadouble
in Jython). If you need more precision, get NumPy and use itsnumpy.float128
.Here is my solution. I first create random numbers with random.uniform, format them in to string with double precision and then convert them back to float. You can adjust the precision by changing '.2f' to '.3f' etc..
For some applications you can use
Fraction
instead of floating-point numbers.(For other applications, there's
decimal
, as suggested out by the other responses.)Decimal datatype
If you are pressed by performance issuses, have a look at GMPY