private static Fraction Multiply(Fraction left, Fraction right) { if (left.IsNaN() || right.IsNaN()) { return(NaN); } CrossReducePair(ref left, ref right); try { checked { var numerator = left.Numerator * right.Numerator; var denominator = left.Denominator * right.Denominator; return(new Fraction(numerator, denominator)); } } catch (Exception) { //throw new FractionException("Multiply error", ex); return(new Fraction(left.ToDouble() * right.ToDouble())); } }
private static Fraction Add(Fraction left, Fraction right) { if (left.IsNaN() || right.IsNaN()) { return(NaN); } var gcd = GCD(left.Denominator, right.Denominator); // nie może zwrócić mniej niż 1 var leftDenominator = left.Denominator / gcd; var rightDenominator = right.Denominator / gcd; try { checked { var numerator = left.Numerator * rightDenominator + right.Numerator * leftDenominator; var denominator = leftDenominator * rightDenominator * gcd; return(new Fraction(numerator, denominator)); } } catch (Exception) { //throw new FractionException("Error dodawania", ex); return(new Fraction(left.ToDouble() + right.ToDouble())); // przybliż ekstremalne przypadki } }
private static Fraction Modulus(Fraction left, Fraction right) { if (left.IsNaN() || right.IsNaN()) { return(NaN); } try { checked { var quotient = (int)(left / right); var whole = new Fraction(quotient * right.Numerator, right.Denominator); return(left - whole); } } catch (Exception) { //throw new FractionException("Modulus error", ex); return(new Fraction(left.ToDouble() % right.ToDouble())); } }