Are there data types with better precision than float?
-
5This is actually two questions with different answers. 1: double precision values in python are floats, and 2: a better precision data type than float would be decimal. Can questions like this be split somehow? The accepted answer addresses #2, but most upvoted answer addresses #1.– bsplosionMar 15, 2019 at 14:30
5 Answers
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
.
-
21Apparently,
numpy.float128
often has 64 bit precision on a 64 bit system.numpy.float128(1) + numpy.float128(2**-64) - numpy.float128(1)
returns0.0
. See stackoverflow.com/a/29821557/420755– JeffFeb 24, 2016 at 18:52 -
10.1 + 0.2 is not exact 0.3 in python, in every other language this is a float problem but never a double problem. Why it is not exact 0.3 in python? Dec 29, 2020 at 4:23
-
4@FuscaSoftware It's a problem in other languages too. IEEE doubles can't represent 0.3 exactly. That sounds like a formatting artifact. Dec 29, 2020 at 7:29
-
Funnily enough this was not mentioned in their documentation page, or any other page that speaks about "float precision" in Python. Jan 18 at 21:42
- 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
-
If I were asking the original question, then @larsmans's would be the answer (even though formally it's off-topic). Feb 6, 2016 at 14:25
-
-
No idea, but now I'd refer to @FredFoo. Generally "Double precision floating values in Python?" → "
float
-s are double precision" Jun 13, 2017 at 10:00
For some applications you can use Fraction
instead of floating-point numbers.
>>> from fractions import Fraction
>>> Fraction(1, 3**54)
Fraction(1, 58149737003040059690390169)
(For other applications, there's decimal
, as suggested out by the other responses.)
-
2how do I choose between Decimal and Fraction? Fraction seems better since it can represent continuing fractions which I guess Decimal can't? Nov 26, 2012 at 13:44
-
2@Janus: consider your requirements, and pick the one that fits them better. Use
Decimal
when you want to work with approximate numbers that have fixed (but configurable) precision. UseFraction
when you want to work with exact ratios, and are prepared to put up with their unbounded storage requirements. Nov 26, 2012 at 17:10 -
May be you need Decimal
>>> from decimal import Decimal
>>> Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')
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..
import random
from decimal import Decimal
GndSpeedHigh = float(format(Decimal(random.uniform(5, 25)), '.2f'))
GndSpeedLow = float(format(Decimal(random.uniform(2, GndSpeedHigh)), '.2f'))
GndSpeedMean = float(Decimal(format(GndSpeedHigh + GndSpeedLow) / 2, '.2f')))
print(GndSpeedMean)
-
2Well. Double precision means double length binary representation of variable compared to binary representation of float. Not two decimal places.– kravemirMar 31, 2015 at 15:27
-
You are right. I might have used bad search criteria when I had this problem myself and this is the outcome. Some others might search this same issue like I did and end up here. Hopefully they find some relief. :) Apr 2, 2015 at 6:18
-
2This is the worst way I can think of to round a float to two decimal places. You should at least use
numpy.floor(100*a)/100
to truncate a numbera
to two decimal places.– WauzlDec 16, 2020 at 10:59