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


C# Rational.CompareTo方法代码示例

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


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

示例1: CompareToTest

 public void CompareToTest()
 {
     Rational target = new Rational(1, 3);
     Assert.AreEqual(target.CompareTo(new Rational(1, 2)), -1);
     Assert.AreEqual(target.CompareTo(new Rational(1, 3)), 0);
     Assert.AreEqual(target.CompareTo(new Rational(1, 4)), 1);
 }
开发者ID:rettour,项目名称:Labs,代码行数:7,代码来源:RationalTest.cs

示例2: MultiplyRound

        public static BigDecimal MultiplyRound(BigDecimal x, Rational f)
        {
            if (f.CompareTo(BigInteger.Zero) == 0)
                return BigDecimal.Zero;
            /* Convert the rational value with two digits of extra precision
                        */
            var mc = new MathContext(2 + x.Precision);
            BigDecimal fbd = f.ToBigDecimal(mc);

            /* and the precision of the product is then dominated by the precision in x
                        */
            return MultiplyRound(x, fbd);
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:13,代码来源:BigMath.cs

示例3: Log

        public static BigDecimal Log(Rational r, MathContext mc)
        {
            /* the value is undefined if x is negative.
                */
            if (r.CompareTo(Rational.Zero) <= 0)
                throw new ArithmeticException("Cannot take log of negative " + r);
            if (r.CompareTo(Rational.One) == 0)
                return BigDecimal.Zero;
            /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result.
                        * eps contains the required absolute error of the result, epsr/r.
                        */
            double eps = PrecisionToError(System.Math.Log(r.ToDouble()), mc.Precision);

            /* Convert this further into a requirement of the relative precision in r, given that
                        * epsr/r is also the relative precision of r. Add one safety digit.
                        */
            var mcloc = new MathContext(1 + ErrorToPrecision(eps));

            BigDecimal resul = Log(r.ToBigDecimal(mcloc));

            return resul.Round(mc);
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:22,代码来源: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


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