コード例 #1
0
        internal static BigNumber Division(BigNumber one, BigNumber two, int precision)
        {
            List <int> oneInt     = new List <int>();
            List <int> oneDecimal = new List <int>();

            oneInt.AddRange(one.IntPart);
            oneDecimal.AddRange(one.DecimalPart);
            List <int> twoText = new List <int>();

            twoText.AddRange(two.IntPart);
            twoText.AddRange(two.DecimalPart);
            RemoveStartZero(twoText);
            //将15/2.3 移位为 150/23  //DecimalLength
            for (int i = 0; i < two.DecimalPart.Count; i++)
            {
                if (oneDecimal.Count != 0)
                {
                    oneInt.Add(oneDecimal[0]);
                    oneDecimal.RemoveAt(0);
                }
                else
                {
                    oneInt.Add(0);
                }
            }
            List <int> resultInt     = new List <int>();
            List <int> resultDecimal = new List <int>();

            int        index = twoText.Count < oneInt.Count ? twoText.Count - 1 : oneInt.Count - 1;
            List <int> div   = oneInt.GetRange(0, index + 1);

            //正数部分的除法
            while (true)
            {
                resultInt.Add(Division(div, twoText));
                div = IntMinus(div, Multiply(twoText, new List <int> {
                    resultInt[resultInt.Count - 1]
                }), 0);
                index++;
                if (index >= oneInt.Count)
                {
                    break;
                }
                div.Add(oneInt[index]);
            }
            index = 0;
            //小数部分的除法
            while (true)
            {
                //满足精度后退出
                if (resultDecimal.Count >= precision)
                {
                    break;
                }
                if (index >= oneDecimal.Count)
                {
                    div.Add(0);
                }
                else
                {
                    div.Add(oneDecimal[index]);
                }
                int r = Division(div, twoText);
                resultDecimal.Add(r);
                div = IntMinus(div, Multiply(twoText, new List <int> {
                    r
                }), 0);
                index++;
            }
            return(new BigNumber(resultInt, resultDecimal, !(one.IsPlus ^ two.IsPlus)));
        }
コード例 #2
0
 /// <summary>除</summary>
 public static BigNumber Division(BigNumber one, BigNumber two)
 {
     return(Division(one, two, 50));
 }
コード例 #3
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);
            }
        }