Are there data types with better precision than float?
And the reason the comparison succeeds with 1.5 is that 1.5 can be represented exactly as a float and as a double ; it has a bunch of zeros in its low bits, so when the promotion adds zeros the result is the same as the double representation.
Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np.
A variable of type float only has 7 digits of precision whereas a variable of type double has 15 digits of precision. If you need better accuracy, use double instead of float.
1.2 is a double (8 bytes). 1.2f is a float (4 bytes). Show activity on this post. Any literal number in your code which includes a decimal point is interpreted as a double , not a float , unless you mark it as a float by appending f .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With