Do you have an algorithm to deal with ratio and percentage for integers?
Closed this issue · 5 comments
I find situations like percentages very common, but using floating pointers to print them out would be very costly.
Do you have an algorithm to deal with that?
One without precision:
For example, giving input (3,10)
Output:
30%
or
0.3
For example, giving input (3,9)
Output:
33.(3)%
or
0.(3)
For example, giving input (2,14)
Output:
14.(285714)%
or
0.(142857)
Also, with precision version (which is very common in statistics, like keeping two digits decimals):
For example, giving input (2,14), precision 2
Output:
14.26%
or
0.14
Well. I just realized precision is only possible with a few possibilities.
Maybe a roundtrip here is needed too?
For example, giving input (3,10)
Output:
30%
or
0.3
For example, giving input (3,9)
Output:
33.(3)%
or
0.(3)
For example, giving input (2,14)
Output:
14.(285714)%
or
0.(142857)
If you are looking for finding the exact decimal representation in terms of the non-repeating and the repeating digits, it's nothing but Euler's theorem. Basically, what it says is that given a pair of coprime integers
Here is the explanation on why that's relevant here. Let's say we want to compute
Clearly
is an integer and we have
Next, the factor
where
then if you multiply
and then subtract
then the result must be an integer because
which means all the digits in the fractional parts cancel each other. Hence, you should have
-
$g$ is precisely the non-repeating digits, -
$h$ is precisely the repeating digits, -
$e$ is the period of repetition, and the number of digits in$h$ might be strictly smaller than$e$ in which case there are leading zeros in the repeated digits, - and then you have to shift every digit to right by
$k$ digit, where$k$ is the larger one between the number of factors of$2$ or$5$ in$q$ .
Well, it doesn't sound simple, right? Especially, I don't think there is a blazingly fast method for finding the smallest exponent
My conclusion is this: I haven't thought about this subject more than I wrote here and also don't know about any existing literature on it, but it sounds like a decent research question. It sounds like at least as hard as the arbitrary-precision formatting of floating-point numbers, if not harder.
Also, with precision version (which is very common in statistics, like keeping two digits decimals):
For example, giving input (2,14), precision 2
Output:
14.26%
or
0.14
Again, if the divisor is not an invariant constant, I do not think there are lots of things we can do with that. Floating-point case was easier because the divisor is indeed a known constant, powers of
I'm closing this as it sounds like an out of scope issue, but feel free to reopen if you think it's relevant.
Also, with precision version (which is very common in statistics, like keeping two digits decimals):
For example, giving input (2,14), precision 2
Output:
14.26%
or
0.14Again, if the divisor is not an invariant constant, I do not think there are lots of things we can do with that. Floating-point case was easier because the divisor is indeed a known constant, powers of 2 or 10.
I'm closing this as it sounds like an out of scope issue, but feel free to reopen if you think it's relevant.
what about just 2 decimal places for percentages for example? It is very common in academic research.
what about just 2 decimal places for percentages for example? It is very common in academic research.
Just multiply 100 to the numerator and divide by the denominator then.
EDIT: Well, actually, if you want the correct round-to-nearest, tie-to-even behavior, then you may need to do a little bit more, like multiply 200 and add 1 and things like that. But basically that's that.