/// <summary> /// 改变元素的计算空间 /// </summary> /// <param name="space">新的计算空间,为null即为默认计算空间</param> /// <param name="newInstance">是否返回新的实例(如果为null,即为不确定)</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> /// <returns></returns> protected NumStr ChangeOS(OperationSpace space, bool?newInstance, int?maxDecimalPlaces) { if (space == null) { space = OperationSpace.DefaultSpace; } if (this.Space == space && (maxDecimalPlaces == null || maxDecimalPlaces == space.DefaultMaxDecimalPlaces)) { if (newInstance == true) { return(new NumStr(this)); } else { return(this); } } NumStr new_num = new NumStr(0, space, maxDecimalPlaces); if (IsZero) { return(new_num); } new_num.PositiveOrNegative = PositiveOrNegative; NumberBaseConversion(new_num, space.NumberBase, this); if (newInstance == false) { Copy(new_num, null, false); return(this); } else { return(new_num); } }
/// <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); } }
public void MultiplyTest() { var a1 = new NumStr("60") * new NumStr("1.23"); Assert.AreEqual(a1.ToString(), "73.8"); a1 = new NumStr("96.2014") * new NumStr("6.3254"); Assert.AreEqual(a1.ToString(), "608.51233556"); a1 = new NumStr("0.25") * new NumStr("0.25"); Assert.AreEqual(a1.ToString(), "0.0625"); a1 = new NumStr("0.5") * new NumStr("60"); Assert.AreEqual(a1.ToString(), "30"); a1 = new NumStr("-0.5") * new NumStr("60"); Assert.AreEqual(a1.ToString(), "-30"); a1 = new NumStr("-98.023"); a1 = a1 * a1; Assert.AreEqual(a1.ToString(), "9608.508529"); var s = new OperationSpace(10, 16); a1 = new NumStr("7B", s) * new NumStr("3C", s); Assert.AreEqual(a1.ToString(), "1CD4"); s = new OperationSpace(4, 10); a1 = new NumStr("96.2014", s) * new NumStr("6.3254", s); Assert.AreEqual(a1.ToString(), "608.5123"); }
public void MinusTest() { var a1 = new NumStr("10000") - new NumStr("0.023"); Assert.AreEqual(a1.ToString(), "9999.977"); a1 = new NumStr("18.023") - new NumStr("9.023"); Assert.AreEqual(a1.ToString(), "9"); a1 = new NumStr("18.023") - new NumStr("9.344"); Assert.AreEqual(a1.ToString(), "8.679"); a1 = new NumStr("18.023") - new NumStr("18.023"); Assert.AreEqual(a1.ToString(), "0"); a1 = new NumStr("-18.023") - new NumStr("9.344"); Assert.AreEqual(a1.ToString(), "-27.367"); a1 = new NumStr("18.023") - new NumStr("-9.344"); Assert.AreEqual(a1.ToString(), "27.367"); a1 = new NumStr("-18.023") - new NumStr("-9.344"); Assert.AreEqual(a1.ToString(), "-8.679"); a1 = new NumStr("-98.023"); a1 = a1 - a1; Assert.AreEqual(a1.ToString(), "0"); var s = new OperationSpace(10, 16); a1 = new NumStr("7B", s) - new NumStr("3C", s); Assert.AreEqual(a1.ToString(), "3F"); }
/// <summary> /// 用纯数字初始化,转换为指定的计算空间 /// </summary> /// <param name="num">数字</param> /// <param name="space">计算空间</param> public NaturalNumStr(NumStr num, OperationSpace space) : base(num, space, 0) { if (PositiveOrNegative < 0) { PositiveOrNegative = 1; } }
/// <summary> /// 用字符串表示的数字初始化,如果为负数则保存绝对值 /// </summary> /// <param name="num">整数</param> /// <param name="space">计算空间</param> public NaturalNumStr(string num, OperationSpace space = null) : base(num, space, 0) { if (PositiveOrNegative < 0) { PositiveOrNegative = 1; } }
/// <summary> /// 用整数初始化 /// </summary> /// <param name="num">整数</param> /// <param name="space">计算空间</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> public NumStr(Int64 num, OperationSpace space = null, int?maxDecimalPlaces = null) { if (space != null) { this.Space = space; } this.MaxDecimalPlaces = maxDecimalPlaces.HasValue ? Math.Min(maxDecimalPlaces.Value, this.Space.DefaultMaxDecimalPlaces) : this.Space.DefaultMaxDecimalPlaces; ChangeNumber(num); }
/// <summary> /// 用相同类型的数字初始化,转换为指定的计算空间 /// </summary> /// <param name="num">原始对象</param> /// <param name="space">计算空间</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> /// <param name="positiveOrNegative">正负性</param> public NumStr(NumStr num, OperationSpace space, int?maxDecimalPlaces = null, int?positiveOrNegative = null) { Space = space ?? num.Space; PositiveOrNegative = positiveOrNegative ?? num.PositiveOrNegative; MaxDecimalPlaces = maxDecimalPlaces.HasValue ? Math.Min(maxDecimalPlaces.Value, this.Space.DefaultMaxDecimalPlaces) : this.Space.DefaultMaxDecimalPlaces; NumberBaseConversion(this, Space.NumberBase, num); }
/// <summary> /// 用纯数字初始化 /// </summary> /// <param name="num">数字</param> /// <param name="space">计算空间</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> public RealNum(NumStr num, OperationSpace space = null, int?maxDecimalPlaces = null) { this.Space = space ?? (num != null ? num.Space : OperationSpace.DefaultSpace); this.MaxDecimalPlaces = maxDecimalPlaces.HasValue ? Math.Min(maxDecimalPlaces.Value, this.Space.DefaultMaxDecimalPlaces) : this.Space.DefaultMaxDecimalPlaces; if (num != null) { this.PositiveOrNegative = num.PositiveOrNegative; value = new NumStr(num, Space, MaxDecimalPlaces); } }
public void LCMTest() { var a1 = NaturalNumStr.LeastCommonMultiple(16, 24); Assert.AreEqual(a1.ToString(), "48"); var s = new OperationSpace(10, 16); a1 = NaturalNumStr.LeastCommonMultiple(new NaturalNumStr("90", s), new NaturalNumStr("60", s)); Assert.AreEqual(a1.ToString(), "120"); }
public void GCDTest() { var a1 = NaturalNumStr.GreatestCommonDivisor(16, 24); Assert.AreEqual(a1.ToString(), "8"); a1 = NaturalNumStr.GreatestCommonDivisor(13, 24); Assert.AreEqual(a1.ToString(), "1"); var s = new OperationSpace(10, 16); a1 = NaturalNumStr.GreatestCommonDivisor(new NaturalNumStr("90", s), new NaturalNumStr("60", s)); Assert.AreEqual(a1.ToString(), "30"); }
/// <summary> /// 用纯数字初始化 /// </summary> /// <param name="num">数字</param> /// <param name="space">计算空间</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> public RationalNum(NumStr num, OperationSpace space = null, int?maxDecimalPlaces = null) : base(num, space, maxDecimalPlaces) { space = space ?? num.Space; if (num.DecimalPlaces == 0) { numerator = new NaturalNumStr(num, space); denominator = new NaturalNumStr(1, space); } else { numerator = new NaturalNumStr(num.RemoveDecimalPoint(), space); denominator = new NaturalNumStr(1, space); denominator.RightShift(num.DecimalPlaces); FractionReduction(); } }
public void PlusTest() { var a1 = new NumStr("21000") + new NumStr("3.277"); Assert.AreEqual(a1.ToString(), "21003.277"); a1 = new NumStr("98.023") + new NumStr("3.977"); Assert.AreEqual(a1.ToString(), "102"); a1 = new NumStr("-98.023") + new NumStr("3.977"); Assert.AreEqual(a1.ToString(), "-94.046"); a1 = new NumStr("98.023") + new NumStr("-3.977"); Assert.AreEqual(a1.ToString(), "94.046"); a1 = new NumStr("-98.023") + new NumStr("-3.977"); Assert.AreEqual(a1.ToString(), "-102"); a1 = new NumStr("-98.023"); a1 = a1 + a1; Assert.AreEqual(a1.ToString(), "-196.046"); var s = new OperationSpace(10, 16); a1 = new NumStr("7B", s) + new NumStr("3C", s); Assert.AreEqual(a1.ToString(), "B7"); }
/// <summary> /// 用整数初始化,如果为负数则保存绝对值 /// </summary> /// <param name="num">整数</param> /// <param name="space">计算空间</param> public NaturalNumStr(UInt64 num, OperationSpace space = null) : base(num, space, 0) { }
public abstract IOperationSpaceElement ChangeOperationSpace(OperationSpace space = null, bool?newInstance = null, int?maxDecimalPlaces = null);
/// <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) { return(new NaturalNumStr(ChangeOS(space, newInstance, 0), false)); }
public override IOperationSpaceElement ChangeOperationSpace(OperationSpace space = null, bool?newInstance = null, int?maxDecimalPlaces = null) { throw new NotImplementedException(); }
/// <summary> /// 拷贝初始化 /// </summary> /// <param name="num">原始对象</param> /// <param name="maxDecimalPlaces">最大保留小数位数</param> public Digitable(Digitable num, int?maxDecimalPlaces = null) { PositiveOrNegative = num.PositiveOrNegative; Space = num.Space; MaxDecimalPlaces = maxDecimalPlaces.HasValue ? Math.Min(maxDecimalPlaces.Value, this.Space.DefaultMaxDecimalPlaces) : num.MaxDecimalPlaces; }
/// <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) { return(ChangeOS(space, newInstance, maxDecimalPlaces)); }
/// <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> /// 用纯数字初始化 /// </summary> /// <param name="num">数字</param> /// <param name="positiveOrNegative">正负性</param> /// <param name="space">计算空间</param> public IntegerNum(NaturalNumStr num, int?positiveOrNegative = null, OperationSpace space = null) : base(num, space, 0) { PositiveOrNegative = positiveOrNegative ?? num.PositiveOrNegative; }
/// <summary> /// 用纯数字初始化(小数部分会被截断) /// </summary> /// <param name="num">数字</param> /// <param name="space">计算空间</param> public IntegerNum(NumStr num, OperationSpace space = null) : base(new NaturalNumStr(num, false), space, 0) { PositiveOrNegative = num.PositiveOrNegative; }
/// <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(); }