You’ve exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can’t expect the least significant digit(s) of your result to actually be meaningful/precise.
If you need arbitrarily precise arithmetic in Java, consider using BigInteger
and BigDecimal
.