/// <summary> /// Compare the <see cref="Fraction" /> with another fraction. /// </summary> /// <param name="other">The <see cref="Fraction" /> to compare against.</param> /// <returns>-1 if it is less, 0 if equal, 1 if it is greater.</returns> public int CompareTo(Fraction other) { var lcm = NumberTheory.LeastCommonMultiple(Denominator, other.Denominator); var leftNumerator = Numerator * lcm / Denominator; var rightNumerator = other.Numerator * lcm / other.Denominator; return(leftNumerator.CompareTo(rightNumerator)); }
public static Fraction operator-(Fraction left, Fraction right) { var newDenominator = NumberTheory.LeastCommonMultiple(left.Denominator, right.Denominator); var newNumerator = left.Sign * left.Numerator * newDenominator / left.Denominator - right.Sign * right.Numerator * newDenominator / right.Denominator; return(new Fraction(newNumerator, newDenominator)); }