Exemple #1
0
        /// <summary>
        /// Compute the natural logarithm of x to a given scale, x > 0.
        /// </summary>
        public static DecimalX Ln(this DecimalX x, int scale)
        {
            // Check that scale > 0.
            if (scale <= 0)
            {
                throw new ArgumentException(InvalidScale2);
            }

            // Check that x > 0.
            if (x.Signum() <= 0)
            {
                throw new ArgumentException(NegativeOrZeroNaturalLog);
            }

            // The number of digits to the left of the decimal point.
            var magnitude = x.ToString().Length - -x.Exponent - 1;

            if (magnitude < 3)
            {
                return(lnNewton(x, scale));
            }

            // x^(1/magnitude)
            var root = x.IntRoot(magnitude, scale);

            // ln(x^(1/magnitude))
            var lnRoot = lnNewton(root, scale);

            // magnitude*ln(x^(1/magnitude))
            var a      = DecimalX.Create(magnitude);
            var result = a.Multiply(lnRoot);

            return(DecimalX.Rescale(result, -scale, RoundingMode.HalfEven));
        }