Example #1
0
        public static Fraction Simplify(Fraction fraction)
        {
            fraction = Normalize(fraction);

            long num = fraction.Numerator;
            long den = fraction.Denominator;

            long g = IntegerMath.GreatestCommonDivisor(num, den);

            num /= g;
            den /= g;

            return(new Fraction(num, den));
        }
Example #2
0
        public static decimal Root(decimal value, long root)
        {
            if (IntegerMath.IsPowerOfTwo(root) && value < 0.0M)
            {
                throw new ArgumentOutOfRangeException(nameof(value), ErrorMessages.MustBePositive);
            }
            else if (value == 0.0M)
            {
                return(0.0M);
            }

            try
            {
                return(NewtonRaphson.Iterate(x => PowSquareLaw(x, root) - value, x => root * PowSquareLaw(x, root - 1), rounding: null));
            }
            catch (IterationsExceededException <decimal> exc)
            {
                return(exc.LastValue);
            }
        }
Example #3
0
        /// <summary>
        /// <para>Defines root as a function</para>
        /// <para>i.e. Root(100, 2) = 10 and Root(8, 3) == 2</para>
        /// </summary>
        /// <param name="value">The value to take the root of</param>
        /// <param name="root">The root to apply</param>
        /// <returns>The root of the value</returns>
        public static double Root(double value, int root)
        {
            if (IntegerMath.IsPowerOfTwo(root) && value < 0.0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), ErrorMessages.MustBePositive);
            }
            else if (value == 0.0)
            {
                return(0.0);
            }

            try
            {
                return(NewtonRaphson.Iterate(x => Math.Pow(x, root) - value, x => root * Math.Pow(x, root - 1), rounding: null));
            }
            catch (IterationsExceededException <double> exc)
            {
                return(exc.LastValue);
            }
        }
Example #4
0
 public static bool IsSimplified(Fraction fraction)
 {
     return(IntegerMath.GreatestCommonDivisor(fraction.Numerator, fraction.Denominator) == 1);
 }