I have a collection of many rational numbers, with the numerator and denominator of each stored as a large (hundreds or thousands of bits) unsigned integer. I'd like to be able to efficiently test whether any given rational number a/b
in the set is equal to any other rational number c/d
in the set.
The most straightforward method would be to test whether a*d == b*c
, of course, but I'd like something more efficient than computing the full products.
Some notes on my particular use-case:
I think this may be theoretically impossible, but throwing it out to the hive mind just in case.
Two rational numbers are said to be equivalent if their lowest forms are the same. For example, 2/3 and 18/27 are equivalent because when these are reduced to their lowest forms, they give the same value. These numbers are the same as equivalent fractions, such as 1/2, 2/4, and 4/6.
To compare any two rational numbers, we follow the following steps: Step 1: Express each of the two given rational numbers with a positive denominator. Step 2: Take the LCM of these positive denominators. Step 3: Express each rational number obtained in step 1 with this LCM as the common denominator.
Clearly, the given rational numbers have the same standard form. Therefore, the given rational numbers 14−35 and −2665 are equal.
You can filter out many not equal pairs of fractions by comparing the bit-lengths. Let l(a) = floor(log2(a)) + 1 the bit-length of a. If a/b = c/d than l(a) + l(d) = l(c) + l(b).
You can use this for a speedup when you are first comparing the lengths and comparing the products, only if the sum of the lengths are equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With