Exemplo n.º 1
0
        /// <summary>e次幂</summary>
        public static BigNumber Exp(BigNumber value, int precision)
        {
            BigNumber sum = new BigNumber("0");

            for (BigNumber n = new BigNumber("0"); ; n++)
            {
                BigNumber r = BigNumber.Division(value.Power(n), n.Factorial(), precision + 1);

                if (r.GetPrecision(0) > precision)
                {
                    break;
                }
                sum = sum + r;
            }
            Remove(sum, precision);
            return(sum);
        }
Exemplo n.º 2
0
        /// <summary>计算value^pow</summary>
        static internal BigNumber Power(BigNumber value, BigNumber pow, int precision)
        {
            if (pow.DecimalPart.Count != 0 && !value.IsPlus)  //第一个条件是小数次幂,第二个条件是负数
            {
                throw new ExpressionException("只有正数才有小数次幂");
            }

            BigNumber result = new BigNumber("1");

            for (BigNumber i = new BigNumber("1"); i.CompareTo(pow) != 1; i++)
            {
                result = result * value;
            }

            BigNumber two = new BigNumber("2");
            int       n   = 1;
            BigNumber tt  = new BigNumber(new List <int>(), pow.DecimalPart, true) * two;

            while (true)
            {
                // 如果整数部分为1,那么就得进行一次2^n开方运算
                if (tt.IntPart.Count == 1 && tt.IntPart[0] == 1)
                {
                    tt.IntPart.Clear();
                    BigNumber r = Root(value, n, precision + 1);

                    if (r.GetPrecision(1) >= precision / BigNumber.OneCount + 2)
                    {
                        break;
                    }
                    result = result * r;
                }
                else if (tt.IsZero())
                {
                    break;
                }

                tt = tt * two;
                n++;
            }
            result.KeepPrecision(precision);
            return(result);
        }