Example #1
0
        public static BigDecimal Minus(BigDecimal Num1, BigDecimal Num2)
        {
            //减法运算和加法完全类似
            int        Rank1    = Num1.AcquireRank();
            int        Rank2    = Num2.AcquireRank();
            BigInteger IntPart1 = Num1.AcquireIntPart();
            BigInteger IntPart2 = Num2.AcquireIntPart();

            if (Rank1 > Rank2)
            {
                //第一个数字小数点需往右边移动
                BigInteger x = IntPart1 * BigInteger.Pow(10, Rank1 - Rank2) - IntPart2;
                return(new BigDecimal(x, Rank2));
            }
            else if (Rank1 < Rank2)
            {
                //第二个数字小数点需往右边移动
                BigInteger x = IntPart1 - IntPart2 * BigInteger.Pow(10, Rank2 - Rank1);
                return(new BigDecimal(x, Rank1));
            }
            else
            {
                //无需调整
                BigInteger x = IntPart1 - IntPart2;
                return(new BigDecimal(x, Rank1));
            }
        }
Example #2
0
        public static BigDecimal Divide(BigDecimal Num1, BigDecimal Num2)
        {
            //暂时没有加入四舍五入的考虑
            int        Rank1    = Num1.AcquireRank();
            int        Rank2    = Num2.AcquireRank();
            BigInteger IntPart1 = Num1.AcquireIntPart();
            BigInteger IntPart2 = Num2.AcquireIntPart();

            if (-Rank1 + Rank2 < Precision + 1)
            {
                //小于相应精度
                //调整被除数的精度
                BigInteger x = IntPart1 * BigInteger.Pow(10, Precision + 1 - (Rank2 - Rank1)) / IntPart2;
                return(new BigDecimal(x, -(Precision + 1)));
            }
            else if (-Rank1 + Rank2 > Precision + 1)
            {
                //大于相应精度
                //调整除数的精度
                BigInteger x = IntPart1 / (IntPart2 * BigInteger.Pow(10, (Rank2 - Rank1) - (Precision + 1)));
                return(new BigDecimal(x, -(Precision + 1)));
            }
            else
            {
                return(new BigDecimal(IntPart1 / IntPart2, -(Precision + 1)));
            }
        }
Example #3
0
        public static BigDecimal Multiply(BigDecimal Num1, BigDecimal Num2)
        {
            //BigDecimal 的乘法运算比加减法运算还要简单
            //IntPart相乘,阶数相加即可
            int        Rank1    = Num1.AcquireRank();
            int        Rank2    = Num2.AcquireRank();
            BigInteger IntPart1 = Num1.AcquireIntPart();
            BigInteger IntPart2 = Num2.AcquireIntPart();

            return(new BigDecimal(IntPart1 * IntPart2, Rank1 + Rank2));
        }
Example #4
0
        public static int Compare(BigDecimal Num1, BigDecimal Num2)
        {
            //1表示Num1 > Num2
            //-1表示Num1 < Num2
            //0 表示Num1 == Num2
            int        Rank1    = Num1.AcquireRank();
            int        Rank2    = Num2.AcquireRank();
            BigInteger IntPart1 = Num1.AcquireIntPart();
            BigInteger IntPart2 = Num2.AcquireIntPart();
            int        Sign1    = IntPart1.Sign;
            int        Sign2    = IntPart2.Sign;

            //Sign正数返回1,负数返回-1,0返回0
            if (Sign1 == 1 && Sign2 == -1)
            {
                //Num1为正,Num2为负
                return(1);
            }
            else if (Sign1 == -1 && Sign2 == 1)
            {
                //Num1为负,Num2为正
                return(-1);
            }
            else if (Sign1 == 0 && Sign2 == 0)
            {
                //Num1为0,Num2为0
                return(0);
            }

            /*int Sign12 = (Num1 - Num2).AcquireIntPart().Sign;
             * //直接相减虽然简单,但是速度太慢,以后有空我会进行优化
             * if (Sign12 > 0)
             * {
             *  return 1;
             *  //Num1 > Num2
             * }
             * else if (Sign12 < 0)
             * {
             *  return -1;
             *  //Num1 < Num2
             * }
             * else
             * {
             *  return 0;
             *  //Num1 == Num2
             * }*/
            //为了避免转为字符串之后进行比较的尴尬,我想了个办法。。。

            if (Rank1 > Rank2)
            {
                //第一个数字小数点需往右边移动
                IntPart1 = IntPart1 * BigInteger.Pow(10, Rank1 - Rank2);
            }
            else if (Rank1 < Rank2)
            {
                //第二个数字小数点需往右边移动
                IntPart2 = IntPart2 * BigInteger.Pow(10, Rank2 - Rank1);
            }
            //除此之外无需调整
            return(BigInteger.Compare(IntPart1, IntPart2));
        }
        public static int Compare(BigDecimal Num1, BigDecimal Num2)
        {
            //1表示Num1 > Num2
            //-1表示Num1 < Num2
            //0 表示Num1 == Num2
            int Rank1 = Num1.AcquireRank();
            int Rank2 = Num2.AcquireRank();
            BigInteger IntPart1 = Num1.AcquireIntPart();
            BigInteger IntPart2 = Num2.AcquireIntPart();
            int Sign1 = IntPart1.Sign;
            int Sign2 = IntPart2.Sign;
            //Sign正数返回1,负数返回-1,0返回0
            if (Sign1 == 1 && Sign2 == -1)
            {
                //Num1为正,Num2为负
                return 1;
            }
            else if (Sign1 == -1 && Sign2 == 1)
            {
                //Num1为负,Num2为正
                return -1;
            }
            else if (Sign1 == 0 && Sign2 == 0)
            {
                //Num1为0,Num2为0
                return 0;
            }
            /*int Sign12 = (Num1 - Num2).AcquireIntPart().Sign;
            //直接相减虽然简单,但是速度太慢,以后有空我会进行优化
            if (Sign12 > 0)
            {
                return 1;
                //Num1 > Num2
            }
            else if (Sign12 < 0)
            {
                return -1;
                //Num1 < Num2
            }
            else
            {
                return 0;
                //Num1 == Num2
            }*/
            //为了避免转为字符串之后进行比较的尴尬,我想了个办法。。。

            if (Rank1 > Rank2)
            {
                //第一个数字小数点需往右边移动
                IntPart1 = IntPart1 * BigInteger.Pow(10, Rank1 - Rank2);
            }
            else if (Rank1 < Rank2)
            {
                //第二个数字小数点需往右边移动
                IntPart2 = IntPart2 * BigInteger.Pow(10, Rank2 - Rank1);
            }
            //除此之外无需调整
            return BigInteger.Compare(IntPart1, IntPart2);
        }
 public static BigDecimal Divide(BigDecimal Num1, BigDecimal Num2)
 {
     //暂时没有加入四舍五入的考虑
     int Rank1 = Num1.AcquireRank();
     int Rank2 = Num2.AcquireRank();
     BigInteger IntPart1 = Num1.AcquireIntPart();
     BigInteger IntPart2 = Num2.AcquireIntPart();
     if (-Rank1 + Rank2 < Precision + 1)
     {
         //小于相应精度
         //调整被除数的精度
         BigInteger x = IntPart1 * BigInteger.Pow(10, Precision + 1 - (Rank2 - Rank1)) / IntPart2;
         return new BigDecimal(x, -(Precision + 1));
     }
     else if (-Rank1 + Rank2 > Precision + 1)
     {
         //大于相应精度
         //调整除数的精度
         BigInteger x = IntPart1 / (IntPart2 * BigInteger.Pow(10, (Rank2 - Rank1) - (Precision + 1)));
         return new BigDecimal(x, -(Precision + 1));
     }
     else
     {
         return new BigDecimal(IntPart1 / IntPart2, -(Precision + 1));
     }
 }
 public static BigDecimal Multiply(BigDecimal Num1, BigDecimal Num2)
 {
     //BigDecimal 的乘法运算比加减法运算还要简单
     //IntPart相乘,阶数相加即可
     int Rank1 = Num1.AcquireRank();
     int Rank2 = Num2.AcquireRank();
     BigInteger IntPart1 = Num1.AcquireIntPart();
     BigInteger IntPart2 = Num2.AcquireIntPart();
     return new BigDecimal(IntPart1 * IntPart2, Rank1 + Rank2);
 }
 public static BigDecimal Minus(BigDecimal Num1, BigDecimal Num2)
 {
     //减法运算和加法完全类似
     int Rank1 = Num1.AcquireRank();
     int Rank2 = Num2.AcquireRank();
     BigInteger IntPart1 = Num1.AcquireIntPart();
     BigInteger IntPart2 = Num2.AcquireIntPart();
     if (Rank1 > Rank2)
     {
         //第一个数字小数点需往右边移动
         BigInteger x = IntPart1 * BigInteger.Pow(10, Rank1 - Rank2) - IntPart2;
         return new BigDecimal(x, Rank2);
     }
     else if (Rank1 < Rank2)
     {
         //第二个数字小数点需往右边移动
         BigInteger x = IntPart1 - IntPart2 * BigInteger.Pow(10, Rank2 - Rank1);
         return new BigDecimal(x, Rank1);
     }
     else
     {
         //无需调整
         BigInteger x = IntPart1 - IntPart2;
         return new BigDecimal(x, Rank1);
     }
 }