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


C# BigDecimal.Abs方法代码示例

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


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

示例1: Abs

 public void Abs()
 {
     BigDecimal big = BigDecimal.Parse("-1234");
     BigDecimal bigabs = big.Abs();
     Assert.IsTrue(bigabs.ToString().Equals("1234"), "the absolute value of -1234 is not 1234");
     big = new BigDecimal(BigInteger.Parse("2345"), 2);
     bigabs = big.Abs();
     Assert.IsTrue(bigabs.ToString().Equals("23.45"), "the absolute value of 23.45 is not 23.45");
 }
开发者ID:tupunco,项目名称:deveel-math,代码行数:9,代码来源:BigDecimalTest.cs

示例2: Hypot

        public static BigDecimal Hypot(BigDecimal x, BigDecimal y)
        {
            /* compute x^2+y^2
                */
            BigDecimal z = x.Pow(2).Add(y.Pow(2));

            /* truncate to the precision set by x and y. Absolute error = 2*x*xerr+2*y*yerr,
                * where the two errors are 1/2 of the ulp's.  Two intermediate protectio digits.
                */
            BigDecimal zerr = x.Abs().Multiply(x.Ulp()).Add(y.Abs().Multiply(y.Ulp()));
            var mc = new MathContext(2 + ErrorToPrecision(z, zerr));

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

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

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

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


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