Пример #1
0
        public static BigInteger Sqrt(BigInteger input)
        {
            if (input < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(input), ErrorMessages.MustBePositive);
            }

            if (input == 0)
            {
                return(0);
            }

            BigInteger t;

            try
            {
                t = NewtonRaphson.Iterate(x => (x * x) - input, x => 2 * x, 2147483648);
            }
            catch (IterationsExceededException <BigInteger> exc)
            {
                t = exc.LastValue;
            }

            if (t * t == input)
            {
                return(t);
            }

            --t;

            BigInteger prev = t * t;

            while (true)
            {
                ++t;

                BigInteger cur = t * t;

                if (cur < prev || cur > input)
                {
                    break;
                }
            }

            return(t - 1);
        }
Пример #2
0
        public static double Rate(double periods, double presentValue, double futureValue, double monthlyPayment, PaymentType type)
        {
            double t     = (int)type;
            double guess = ((monthlyPayment * periods) - presentValue) / presentValue;

            try
            {
                return(NewtonRaphson.Iterate(
                           (double r0) => (((presentValue * Math.Pow(1 + r0, periods)) - futureValue) / ((1 + (r0 * t)) * ((Math.Pow(1 + r0, periods) - 1) / r0))) - monthlyPayment,
                           (double r0) => ((futureValue * ((periods * r0 * r0 * t * Math.Pow(1 + r0, periods)) + (periods * r0 * Math.Pow(1 + r0, periods)) - (r0 * Math.Pow(1 + r0, periods)) - Math.Pow(1 + r0, periods) + r0 + 1)) + (presentValue * Math.Pow(1 + r0, periods) * ((-periods * r0 * r0 * t) + Math.Pow(1 + r0, periods) + (r0 * (Math.Pow(1 + r0, periods) - periods - 1)) - 1))) / ((r0 + 1) * (Math.Pow(1 + r0, periods) - 1) * (Math.Pow(1 + r0, periods) - 1) * ((r0 * t) + 1) * ((r0 * t) + 1)),
                           guess));
            }
            catch (IterationsExceededException <double> exc)
            {
                return(exc.LastValue);
            }
        }
Пример #3
0
        public static decimal Rate(int periods, decimal presentValue, decimal futureValue, decimal monthlyPayment, PaymentType type)
        {
            decimal t     = (int)type;
            decimal N0    = periods;
            decimal guess = ((monthlyPayment * periods) - presentValue) / presentValue;

            try
            {
                return(NewtonRaphson.Iterate(
                           (decimal r0) => (((presentValue * DecimalMath.Pow(1M + r0, periods)) - futureValue) / ((1M + (r0 * t)) * ((DecimalMath.Pow(1M + r0, periods) - 1) / r0))) - monthlyPayment,
                           (decimal r0) => ((futureValue * ((N0 * r0 * r0 * t * DecimalMath.Pow(1M + r0, periods)) + (periods * r0 * DecimalMath.Pow(1M + r0, periods)) - (r0 * DecimalMath.Pow(1M + r0, periods)) - DecimalMath.Pow(1M + r0, periods) + r0 + 1)) + (presentValue * DecimalMath.Pow(1M + r0, periods) * ((-N0 * r0 * r0 * t) + DecimalMath.Pow(1M + r0, periods) + (r0 * (DecimalMath.Pow(1 + r0, periods) - periods - 1)) - 1))) / ((r0 + 1) * (DecimalMath.Pow(1 + r0, periods) - 1) * (DecimalMath.Pow(1 + r0, periods) - 1) * ((r0 * t) + 1) * ((r0 * t) + 1)),
                           guess));
            }
            catch (IterationsExceededException <decimal> exc)
            {
                return(exc.LastValue);
            }
        }
Пример #4
0
        private static ulong SqrtInternal(ulong input)
        {
            if (input == 0)
            {
                return(0);
            }

            double i = input;
            double value;

            try
            {
                value = NewtonRaphson.Iterate(x => (x * x) - i, x => 2.0 * x, 2147483648);
            }
            catch (IterationsExceededException <double> exc)
            {
                value = exc.LastValue;
            }

            ulong t = (ulong)value;

            while (t * t < t)
            {
                --t;
            }

            ulong prev = t * t;

            while (true)
            {
                ++t;

                ulong cur = t * t;

                if (cur < prev || cur > input)
                {
                    break;
                }
            }

            return(t - 1);
        }
Пример #5
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);
            }
        }
Пример #6
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);
            }
        }