/// <summary> /// The subtraction operator /// </summary> /// <param name="fractA">The first fraction.</param> /// <param name="fractB">The second fraction.</param> /// <returns><paramref name="fractB"/> substracted from <paramref name="fractA"/>.</returns> public static Fraction operator -(Fraction fractA, Fraction fractB) { //addition fractA = fractA.Simplify(); // simplifying fractB = fractB.Simplify(); if (fractA.Denominator == fractB.Denominator) { var fraction = new Fraction //creating a new fraction { Numerator = fractA.Numerator - fractB.Numerator, // subtracting the nominators Denominator = fractA.Denominator // assigning the denominator }; return(fraction.Simplify()); // a simplified version of the fraction } var gdc = Numbers.Get.Gdc(fractA.Denominator, fractB.Denominator); if (gdc == 1) { // if the denominators are coprimes, i.e. no factors in common var fraction = new Fraction { Numerator = fractA.Numerator * fractB.Denominator - fractB.Numerator * fractA.Denominator, Denominator = fractA.Denominator * fractB.Denominator }; return(fraction.Simplify()); // a simplified version of the fraction } if (fractA.Denominator > fractB.Denominator) //fractA.Denominator will be the new denominator { var fraction = new Fraction { Numerator = fractA.Numerator - fractB.Numerator * gdc, Denominator = fractA.Denominator }; return(fraction.Simplify()); } else //fractB.Denominator will be the new denominator { var fraction = new Fraction { Numerator = fractB.Numerator - fractA.Numerator * gdc, Denominator = fractB.Denominator }; return(fraction.Simplify()); } }
/// <summary> /// Subtraction /// </summary> /// <param name="fractA"></param> /// <param name="fractB"></param> /// <returns></returns> public static Fraction operator -(Fraction fractA, Fraction fractB) { //addition fractA = fractA.Simplify(); // simplifying fractB = fractB.Simplify(); if (fractA.Denominator == fractB.Denominator) { var fraction = new Fraction //creating a new fraction { Numerator = fractA.Numerator - fractB.Numerator, // subtracting the nominators Denominator = fractA.Denominator // assigning the denominator }; return fraction.Simplify(); // a simplified version of the fraction } var gdc = Numbers.Get.Gdc(fractA.Denominator, fractB.Denominator); if (gdc == 1) { // if the denominators are coprimes, i.e. no factors in common var fraction = new Fraction { Numerator = fractA.Numerator * fractB.Denominator - fractB.Numerator * fractA.Denominator, Denominator = fractA.Denominator * fractB.Denominator }; return fraction.Simplify(); // a simplified version of the fraction } if (fractA.Denominator > fractB.Denominator) //fractA.Denominator will be the new denominator { var fraction = new Fraction { Numerator = fractA.Numerator - fractB.Numerator * gdc, Denominator = fractA.Denominator }; return fraction.Simplify(); } else //fractB.Denominator will be the new denominator { var fraction = new Fraction { Numerator = fractB.Numerator - fractA.Numerator * gdc, Denominator = fractB.Denominator }; return fraction.Simplify(); } }