/// <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> /// 不考虑精度、误差情况下的除法,会一直除到两者的有效位数之和 /// <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); }