/// <summary> /// 加法,有效位数取决于绝对误差最大的数 /// <para>例:1.001+100.1=101.1</para> /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static SKSpecialDecimal operator +(SKSpecialDecimal a, SKSpecialDecimal b) { if (a.is_zero()) { return(new SKSpecialDecimal(b)); } else if (b.is_zero()) { return(new SKSpecialDecimal(a)); } SKSpecialDecimal a_copy = new SKSpecialDecimal(a); SKSpecialDecimal b_copy = new SKSpecialDecimal(b); //越小越精确,表示末位所在的位置 SKSpecialDecimal ret = add(a_copy, b_copy); if (CUT_IN_OPERATOR) { int a_min_bit = a_copy.exp_10 - a_copy.get_digit() + 1; int b_min_bit = b_copy.exp_10 - b_copy.get_digit() + 1; int ret_min_bit = ret.exp_10 - ret.get_digit() + 1; if (a_min_bit > b_min_bit)//以a的精度为准 { ret.cut(ret.get_digit() - a_min_bit + ret_min_bit); } else//以b的精度为准 { ret.cut(ret.get_digit() - b_min_bit + ret_min_bit); } } return(ret); }
/// <summary> /// 除法,结果的有效位数与两数有效位数较少的数相同 /// </summary> /// <param name="a">除数</param> /// <param name="b">被除数</param> /// <returns></returns> public static SKSpecialDecimal operator /(SKSpecialDecimal a, SKSpecialDecimal b) { if (a.get_digit() == 0 || b.get_digit() == 0) { return(new SKSpecialDecimal()); } int less = Math.Min(a.get_digit(), b.get_digit()); SKSpecialDecimal ret = div(a, b); if (CUT_IN_OPERATOR) { ret.cut(less); } return(ret); }
/// <summary> /// 在(x-1)位处发生进位,同时截取x位有效数字 /// </summary> /// <param name="x"></param> private void upgrade(int x) { if (x < 2) { return; } data.RemoveRange(x, data.Count - x); SKSpecialDecimal _tmp = new SKSpecialDecimal(this); SKSpecialDecimal _add = new SKSpecialDecimal(); _add[x - 1] = 1; _add.exp_10 = _tmp.get_exp(); _tmp = add(_tmp, _add); if (_tmp.get_digit() > get_digit()) //进位导致位数增加了 { _tmp.cut(get_digit()); //本次cut一定不会导致进位了 } reset(_tmp); }