示例#1
0
        private static DecimalX lnNewton(DecimalX x, int scale)
        {
            DecimalX term;

            var sp1 = scale + 1;
            var n   = x;

            // Convergence tolerance = 5*(10^-(scale+1))
            var tolerance = DecimalX.Create(5);

            tolerance = tolerance.MovePointLeft(sp1);

            // Loop until the approximations converge
            // (two successive approximations are within the tolerance).
            do
            {
                // e^x
                var eToX = x.Exp(sp1);

                // (e^x - n)/e^x
                term = eToX.Subtract(n).CDivide(eToX, sp1, RoundingMode.Down);

                // x - (e^x - n)/e^x
                x = x.Subtract(term);
            } while (term.CompareTo(tolerance) > 0);

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