What is the best method for adding fractions?

404 views Asked by At

What is the most efficient method when adding fractions and subsequently reducing by dividing by the GCD?

Method 1:

a/b + c/d = (a*d + b*c) / b*d

Method 2:

x = lcm(b, d)
[a * (x / b) + c * (x / d)] / x
1

There are 1 answers

0
Yakk - Adam Nevraumont On

Method 3:

x = gcd(b, d)
[a * (d/x) + c * (b/x)] / (b/x*d)

which minimizes the chance of overflow by reducing the magnitude of the values in the calculation.

Overflow checks need still be done.

Even with infinite precision values, the above may reduce allocation costs (bigger numbers use more memory).

Oh, and profile,