Exemplo n.º 1
0
        /// <summary>开方</summary>
        static internal BigNumber Sqrt(BigNumber value, int precision)
        {
            if (!value.IsPlus)
            {
                throw new ExpressionException("只有正数才有开平方运算");
            }

            List <int> div            = new List <int>();
            int        index          = 0;
            int        resultIntCount = (value.IntPart.Count + 1) / 2;

            List <int> result = GetFirstDiv(value, ref div, ref index);

            div = FirstTry(result, div);
            div.AddRange(GetNewTwoDiv(value, ref index));
            BigCalculate.RemoveStartZero(div);
            //考虑精度的计算
            while (true)
            {
                div = TryDiv(div, result);
                if (result.Count - resultIntCount >= precision)
                {
                    break;
                }

                div.AddRange(GetNewTwoDiv(value, ref index));
                BigCalculate.RemoveStartZero(div);
            }

            return(new BigNumber(result.GetRange(0, resultIntCount), result.GetRange(resultIntCount, result.Count - resultIntCount), true));
        }
Exemplo n.º 2
0
 /// <summary>返回0.00001这样的数小数点后、有效数前有几个零</summary>
 internal int GetPrecision(int value)
 {
     if (value == 0)
     {
         BigCalculate.RemoveStartZero(IntPart);
         if (IntPart.Count == 1 && IntPart[0] == 0)
         {
             if (DecimalPart.Count == 0)
             {
                 return(0);
             }
             for (int i = 0; i < DecimalPart.Count; i++)
             {
                 if (DecimalPart[i] != 0)
                 {
                     return(i);
                 }
             }
             return(DecimalPart.Count);
         }
         return(0);
     }
     else
     {
         if (IntPart.Count == 1 && IntPart[0] == 1)
         {
             if (DecimalPart.Count == 0)
             {
                 return(0);
             }
             for (int i = 0; i < DecimalPart.Count; i++)
             {
                 if (DecimalPart[i] != 0)
                 {
                     return(i);
                 }
             }
             return(DecimalPart.Count);
         }
         return(0);
     }
 }
Exemplo n.º 3
0
        /// <summary>识别出一个大数,返回值为这个是不是正数</summary>
        public static bool GetBigNumber(string text, List <int> intPart, List <int> decimalPart)
        {
            ValidateNumber(text);
            bool isPlus  = true;
            int  haveSym = 0;

            if (text[0] == '-')
            {
                isPlus  = false;
                haveSym = 1;
            }
            else if (text[0] == '+')
            {
                haveSym = 1;
            }
            int pointIndex = text.IndexOf('.');

            //有小数点
            if (pointIndex != -1)
            {
                int i = 0;
                if ((pointIndex - haveSym) % BigNumber.OneCount != 0)
                {
                    intPart.Add(Convert.ToInt32(text.Substring(haveSym, (pointIndex - haveSym) % BigNumber.OneCount)));
                }
                for (; i < (pointIndex - haveSym) / BigNumber.OneCount; i++)
                {
                    intPart.Add(Convert.ToInt32(text.Substring(haveSym + i * BigNumber.OneCount + (pointIndex - haveSym) % BigNumber.OneCount, BigNumber.OneCount)));
                }
                i = pointIndex + 1;
                while (i < text.Length)
                {
                    if (text.Length - i >= BigNumber.OneCount)
                    {
                        decimalPart.Add(Convert.ToInt32(text.Substring(i, BigNumber.OneCount)));
                    }
                    else
                    {
                        decimalPart.Add(Convert.ToInt32(text.Substring(i, (text.Length - i) % BigNumber.OneCount).PadRight(BigNumber.OneCount, '0')));
                    }
                    i += BigNumber.OneCount;
                }
            }
            //没有小数点
            else
            {
                if ((text.Length - haveSym) % BigNumber.OneCount != 0)
                {
                    intPart.Add(Convert.ToInt32(text.Substring(haveSym, (text.Length - haveSym) % BigNumber.OneCount)));
                }
                for (int i = 0; i < (text.Length - haveSym) / BigNumber.OneCount; i++)
                {
                    intPart.Add(Convert.ToInt32(text.Substring(haveSym + i * BigNumber.OneCount + (text.Length - haveSym) % BigNumber.OneCount, BigNumber.OneCount)));
                }
            }
            BigCalculate.RemoveStartZero(intPart);
            if (intPart.Count == 0)
            {
                intPart.Add(0);
            }
            return(isPlus);
        }
Exemplo n.º 4
0
        static int ComparePlus(BigNumber one, BigNumber two)
        {
            BigCalculate.RemoveStartZero(one.IntPart);
            BigCalculate.RemoveStartZero(two.IntPart);
            int minDecimalLength = one.DecimalPart.Count < two.DecimalPart.Count ? one.DecimalPart.Count : two.DecimalPart.Count;

            //通过正数的长短比较
            if (one.IntPart.Count > two.IntPart.Count)
            {
                return(1);
            }
            else if (one.IntPart.Count < two.IntPart.Count)
            {
                return(-1);
            }
            else
            {
                //从最高位依次比较
                for (int i = 0; i < one.IntPart.Count; i++)
                {
                    if (one.IntPart[i] > two.IntPart[i])
                    {
                        return(1);
                    }
                    else if (one.IntPart[i] < two.IntPart[i])
                    {
                        return(-1);
                    }
                }
                //比较小数部分,行判断是否有小数位
                if (one.DecimalPart.Count == 0 && two.DecimalPart.Count != 0)
                {
                    return(-1);
                }
                else if (one.DecimalPart.Count != 0 && two.DecimalPart.Count == 0)
                {
                    return(1);
                }
                else if (one.DecimalPart.Count == 0 && two.DecimalPart.Count == 0)
                {
                    return(0);
                }
                else
                {
                    for (int i = 0; i < minDecimalLength; i++)
                    {
                        if (one.DecimalPart[i] > two.DecimalPart[i])
                        {
                            return(1);
                        }
                        else if (one.DecimalPart[i] < two.DecimalPart[i])
                        {
                            return(-1);
                        }
                    }
                    if (one.DecimalPart.Count > two.DecimalPart.Count)
                    {
                        return(1);
                    }
                    else if (one.DecimalPart.Count < two.DecimalPart.Count)
                    {
                        return(-1);
                    }
                }
                return(0);
            }
        }