Exemplo n.º 1
0
        /// <summary>
        /// 按位取模运算(不考虑正负号)
        /// </summary>
        /// <param name="num">模数</param>
        /// <param name="maxDecimalPlaces">答案保留小数位数</param>
        /// <returns>包含余数和商的元组</returns>
        protected override (Digitable, Digitable) Mod_unsigned(Digitable num, int maxDecimalPlaces)
        {
            RationalNum a = this, b = num as RationalNum;

            if (a == null || b == null)
            {
                throw new ProgramInterruptException(ProgramInterruptExceptionType.IllegalValue);
            }
            //统一计算空间
            if (a.Space != b.Space)
            {
                if (a.Space == OperationSpace.DefaultSpace)
                {
                    a = (RationalNum)a.ChangeOperationSpace(b.Space);
                }
                else if (b.Space == OperationSpace.DefaultSpace)
                {
                    b = (RationalNum)b.ChangeOperationSpace(a.Space);
                }
                else
                {
                    throw new ProgramInterruptException(ProgramInterruptExceptionType.NotSameOperationSpace);
                }
            }

            var ans = new RationalNum(a, b, 1, a.Space);

            ans.GetMixedNumber(out var i, out var n);
            return(new RationalNum(n * b.numerator, ans.denominator * b.denominator, 1, a.Space, maxDecimalPlaces),
                   i);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 按位乘法运算(不考虑正负号)
        /// </summary>
        /// <param name="num">乘数</param>
        /// <param name="maxDecimalPlaces">答案保留小数位数</param>
        /// <returns></returns>
        protected override Digitable Multiply_unsigned(Digitable num, int maxDecimalPlaces)
        {
            RationalNum a = this, b = num as RationalNum;

            if (a == null || b == null)
            {
                throw new ProgramInterruptException(ProgramInterruptExceptionType.IllegalValue);
            }
            //统一计算空间
            if (a.Space != b.Space)
            {
                if (a.Space == OperationSpace.DefaultSpace)
                {
                    a = (RationalNum)a.ChangeOperationSpace(b.Space);
                }
                else if (b.Space == OperationSpace.DefaultSpace)
                {
                    b = (RationalNum)b.ChangeOperationSpace(a.Space);
                }
                else
                {
                    throw new ProgramInterruptException(ProgramInterruptExceptionType.NotSameOperationSpace);
                }
            }
            return(new RationalNum(a.numerator * b.numerator, a.denominator * b.denominator, 1, a.Space, maxDecimalPlaces));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 将本数和另一个数取绝对值进行比较,大于输出1,小于输出-1,等于输出0
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public override int CompareAbsolute(Digitable num)
        {
            RationalNum a = this, b = num as RationalNum;

            if (ReferenceEquals(a, b))
            {
                return(0);
            }

            if (a == null || b == null)
            {
                throw new ProgramInterruptException(ProgramInterruptExceptionType.IllegalValue);
            }
            //统一计算空间
            if (a.Space != b.Space)
            {
                if (a.Space == OperationSpace.DefaultSpace)
                {
                    a = (RationalNum)a.ChangeOperationSpace(b.Space);
                }
                else if (b.Space == OperationSpace.DefaultSpace)
                {
                    b = (RationalNum)b.ChangeOperationSpace(a.Space);
                }
                else
                {
                    throw new ProgramInterruptException(ProgramInterruptExceptionType.NotSameOperationSpace);
                }
            }

            //通分
            ReductionToACommonDenominator(out var d, out var nums, a, b);
            return(nums[0].CompareAbsolute(nums[1]));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 取模运算
        /// </summary>
        /// <param name="a">被模数</param>
        /// <param name="b">模数</param>
        /// <param name="maxDecimalPlaces">答案保留小数位数</param>
        /// <returns>包含余数和商的元组</returns>
        public static (RationalNum, IntegerNum) Mod(RationalNum a, RationalNum b, int?maxDecimalPlaces = null)
        {
            var ans = Digitable.Mod(a, b, maxDecimalPlaces);

            return(ans.Item1 as RationalNum, ans.Item2 as IntegerNum);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 拷贝初始化
 /// </summary>
 /// <param name="num">原始对象</param>
 /// <param name="deep">是否为深拷贝</param>
 public IntegerNum(RationalNum num, bool deep = true)
     : base(num, 0, deep)
 {
 }
Exemplo n.º 6
0
 /// <summary>
 /// 将两数进行比较,大于输出1,小于输出-1,等于输出0
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static int CompareAbsolute(RationalNum a, RationalNum b) => a.CompareAbsolute(b);