Is there an algorithm to compare two numeric expressions that follow these rules:
The comparison in question is comparison of the resulting multiplication products - which is larger?
An example of two such expressions is: (7^21)*(2^49) and (5^26)*(3^31).
The idea is to compare such expressions with the powers being quite large, up to 2^64.
As commented, you can use the log() function and create a compare method:
import math
def _cmp(A, B):
asum = sum(a * math.log(p) for p, a in A)
bsum = sum(b * math.log(q) for q, b in B)
if asum > bsum:
return 'A > B'
elif asum < bsum:
return 'A < B'
else:
return 'A = B'
A = [(7, 21), (2, 49)]
B = [(5, 26), (3, 31)]
print(_cmp(A, B))
A < B
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