public static Fractional operator +(Fractional f1, Fractional f2) { var gcd = GCD(f1.Denominator, f2.Denominator); var denominator = f1.Denominator / gcd * f2.Denominator; var nominator = checked(f1.Nominator * (f2.Denominator / gcd) + f2.Nominator * (f1.Denominator / gcd)); var result = new Fractional() { Nominator = nominator, Denominator = denominator }; result.Simplify(); return result; }
public static Fractional Calculate(params int[] coefficients) { Fractional frac = new Fractional() { Nominator = 1, Denominator = coefficients.Last() }; for (int i = coefficients.Length - 2; i >= 0; i--) { frac = (frac + new Fractional() { Nominator = coefficients[i], Denominator = 1 }).Reciprocal(); frac.Simplify(); } return frac.Reciprocal(); }
public void TestAdd(int nominator1, int denominator1, int nominator2, int denominator2, int resultNominator, int resultDenominator) { var sut1 = new Fractional() { Nominator = (BigInt)nominator1, Denominator = (BigInt)denominator1 }; var sut2 = new Fractional() { Nominator = (BigInt)nominator2, Denominator = (BigInt)denominator2 }; var result = sut1 + sut2; Assert.That((int)result.Nominator, Is.EqualTo(resultNominator)); Assert.That((int)result.Denominator, Is.EqualTo(resultDenominator)); }
public void TestSimplify(int nominator, int denominator, int resultNominator, int resultDenominator) { var sut = new Fractional() { Nominator = nominator, Denominator = denominator }; sut.Simplify(); Assert.That((int)sut.Nominator, Is.EqualTo(resultNominator)); Assert.That((int)sut.Denominator, Is.EqualTo(resultDenominator)); }