コード例 #1
0
        /// <summary>减</summary>
        public static BigNumber Minus(BigNumber one, BigNumber two)
        {
            BigNumber oneAbsolute = one.AbsoluteNumber;
            BigNumber twoAbsolute = two.AbsoluteNumber;

            //正数减正数
            if (one.IsPlus && two.IsPlus)
            {
                if (oneAbsolute.CompareTo(twoAbsolute) == 1)
                {
                    return(PlusMinus(one, two));
                }
                else if (oneAbsolute.CompareTo(twoAbsolute) == -1)
                {
                    return(PlusMinus(two, one).ReverseNumber);
                }
                else
                {
                    return(BigNumber.Zero);
                }
            }
            //正数减负数
            else if (one.IsPlus && !two.IsPlus)
            {
                return(PlusAdd(oneAbsolute, twoAbsolute));
            }
            //负数减正数
            else if (!one.IsPlus && two.IsPlus)
            {
                return(PlusAdd(oneAbsolute, twoAbsolute).ReverseNumber);
            }
            //负数减负数
            else if (!one.IsPlus && !two.IsPlus)
            {
                return(Add(one, twoAbsolute));
            }
            throw new Exception("不可能到达的地方");
        }
コード例 #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);
        }