I have to calculate the ratio between 0.000857179311146189 and 0.026955533883055983 but am unsure how to do this other than by dividing the two numbers. Is it possible to calculate this with the result in the form 0.001714 : 0.053912
Asked
Active
Viewed 1,185 times
-2
-
1Why do you want the ratio to be written in terms of those two specific numbers? Why, for instance, did you choose `0.001714` as the numerator rather than `0.000857` or `42` or `99.1`. Because if you're allowed to choose the numerator and denominator, then `print(str(a) + " : " + str(b))` will trivially find a ratio, but I doubt that's the result you want. – Silvio Mayolo Mar 12 '22 at 17:57
-
A ratio is a number, your last example is just a representation of a number. So, what else could it be other than the quotient of your 2 numbers? – Thierry Lathuille Mar 12 '22 at 17:58
-
The ratio of 0.000857179311146189 and 0.026955533883055983 is 0.000857179311146189:0.026955533883055983, not sure what you want here – Mr. Techie Mar 12 '22 at 17:59
-
Why not 902 : 28365? Smaller numerator and denominator (than in 1714 : 53912), and much more accurate. – Kelly Bundy Mar 12 '22 at 18:04
-
@SilvioMayolo is correct, why do you specifically choose `0.001714` as the numerator? Either way you can use `fractions.Fraction` in order to represent the ratio (`Fraction(857179311146189, 26955533883055983)`) – Bharel Mar 12 '22 at 18:04
2 Answers
0
Using the fractions
module:
from fractions import Fraction
num1 = Fraction(0.000857179311146189)
num2 = Fraction(0.026955533883055983)
ratio = Fraction(num1, num2).limit_denominator()
print(f"{ratio.numerator / 10**6} : {ratio.denominator / 10**6}")
# 0.027671 : 0.870164

Cubix48
- 2,607
- 2
- 5
- 17
-
Yeah, that's how I got my 902 : 28365, except I used their 53912 as the limit so that my fraction is better in all regards. – Kelly Bundy Mar 12 '22 at 18:15
0
I understand OP issue, but the 2 pair-numbers you provided is not the same ratio
Your problem is to find the greatest common divisor, for float
https://stackoverflow.com/a/45325587/3789481
def float_gcd(a, b, rtol = 1e-05, atol = 1e-08):
t = min(abs(a), abs(b))
while abs(b) > rtol * t + atol:
a, b = b, a % b
return a
x = 0.000857179311146189
y = 0.026955533883055983
div = float_gcd(x, y)
print(f"{x/div}:{y/div}")
#Result: 34462.78514615594:1083743.8102297517
I believe 34462.78514615594:1083743.8102297517
is the nearest ratio you would like to get, let's check
For int, we could use math library https://stackoverflow.com/a/65640573/3789481
import math
div = math.gcd(x, y)
print(f"{x/div}:{y/div}")

Tấn Nguyên
- 1,607
- 4
- 15
- 25
-
Why do you think 34462.78514615594 : 1083743.8102297517 is the nearest ratio? Surely 0.000857179311146189 : 0.026955533883055983 itself is the nearest (to itself)? – Kelly Bundy Mar 12 '22 at 18:19