/// <summary> /// 把string转化为高精度数 /// </summary> /// <param name="a"></param> /// <returns></returns> public BigNumber StringToBn(string a) { BigNumber b = new BigNumber(); bool Decimal = false; int i, dot = 0; for (i = 0; i < a.Length; i++) { if (a[i] == '.') { dot = i; Decimal = true; break; } } if (a[0] == '-') { b.positive = -1; if (Decimal) { for (i = dot - 1; i > 0; i--) { b.value[i + 20 - dot] = a[i] - 48; } for (i = dot + 1; i < a.Length; i++) { b.value[i + 19 - dot] = a[i] - 48; } } else { for (i = 1; i < a.Length; i++) { b.value[20 + i - a.Length] = a[i] - 48; } } return(b); } else { b.positive = 1; if (Decimal) { for (i = dot - 1; i >= 0; i--) { b.value[i + 20 - dot] = a[i] - 48; } for (i = dot + 1; i < a.Length; i++) { b.value[i + 19 - dot] = a[i] - 48; } } else { for (i = 0; i < a.Length; i++) { b.value[20 + i - a.Length] = a[i] - 48; } } return(b); } }
/// <summary> /// 求平方 /// </summary> /// <param name="a"></param> /// <returns></returns> public static BigNumber square(BigNumber a) { return(copy(a * a)); }
/// <summary> /// /重载:一个高精度数除以一个double型的数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator /(BigNumber a, double b) { BigNumber c = Trans(b); return(a / c); }
/// <summary> /// /重载:一个double型的数除以一个高精度数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator /(double a, BigNumber b) { BigNumber c = Trans(a); return(c / b); }
/// <summary> /// -重载:一个int型减一个高精度数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator -(int a, BigNumber b) { BigNumber c = Trans(a); return(c - b); }
/// <summary> /// *重载:一个double型乘一个高精度数 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator *(double a, BigNumber b) { BigNumber c = Trans(a); return(b * c); }
/// <summary> /// +重载:一个高精度数加一个int型 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator +(BigNumber a, int b) { BigNumber c = Trans(b); return(a + c); }
/// <summary> /// -重载:两个高精度数相减 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator -(BigNumber a, BigNumber b) { BigNumber c = invert(b); return(a + c); }
/// <summary> /// +重载:两个高精度数相加 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BigNumber operator +(BigNumber a, BigNumber b) { BigNumber c = new BigNumber(); int flag = a.positive * b.positive; if (flag == 1) { c.positive = a.positive; int carry = 0; for (int i = 59; i >= 0; i--) { c.value[i] = a.value[i] + b.value[i] + carry; if (c.value[i] > 9) { c.value[i] = c.value[i] - 10; carry = 1; } else { carry = 0; } } } else { if (abs(a) > abs(b)) { c.positive = a.positive; int borrow = 0; for (int i = 59; i >= 0; i--) { c.value[i] = a.value[i] - b.value[i] - borrow; if (c.value[i] >= 0) { borrow = 0; } else { c.value[i] += 10; borrow = 1; } } } else { c.positive = b.positive; int borrow = 0; for (int i = 59; i >= 0; i--) { c.value[i] = b.value[i] - a.value[i] - borrow; if (c.value[i] >= 0) { borrow = 0; } else { c.value[i] += 10; borrow = 1; } } } } if (c.zero(c)) { c.positive = 1; } return(c); }
/// <summary> /// e^x高精度运算 /// </summary> /// <param name="a"></param> /// <returns></returns> public BigNumber exp(BigNumber a) { BigNumber e = new BigNumber(); int i; for (i = 0; i < 18; i++) { e.value[i] = 0; } e.value[32] = e.value[40] = 0; e.value[21] = e.value[25] = e.value[46] = 1; e.value[19] = e.value[23] = e.value[27] = e.value[35] = e.value[41] = e.value[49] = e.value[52] = e.value[59] = 2; e.value[36] = e.value[38] = e.value[47] = 3; e.value[29] = e.value[33] = e.value[44] = e.value[53] = 4; e.value[30] = e.value[34] = e.value[37] = e.value[48] = e.value[57] = 5; e.value[39] = e.value[50] = e.value[51] = 6; e.value[20] = e.value[43] = e.value[45] = e.value[55] = e.value[56] = e.value[58] = 7; e.value[22] = e.value[24] = e.value[26] = e.value[28] = e.value[42] = 8; e.value[31] = e.value[54] = 9; //Trans(2.7182818284590452353602874713526624977572); BigNumber Integer = new BigNumber(); BigNumber Decimal = new BigNumber(); for (i = 0; i < 20; i++) { Integer.value[i] = a.value[i]; } for (i = 20; i < 60; i++) { Decimal.value[i] = a.value[i]; } BigNumber temp = Trans(1); BigNumber temp1 = copy(Decimal); BigNumber result = Trans(1); i = 1; if (a.zero(a)) { return(Trans(1)); } else if (a.positive == 1) { while (!Integer.zero(Integer)) { temp = temp * e; Integer = Integer - 1; } while (!temp1.zero(temp1)) { result = result + temp1; i++; temp1 = temp1 * Decimal / i; } result = result * temp; return(result); } else { while (!Integer.zero(Integer)) { temp = temp * e; Integer = Integer - 1; } while (!temp1.zero(temp1)) { result = result + temp1; i++; temp1 = temp1 * Decimal / i; } result = 1 / (temp * result); return(result); } }
/// <summary> /// 经典四阶R-K算法 /// </summary> /// <param name="a"></param> /// <returns></returns> public BigNumber RK(BigNumber a) { int i = 0; BigNumber two = new BigNumber(); two.value[19] = 2; if (!(a < two)) { do { a = a / 2; i++; } while (!(a < two)); } BigNumber an = a; BigNumber yn = new BigNumber(); BigNumber xn = new BigNumber(); //Trans(1); xn.value[19] = 1; BigNumber one = new BigNumber(); one.value[19] = 1; BigNumber h = new BigNumber(); //Trans(0.000005); h.value[25] = 5; BigNumber ln2 = new BigNumber(); //0.69314 71805 59945 30941 72321 21458 17656 80718 // 20 24 25 29 30 34 35 39 40 44 45 49 50 54 55 59 ln2.value[23] = ln2.value[26] = ln2.value[39] = ln2.value[44] = ln2.value[46] = ln2.value[50] = ln2.value[58] = 1; ln2.value[41] = ln2.value[43] = ln2.value[45] = 2; ln2.value[22] = ln2.value[35] = ln2.value[42] = 3; ln2.value[24] = ln2.value[33] = ln2.value[38] = ln2.value[47] = 4; ln2.value[29] = ln2.value[30] = ln2.value[34] = ln2.value[48] = ln2.value[53] = 5; ln2.value[20] = ln2.value[52] = ln2.value[54] = 6; ln2.value[25] = ln2.value[40] = ln2.value[51] = ln2.value[57] = 7; ln2.value[27] = ln2.value[49] = ln2.value[55] = ln2.value[59] = 8; ln2.value[21] = ln2.value[31] = ln2.value[32] = ln2.value[37] = 9; if (a > one) { while (xn < an) { yn += ((((1 / xn) + (4 / (xn + (h / 2))) + (1 / (xn + h))) * h) / 6); xn += h; } } else { bool flag = false; set(h, flag); while (xn > an) { yn = yn + ((((1 / xn) + (4 / (xn + (h / 2))) + (1 / (xn + h))) * h) / 6); xn += h; } } BigNumber result = new BigNumber(); result = yn + (ln2 * i); return(result); }