/// <summary> /// 乘上一个一位数,未考虑精度取舍问题 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> private static SKSpecialDecimal mul_single(SKSpecialDecimal _a, byte b) { SKSpecialDecimal a = new SKSpecialDecimal(_a); if (b > 9) { throw new Exception("必须是一位数啊!"); } if (a.get_digit() == 0 || a.is_zero() || b == 1) { return(new SKSpecialDecimal(a)); } else if (b == 0) { return(new SKSpecialDecimal(0)); } SKSpecialDecimal ret = new SKSpecialDecimal(); byte carry = 0; for (int i = a.get_digit() - 1; i > -1; i--) { byte tmp = (byte)(a[i] * b + carry); ret[i + 1] = (byte)(tmp % 10); carry = (byte)(tmp / 10); } ret[0] = carry; ret.fix(); if (!ret.is_zero()) { ret.exp_10 = (carry == 0) ? a.get_exp() : (a.get_exp() + 1); } return(ret); }
/// <summary> /// 不考虑精度、误差情况下的乘法 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> private static SKSpecialDecimal mul(SKSpecialDecimal _a, SKSpecialDecimal _b) { SKSpecialDecimal a = new SKSpecialDecimal(_a); SKSpecialDecimal b = new SKSpecialDecimal(_b); if (a.get_digit() == 0) { return(new SKSpecialDecimal(b)); } else if (b.get_digit() == 0) { return(new SKSpecialDecimal(a)); } else if (a.is_zero() || b.is_zero()) { return(new SKSpecialDecimal(0)); } SKSpecialDecimal ret = new SKSpecialDecimal(0); SKSpecialDecimal longer, shorter; if (a.get_digit() > b.get_digit()) { longer = a; shorter = b; } else { longer = b; shorter = a; } for (int i = 0; i < shorter.get_digit(); i++) { SKSpecialDecimal tmp = mul_single(longer, shorter[i]); tmp.mul_10(shorter.get_exp() - i); ret = add(ret, tmp); } ret.fix(); ret.positive = (a.get_positive() == b.get_positive()); return(ret); }
/// <summary> /// 不考虑精度、误差情况下的加法 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> private static SKSpecialDecimal add(SKSpecialDecimal _a, SKSpecialDecimal _b) { SKSpecialDecimal a = new SKSpecialDecimal(_a); SKSpecialDecimal b = new SKSpecialDecimal(_b); if (a.get_digit() == 0 || a.is_zero()) { return(new SKSpecialDecimal(b)); } else if (b.get_digit() == 0 || b.is_zero()) { return(new SKSpecialDecimal(a)); } SKSpecialDecimal ans = new SKSpecialDecimal(); int compare_abs = abs(a).compare_to(abs(b)); ans.exp_10 = Math.Max(a.get_exp(), b.get_exp()) + 1; int up_range = ans.exp_10; int down_range = Math.Min(a.get_exp() - a.get_digit(), b.get_exp() - b.get_digit()) + 1; int range = up_range - down_range + 1; int carry = 0; if (a.positive == b.positive) { ans.positive = a.positive; for (int i = range - 1; i > -1; i--) { byte tmp = (byte)((a[i - ans.get_exp() + a.get_exp()] + b[i - ans.get_exp() + b.get_exp()] + carry)); ans[i] = (byte)(tmp % 10); if (tmp > 9) { carry = 1; } else { carry = 0; } } } else if (compare_abs == 0) { ans[0] = 0; } else { SKSpecialDecimal bigger, smaller; if (compare_abs > 0) { ans.positive = a.positive; bigger = a; smaller = b; } else { ans.positive = b.positive; bigger = b; smaller = a; } for (int i = range - 1; i > -1; i--) { sbyte tmp = (sbyte)(((sbyte)bigger[i - ans.get_exp() + bigger.get_exp()] - (sbyte)smaller[i - ans.get_exp() + smaller.get_exp()] - carry)); ans[i] = (byte)((tmp + 10) % 10); if (tmp < 0) { carry = 1; } else { carry = 0; } } } ans.fix(); return(ans); }
/// <summary> /// 不考虑精度、误差情况下的除法,会一直除到两者的有效位数之和 /// <para>a除以b</para> /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> private static SKSpecialDecimal div(SKSpecialDecimal _a, SKSpecialDecimal _b) { SKSpecialDecimal a = new SKSpecialDecimal(_a); SKSpecialDecimal b = new SKSpecialDecimal(_b); if (a.get_digit() == 0 || b.get_digit() == 0 || b.is_zero()) { return(new SKSpecialDecimal()); } else if (a.is_zero()) { return(new SKSpecialDecimal(0)); } SKSpecialDecimal ret = new SKSpecialDecimal(); SKSpecialDecimal tmp = new SKSpecialDecimal(b); tmp.exp_10 = a.get_exp(); bool first_zero = (abs(tmp).compare_to(abs(a)) > 0); ret.positive = (a.get_positive() == b.get_positive()) ? true : false; ret.exp_10 = a.get_exp() - b.get_exp(); bool stop = false; SKSpecialDecimal accumulate = new SKSpecialDecimal(0); a = abs(a); b = abs(b); for (int i = (first_zero) ? 1 : 0; i < a.get_digit() + b.get_digit(); i++) { for (int j = 1; j < 10; j++) { SKSpecialDecimal mul_tmp2 = mul_single(b, (byte)j); mul_tmp2.mul_10(ret.get_exp() - i); SKSpecialDecimal now_acc = add(accumulate, mul_tmp2); //ret[i] = (byte)j; int compare_ans = now_acc.compare_to(a); if (compare_ans == 0) { ret[i] = (byte)j; stop = true; break; } else if (compare_ans > 0) { ret[i] = (byte)(j - 1); mul_tmp2 = mul_single(b, (byte)(j - 1)); mul_tmp2.mul_10(ret.get_exp() - i); accumulate = add(accumulate, mul_tmp2); break; } else if (j == 9) { ret[i] = (byte)j; accumulate = add(accumulate, mul_tmp2); } } if (stop) { for (int j = i + 1; j < a.get_digit() + b.get_digit(); j++) { ret[j] = 0; } break; } } ret.fix(); return(ret); }