public Rational Add(Rational val) { BigInteger num = Numerator.Multiply(val.Denominator).Add(Denominator.Multiply(val.Numerator)); BigInteger deno = Denominator.Multiply(val.Denominator); return(new Rational(num, deno)); } /* Rational.add */
public Rational Multiply(Rational val) { BigInteger num = Numerator.Multiply(val.Numerator); BigInteger deno = Denominator.Multiply(val.Denominator); /* Normalization to an coprime format will be done inside * the ctor() and is not duplicated here. */ return(new Rational(num, deno)); }
public Rational Divide(Rational val) { if (val.CompareTo(Zero) == 0) { throw new DivideByZeroException(); } BigInteger num = Numerator.Multiply(val.Denominator); BigInteger deno = Denominator.Multiply(val.Numerator); /* Reduction to a coprime format is done inside the ctor, * and not repeated here. */ return(new Rational(num, deno)); }
public Rational Add(int val) { BigInteger val2 = Numerator.Add(Denominator.Multiply(BigInteger.Parse("" + val))); return(new Rational(val2, Denominator)); }