/// <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)); }
/// <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); }
/// <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])); }