I have the following double calculation in Java:
double C1 = some value; // It's always an integer
double C2 = some value; // It's always integer, and C2 >= C1
double S = some value; // It's always 0 < S <= 1
double carry = some value; // It's always 0 <= carry < 1
double rate = ((C1/C2)/S);
double C3 = Math.floor( (rate * S * C2) + carry );
Now, if we did not lose precision, C3 == C1
would be true. But since we lose precision, will C3
and C1
still always be equal?
If they would not always be equal, if I have C1
, C2
, S
, and C3
, how could I modify the rate
to make sure after calculation C3
would be equal to C1
?
Note: unfortunately using BigDecimal is not an option.
A double is a 64 bit IEEE 754 floating-point. Double can provide precision up to 15 to 16 decimal points.
We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places.
doubles are not exact. It is because there are infinite possible real numbers and only finite number of bits to represent these numbers.
Generally, we don't compare two double values directly through ==
. Instead, we measure the difference between them. e.g. Math.abs(C3 - C1) < 1e-6
.
Not so.
Floating point is an approximation. The next double, bit 1 added to the mantissa can cause a quite large gap for large exponents between those two doubles.
S approximating 0 will cause huge doubles, that have a large gap between them. That will no longer be correctable, say with that simple rounding. Even if the small S does not cause the division to exceed the double range with INFINITY.
One can easily be of by 1000.
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