当前位置: 首页>>代码示例>>C#>>正文


C# BigDecimal.Pow方法代码示例

本文整理汇总了C#中BigDecimal.Pow方法的典型用法代码示例。如果您正苦于以下问题:C# BigDecimal.Pow方法的具体用法?C# BigDecimal.Pow怎么用?C# BigDecimal.Pow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BigDecimal的用法示例。


在下文中一共展示了BigDecimal.Pow方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Hypot

        public static BigDecimal Hypot(int n, BigDecimal x)
        {
            /* compute n^2+x^2 in infinite precision
                */
            BigDecimal z = (new BigDecimal(n)).Pow(2).Add(x.Pow(2));

            /* Truncate to the precision set by x. Absolute error = in z (square of the result) is |2*x*xerr|,
                * where the error is 1/2 of the ulp. Two intermediate protection digits.
                * zerr is a signed value, but used only in conjunction with err2prec(), so this feature does not harm.
                */
            double zerr = x.ToDouble()*x.Ulp().ToDouble();
            var mc = new MathContext(2 + ErrorToPrecision(z.ToDouble(), zerr));

            /* Pull square root */
            z = Sqrt(z.Round(mc));

            /* Final rounding. Absolute error in the square root is x*xerr/z, where zerr holds 2*x*xerr.
                */
            mc = new MathContext(ErrorToPrecision(z.ToDouble(), 0.5*zerr/z.ToDouble()));
            return z.Round(mc);
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:21,代码来源:BigMath.cs

示例2: Sqrt

        public static BigDecimal Sqrt(BigDecimal x, MathContext mc)
        {
            if (x.CompareTo(BigDecimal.Zero) < 0)
                throw new ArithmeticException("negative argument " + x + " of square root");

            if (x.Abs().Subtract(new BigDecimal(System.Math.Pow(10d, -mc.Precision))).CompareTo(BigDecimal.Zero) < 0)
                return ScalePrecision(BigDecimal.Zero, mc);

            /* start the computation from a double precision estimate */
            var s = new BigDecimal(System.Math.Sqrt(x.ToDouble()), mc);
            BigDecimal half = BigDecimal.ValueOf(2);

            /* increase the local accuracy by 2 digits */
            var locmc = new MathContext(mc.Precision + 2, mc.RoundingMode);

            /* relative accuracy requested is 10^(-precision)
                */
            double eps = System.Math.Pow(10.0, -mc.Precision);
            while (true) {
                /* s = s -(s/2-x/2s); test correction s-x/s for being
                        * smaller than the precision requested. The relative correction is 1-x/s^2,
                        * (actually half of this, which we use for a little bit of additional protection).
                        */
                if (System.Math.Abs(BigDecimal.One.Subtract(x.Divide(s.Pow(2, locmc), locmc)).ToDouble()) < eps)
                    break;
                s = s.Add(x.Divide(s, locmc)).Divide(half, locmc);
            }

            return s;
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:30,代码来源:BigMath.cs

示例3: Root

        public static BigDecimal Root(int n, BigDecimal x)
        {
            if (x.CompareTo(BigDecimal.Zero) < 0)
                throw new ArithmeticException("negative argument " + x + " of root");
            if (n <= 0)
                throw new ArithmeticException("negative power " + n + " of root");

            if (n == 1)
                return x;

            /* start the computation from a double precision estimate */
            var s = new BigDecimal(System.Math.Pow(x.ToDouble(), 1.0/n));

            /* this creates nth with nominal precision of 1 digit
                */
            var nth = new BigDecimal(n);

            /* Specify an internal accuracy within the loop which is
                * slightly larger than what is demanded by 'eps' below.
                */
            BigDecimal xhighpr = ScalePrecision(x, 2);
            var mc = new MathContext(2 + x.Precision);

            /* Relative accuracy of the result is eps.
                */
            double eps = x.Ulp().ToDouble()/(2*n*x.ToDouble());
            for (;;) {
                /* s = s -(s/n-x/n/s^(n-1)) = s-(s-x/s^(n-1))/n; test correction s/n-x/s for being
                        * smaller than the precision requested. The relative correction is (1-x/s^n)/n,
                        */
                BigDecimal c = xhighpr.Divide(s.Pow(n - 1), mc);
                c = s.Subtract(c);
                var locmc = new MathContext(c.Precision);
                c = c.Divide(nth, locmc);
                s = s.Subtract(c);
                if (System.Math.Abs(c.ToDouble()/s.ToDouble()) < eps)
                    break;
            }
            return s.Round(new MathContext(ErrorToPrecision(eps)));
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:40,代码来源:BigMath.cs

示例4: PowRound

        public static BigDecimal PowRound(BigDecimal x, Rational q)
        {
            /** Special cases: x^1=x and x^0 = 1
                */
            if (q.CompareTo(BigInteger.One) == 0)
                return x;
            if (q.Sign == 0)
                return BigDecimal.One;
            if (q.IsInteger) {
                /* We are sure that the denominator is positive here, because normalize() has been
                        * called during constrution etc.
                        */
                return PowRound(x, q.Numerator);
            }
            /* Refuse to operate on the general negative basis. The integer q have already been handled above.
                        */
            if (x.CompareTo(BigDecimal.Zero) < 0)
                throw new ArithmeticException("Cannot power negative " + x);
            if (q.IsIntegerFraction) {
                /* Newton method with first estimate in double precision.
                                * The disadvantage of this first line here is that the result must fit in the
                                * standard range of double precision numbers exponents.
                                */
                double estim = System.Math.Pow(x.ToDouble(), q.ToDouble());
                var res = new BigDecimal(estim);

                /* The error in x^q is q*x^(q-1)*Delta(x).
                                * The relative error is q*Delta(x)/x, q times the relative error of x.
                                */
                var reserr = new BigDecimal(0.5*q.Abs().ToDouble()
                                            *x.Ulp().Divide(x.Abs(), MathContext.Decimal64).ToDouble());

                /* The main point in branching the cases above is that this conversion
                                * will succeed for numerator and denominator of q.
                                */
                int qa = q.Numerator.ToInt32();
                int qb = q.Denominator.ToInt32();

                /* Newton iterations. */
                BigDecimal xpowa = PowRound(x, qa);
                for (;;) {
                    /* numerator and denominator of the Newton term.  The major
                                        * disadvantage of this implementation is that the updates of the powers
                                        * of the new estimate are done in full precision calling BigDecimal.pow(),
                                        * which becomes slow if the denominator of q is large.
                                        */
                    BigDecimal nu = res.Pow(qb).Subtract(xpowa);
                    BigDecimal de = MultiplyRound(res.Pow(qb - 1), q.Denominator);

                    /* estimated correction */
                    BigDecimal eps = nu.Divide(de, MathContext.Decimal64);

                    BigDecimal err = res.Multiply(reserr, MathContext.Decimal64);
                    int precDiv = 2 + ErrorToPrecision(eps, err);
                    if (precDiv <= 0) {
                        /* The case when the precision is already reached and any precision
                                                * will do. */
                        eps = nu.Divide(de, MathContext.Decimal32);
                    } else {
                        eps = nu.Divide(de, new MathContext(precDiv));
                    }

                    res = SubtractRound(res, eps);
                    /* reached final precision if the relative error fell below reserr,
                                        * |eps/res| < reserr
                                        */
                    if (eps.Divide(res, MathContext.Decimal64).Abs().CompareTo(reserr) < 0) {
                        /* delete the bits of extra precision kept in this
                                                * working copy.
                                                */
                        return res.Round(new MathContext(ErrorToPrecision(reserr.ToDouble())));
                    }
                }
            }
            /* The error in x^q is q*x^(q-1)*Delta(x) + Delta(q)*x^q*log(x).
                                * The relative error is q/x*Delta(x) + Delta(q)*log(x). Convert q to a floating point
                                * number such that its relative error becomes negligible: Delta(q)/q << Delta(x)/x/log(x) .
                                */
            int precq = 3 + ErrorToPrecision((x.Ulp().Divide(x, MathContext.Decimal64)).ToDouble()
                                             /System.Math.Log(x.ToDouble()));

            /* Perform the actual calculation as exponentiation of two floating point numbers.
                                */
            return Pow(x, q.ToBigDecimal(new MathContext(precq)));
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:85,代码来源:BigMath.cs

示例5: PowWithContext

 public void PowWithContext(string a, int aScale, int exp, string c, int cScale, int precision, RoundingMode roundingMode)
 {
     BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
     MathContext mc = new MathContext(precision, roundingMode);
     BigDecimal result = aNumber.Pow(exp, mc);
     Assert.AreEqual(c, result.ToString(), "incorrect value");
     Assert.AreEqual(cScale, result.Scale, "incorrect scale");
 }
开发者ID:tupunco,项目名称:deveel-math,代码行数:8,代码来源:BigDecimalArithmeticTest.cs

示例6: Pow

 public void Pow(string a, int aScale, int exp, string c, int cScale)
 {
     BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
     BigDecimal result = aNumber.Pow(exp);
     Assert.AreEqual(c, result.ToString(), "incorrect value");
     Assert.AreEqual(cScale, result.Scale, "incorrect scale");
 }
开发者ID:tupunco,项目名称:deveel-math,代码行数:7,代码来源:BigDecimalArithmeticTest.cs


注:本文中的BigDecimal.Pow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。