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


C# Rational.Multiply方法代码示例

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


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

示例1: Log

        public static BigDecimal Log(int n, MathContext mc)
        {
            /* the value is undefined if x is negative.
                */
            if (n <= 0)
                throw new ArithmeticException("Cannot take log of negative " + n);
            if (n == 1)
                return BigDecimal.Zero;
            if (n == 2) {
                if (mc.Precision < Log2.Precision)
                    return Log2.Round(mc);
                /* Broadhurst <a href="http://arxiv.org/abs/math/9803067">arXiv:math/9803067</a>
                                * Error propagation: the error in log(2) is twice the error in S(2,-5,...).
                                */
                int[] a = {2, -5, -2, -7, -2, -5, 2, -3};
                BigDecimal S = BroadhurstBbp(2, 1, a, new MathContext(1 + mc.Precision));
                S = S.Multiply(new BigDecimal(8));
                S = Sqrt(DivideRound(S, 3));
                return S.Round(mc);
            }
            if (n == 3) {
                /* summation of a series roughly proportional to (7/500)^k. Estimate count
                        * of terms to estimate the precision (drop the favorable additional
                        * 1/k here): 0.013^k <= 10^(-precision), so k*log10(0.013) <= -precision
                        * so k>= precision/1.87.
                        */
                var kmax = (int) (mc.Precision/1.87);
                var mcloc = new MathContext(mc.Precision + 1 + (int) (System.Math.Log10(kmax*0.693/1.098)));
                BigDecimal log3 = MultiplyRound(Log(2, mcloc), 19);

                /* log3 is roughly 1, so absolute and relative error are the same. The
                        * result will be divided by 12, so a conservative error is the one
                        * already found in mc
                        */
                double eps = PrecisionToError(1.098, mc.Precision)/kmax;
                var r = new Rational(7153, 524288);
                var pk = new Rational(7153, 524288);
                for (int k = 1;; k++) {
                    Rational tmp = pk.Divide(k);
                    if (tmp.ToDouble() < eps)
                        break;

                    /* how many digits of tmp do we need in the sum?
                                */
                    mcloc = new MathContext(ErrorToPrecision(tmp.ToDouble(), eps));
                    BigDecimal c = pk.Divide(k).ToBigDecimal(mcloc);
                    if (k%2 != 0)
                        log3 = log3.Add(c);
                    else
                        log3 = log3.Subtract(c);
                    pk = pk.Multiply(r);
                }
                log3 = DivideRound(log3, 12);
                return log3.Round(mc);
            }
            if (n == 5) {
                /* summation of a series roughly proportional to (7/160)^k. Estimate count
                        * of terms to estimate the precision (drop the favorable additional
                        * 1/k here): 0.046^k <= 10^(-precision), so k*log10(0.046) <= -precision
                        * so k>= precision/1.33.
                        */
                var kmax = (int) (mc.Precision/1.33);
                var mcloc = new MathContext(mc.Precision + 1 + (int) (System.Math.Log10(kmax*0.693/1.609)));
                BigDecimal log5 = MultiplyRound(Log(2, mcloc), 14);

                /* log5 is roughly 1.6, so absolute and relative error are the same. The
                        * result will be divided by 6, so a conservative error is the one
                        * already found in mc
                        */
                double eps = PrecisionToError(1.6, mc.Precision)/kmax;
                var r = new Rational(759, 16384);
                var pk = new Rational(759, 16384);
                for (int k = 1;; k++) {
                    Rational tmp = pk.Divide(k);
                    if (tmp.ToDouble() < eps)
                        break;

                    /* how many digits of tmp do we need in the sum?
                                */
                    mcloc = new MathContext(ErrorToPrecision(tmp.ToDouble(), eps));
                    BigDecimal c = pk.Divide(k).ToBigDecimal(mcloc);
                    log5 = log5.Subtract(c);
                    pk = pk.Multiply(r);
                }
                log5 = DivideRound(log5, 6);
                return log5.Round(mc);
            }
            if (n == 7) {
                /* summation of a series roughly proportional to (1/8)^k. Estimate count
                        * of terms to estimate the precision (drop the favorable additional
                        * 1/k here): 0.125^k <= 10^(-precision), so k*log10(0.125) <= -precision
                        * so k>= precision/0.903.
                        */
                var kmax = (int) (mc.Precision/0.903);
                var mcloc = new MathContext(mc.Precision + 1 + (int) (System.Math.Log10(kmax*3*0.693/1.098)));
                BigDecimal log7 = MultiplyRound(Log(2, mcloc), 3);

                /* log7 is roughly 1.9, so absolute and relative error are the same.
                        */
                double eps = PrecisionToError(1.9, mc.Precision)/kmax;
//.........这里部分代码省略.........
开发者ID:tupunco,项目名称:deveel-math,代码行数:101,代码来源:BigMath.cs


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