I am interested in understanding the maximum number of fractional decimal digits required for exact division.
The Java BigDecimal implementation states (and implements):
If the quotient this/divisor has a terminating decimal expansion, the expansion can have no more than
(a.precision() + ceil(10*b.precision)/3)
digits.
There is a typo/parenthesis error in the Java BigDecimal source comment. It should read
(a.precision() + ceil(10*b.precision/3))
In this context precision is used as in computer representations of floating point numbers (e.g. IEEE Standard for Floating-Point Arithmetic 754-2008). It can be thought of as the number of digits in the significand/coefficient/mantissa s when a terminating decimal is written in base 10 scientific notation s * 10e where s has been normalized to remove all leading and trailing zeros.
Here is a snippet of the code in question:
/* * If the quotient this/divisor has a terminating decimal * expansion, the expansion can have no more than * (a.precision() + ceil(10*b.precision)/3) digits. * Therefore, create a MathContext object with this * precision and do a divide with the UNNECESSARY rounding * mode. */ MathContext mc = new MathContext( (int)Math.min(this.precision() + (long)Math.ceil(10.0*divisor.precision()/3.0), Integer.MAX_VALUE), RoundingMode.UNNECESSARY); BigDecimal quotient; try { quotient = this.divide(divisor, mc); } catch (ArithmeticException e) { throw new ArithmeticException("Non-terminating decimal expansion; " + "no exact representable decimal result."); }
This links to the BigDecimal source code:
https://github.com/frohoff/jdk8u-dev-jdk/blob/da0da73ab82ed714dc5be94acd2f0d00fbdfe2e9/src/share/classes/java/math/BigDecimal.java#L1674
I found a reference to Java BigDecimal in the answer provided by @siefca to
How to estimate the number of decimal places required for a division?
but, there is no explanation as to how it is derived.
Q: How is this formula for the maximum number of digits in an exact decimal division derived?
.precision
that you reference, so we should try and give Readers a mathematical definition for it. – hardmath Apr 02 '23 at 01:33