Example #1
0
        /// <summary>
        /// Multiplies the specified <see cref="Fraction"/> by nonnegative integer number.
        /// </summary>
        /// <param name="fraction">The multiplicand.</param>
        /// <param name="number">The multiplier.</param>
        /// <returns>The product of <paramref name="fraction"/> and <paramref name="number"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="fraction"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="number"/> is negative.</exception>
        public static Fraction operator *(Fraction fraction, long number)
        {
            ThrowIfArgument.IsNull(nameof(fraction), fraction);
            ThrowIfArgument.IsNegative(nameof(number), number, "Number is negative.");

            return(new Fraction(fraction.Numerator * number,
                                fraction.Denominator));
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Fraction"/> with the specified
        /// numerator and denominator.
        /// </summary>
        /// <param name="numerator">The numerator of a fraction.</param>
        /// <param name="denominator">The denominator of a fraction.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="numerator"/> is negative. -or-
        /// <paramref name="denominator"/> is zero or negative.</exception>
        public Fraction(long numerator, long denominator)
        {
            ThrowIfArgument.IsNegative(nameof(numerator), numerator, "Numerator is negative.");
            ThrowIfArgument.IsNonpositive(nameof(denominator), denominator, "Denominator is zero or negative.");

            // Simplify a fraction

            var greatestCommonDivisor = MathUtilities.GreatestCommonDivisor(numerator, denominator);

            numerator   /= greatestCommonDivisor;
            denominator /= greatestCommonDivisor;

            Numerator   = numerator;
            Denominator = denominator;
        }