Given two decimal numbers, is it possible to estimate the number of decimal places required to fit the result of their division? Provided that the division yields a finite number of decimals, of course.
For example:
1234.5678/2=617.2839, 4 decimal places required1234.5678/4=308.64195, 5 decimal places required1234.5678/8=154.320975, 6 decimal places required1234.5678/6.4=192.90121875, 8 decimal places required
By estimate, I don't necessarily need the exact number of decimals in the result, but a number of decimals at least equal to the required amount, so that it is guaranteed that the result fits.
What I've tried
I was able to roughly solve my problem using rational numbers & prime factorization, but this is very compute expensive. Here are the steps:
- Take the original division:
1234.5678 / 6.4 - Convert it to a rational number:
12345678 / 64000 - Simplify this fraction using the GCD of the two numbers:
6172839 / 32000 - Take the denominator:
32000 - Compute the factors of
2and5by dividing successively by these two numbers:
32000= 28 * 53 - (if it is found at this step that the number has other factors than
2and5, then stop here: the division yields an infinite number of digits) - Take the maximum of the two exponents:
max(8,3) = 8 - ⇒ 8 decimal places is enough to fit the result of the division.
How I came to the conclusion above
Out of all the prime numbers, only dividing by 2 and 5 yields a finite number of digits.
Each division by 10 extends the scale of the decimal number by 1 digit.
Each combination of 2 and 5 yields a 10, so an extra digit.
In 2x * 5y, there are min(x,y) times 10.
Now each division by 2 or 5 can potentially (although not always) require an extra digit. So I will carefully add an extra digit for each remaining 2 or 5 factor:
Maximum required digits = min(x,y) + (x - min(x,y)) + (y - min(x,y))
Which simplifies to: x + y - min(x,y)
Which further simplifies to max(x,y).
I feel like my approach, although it works, is overly complex. The direct consequence on my software is the slowness of the algorithm.
Is there a more straightforward approach to estimating the number of decimal places required for the result of the division to fit?
Note that I've read this question: Number of decimal places to be considered in division but it didn't help.
1234.5678 / 6.4, which is12345678 / 64000, I will actually divide1234567800000000 / 64000(adding8zeros to the numerator), so I can be sure that this division will have no remainder. The result,19290121875, will be associated with the scale8to form the decimal192.90121875. I need to compute this scale before the division, as fast as possible. – BenMorel Jun 26 '15 at 22:201234.5678by6.4rounded to 2 decimals, then easy, I'll divide internally1234567800 / 64000and retain the quotient. If you don't hint me on the scale of the result, then I need to do some extra computation to pre-calculate how many zeros I have to add to the numerator before doing the division. And I'm trying to make this computation faster, assuming that there might be a cleverer method than mine. Hence this question. – BenMorel Jun 26 '15 at 23:35