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


C# BigDecimal.Round方法代码示例

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


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

示例1: 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

示例2: 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

示例3: RoundMathContextPrecision0

 public void RoundMathContextPrecision0()
 {
     String a = "3736186567876876578956958765675671119238118911893939591735";
     int aScale = 45;
     int precision = 0;
     RoundingMode rm = RoundingMode.HalfUp;
     MathContext mc = new MathContext(precision, rm);
     String res = "3736186567876.876578956958765675671119238118911893939591735";
     BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
     BigDecimal result = aNumber.Round(mc);
     Assert.AreEqual(res, result.ToString(), "incorrect quotient value");
     Assert.AreEqual(aScale, result.Scale, "incorrect quotient scale");
 }
开发者ID:tupunco,项目名称:deveel-math,代码行数:13,代码来源:BigDecimalArithmeticTest.cs


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