/// <summary> /// 改变元素的计算空间 /// </summary> /// <param name="space">新的计算空间,为null即为默认计算空间</param> /// <param name="newInstance">是否返回新的实例(如果为null,即为不确定)</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> /// <returns></returns> public override IOperationSpaceElement ChangeOperationSpace(OperationSpace space = null, bool?newInstance = null, int?maxDecimalPlaces = null) { if (space == null) { space = OperationSpace.DefaultSpace; } var mdp = maxDecimalPlaces.HasValue ? Math.Min(maxDecimalPlaces.Value, space.DefaultMaxDecimalPlaces) : space.DefaultMaxDecimalPlaces; if (newInstance == true) { var new_num = new RationalNum(this, null, true) { Space = space, MaxDecimalPlaces = mdp, numerator = (NaturalNumStr)numerator.ChangeOperationSpace(space, newInstance), denominator = (NaturalNumStr)denominator.ChangeOperationSpace(space, newInstance) }; return(new_num); } else { valueChanged = true; Space = space; MaxDecimalPlaces = mdp; numerator = (NaturalNumStr)numerator.ChangeOperationSpace(space, newInstance); denominator = (NaturalNumStr)denominator.ChangeOperationSpace(space, newInstance); return(this); } }
/// <summary> /// 用分式初始化 /// </summary> /// <param name="numerator">分子</param> /// <param name="denominator">分母</param> /// <param name="positiveOrNegative">正负性</param> /// <param name="space">计算空间</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> public RationalNum(NaturalNumStr numerator, NaturalNumStr denominator, int positiveOrNegative = 1, OperationSpace space = null, int?maxDecimalPlaces = null) : base(null, space ?? numerator.Space, maxDecimalPlaces) { if (space == null && numerator.Space != denominator.Space) { throw new ProgramInterruptException(ProgramInterruptExceptionType.IllegalValue); } else { space = space ?? numerator.Space; } this.numerator = (NaturalNumStr)numerator.ChangeOperationSpace(space); this.denominator = (NaturalNumStr)denominator.ChangeOperationSpace(space); this.PositiveOrNegative = positiveOrNegative; FractionReduction(); }