/// <summary> /// 按位乘法运算(不考虑正负号) /// </summary> /// <param name="num">乘数</param> /// <param name="maxDecimalPlaces">答案保留小数位数</param> /// <returns></returns> protected override Digitable Multiply_unsigned(Digitable num, int maxDecimalPlaces) { IntegerNum a = this, b = num as IntegerNum; if (a == null || b == null) { throw new ProgramInterruptException(ProgramInterruptExceptionType.IllegalValue); } //统一计算空间 if (a.Space != b.Space) { if (a.Space == OperationSpace.DefaultSpace) { a = (IntegerNum)a.ChangeOperationSpace(b.Space); } else if (b.Space == OperationSpace.DefaultSpace) { b = (IntegerNum)b.ChangeOperationSpace(a.Space); } else { throw new ProgramInterruptException(ProgramInterruptExceptionType.NotSameOperationSpace); } } return(new IntegerNum(a.numerator * b.numerator, 1, a.Space)); }
/// <summary> /// 将本数和另一个数取绝对值进行比较,大于输出1,小于输出-1,等于输出0 /// </summary> /// <param name="num"></param> /// <returns></returns> public override int CompareAbsolute(Digitable num) { IntegerNum a = this, b = num as IntegerNum; 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 = (IntegerNum)a.ChangeOperationSpace(b.Space); } else if (b.Space == OperationSpace.DefaultSpace) { b = (IntegerNum)b.ChangeOperationSpace(a.Space); } else { throw new ProgramInterruptException(ProgramInterruptExceptionType.NotSameOperationSpace); } } return(a.numerator.CompareAbsolute(b.numerator)); }
/// <summary> /// 获得带分数的形式 /// </summary> /// <param name="integer">整数部分</param> /// <param name="numerator">新的分子</param> public void GetMixedNumber(out IntegerNum integer, out IntegerNum numerator) { var ans = NaturalNumStr.Mod(this.numerator, this.denominator); integer = ans.Item2; numerator = ans.Item1; }
/// <summary> /// 通分 /// </summary> /// <param name="denominator">通分后的分母</param> /// <param name="numerators">通分后的分子列表</param> /// <param name="nums">各个分数(不改变原有的值)</param> public static void ReductionToACommonDenominator(out IntegerNum denominator, out IntegerNum[] numerators, params RationalNum[] nums) { denominator = null; numerators = new IntegerNum[nums.Length]; if (nums.Length == 1) { denominator = new IntegerNum(nums[0].denominator); numerators[0] = new IntegerNum(nums[0].numerator); } else if (nums.Length > 1) { denominator = NaturalNumStr.LeastCommonMultiple(nums[0].denominator, nums[1].denominator); for (var i = 2; i < nums.Length; i++) { denominator = NaturalNumStr.LeastCommonMultiple(denominator.numerator, nums[i].denominator); } for (var i = 0; i < nums.Length; i++) { numerators[i] = nums[i].numerator * (denominator / nums[i].denominator); } } }
/// <summary> /// 将两数进行比较,大于输出1,小于输出-1,等于输出0 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static int CompareAbsolute(IntegerNum a, IntegerNum b) => a.CompareAbsolute(b);
/// <summary> /// 求两个数的最小公倍数。如果其中有个数为0,则返回0 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static IntegerNum LeastCommonMultiple(IntegerNum a, IntegerNum b) { return(NaturalNumStr.LeastCommonMultiple(a.numerator, b.numerator)); }
/// <summary> /// 用分式初始化 /// </summary> /// <param name="numerator">分子</param> /// <param name="denominator">分母</param> /// <param name="positiveOrNegative">正负性</param> /// <param name="space">计算空间</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> public RationalNum(IntegerNum numerator, IntegerNum denominator, int?positiveOrNegative = null, OperationSpace space = null, int?maxDecimalPlaces = null) : this(numerator.numerator, denominator.numerator, positiveOrNegative ?? numerator.PositiveOrNegative * denominator.PositiveOrNegative, space, maxDecimalPlaces) { }
/// <summary> /// 求两个数的最大公约数。如果其中一个数为0、另一个不为0,则返回不为0的那个数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static IntegerNum GreatestCommonDivisor(IntegerNum a, IntegerNum b) { return(NaturalNumStr.GreatestCommonDivisor(a.numerator, b.numerator)); }