Is it possible to establish, even roughly, what the maximum precision loss would be when dealing with two double
values in java (adding/subtracting)? Probably the worst case scenario is when two numbers cannot be represented exactly, and then an operation is performed on them, which results in a value that also cannot be represented exactly.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The worst case is that all precision can be lost. This can for example happen if the result is larger than the largest representable finite number. Then it will be stored as POSITIVE_INFINITY (or NEGATIVE_INFINITY).
Regarding your update, it can happen with addition.
Result:
See it online: ideone
In general the size of the representation error is relative to the size of your numbers.
Have a look at
Math.ulp(double)
. The ulp of adouble
is the delta to the next highest value. For instance, if you add to numbers and one is smaller than the ulp of the other, you know that the addition will have no effect. If you multiply two doubles, you can multiply their ulps to get the maximum error of the result.You could have a look at the actual precision of your inputs, for example the code below outputs:
You would still need to then estimate the loss on the result of your operation. And that does not work with corner cases (around Infinity, NaN etc.).