// END OPERATORS // OPERATORS SUPPORT METHODS private static BigNumber Quantevolte(BigNumber A, BigNumber B) { BigNumber counter = new BigNumber("0"); int Acount = A.Number.Count(); int Bcount = B.Number.Count(); //Costante di riduzione dei cicli BigNumber moltiplicatore = new BigNumber("10"); int riduttore = 0; while (riduttore < (Acount - Bcount - 2)) { riduttore++; moltiplicatore *= 10; } riduttore++; BigNumber R; BigNumber NB = B; BigNumber conta = new BigNumber("0"); while (NB.Number.Count() < (Acount - riduttore)) { NB *= moltiplicatore; conta++; } bool is_zero = (conta == 0); if (!is_zero) { int y = Convert.ToInt32(conta.ToString()); conta = new BigNumber("1"); for (int x = 0; x < y; x++) { conta *= moltiplicatore; } } R = A; while (!(R = (R - NB)).Negative) { counter++; } R = A - (NB * counter); if (!is_zero) { counter *= conta; } BigNumber S = R; while (B.Number.Count() < R.Number.Count()) { S = Quantevolte(R, B); counter += S; R = A - (counter * B); } //ultime cifre while (!(R = (R - B)).Negative) { counter++; } return(counter); }
public static BigNumber operator *(BigNumber A, BigNumber B) { //Controlli pre-prodotto if ((A == 0) || (B == 0)) { return(new BigNumber("")); } //fine controlli BigNumber T = new BigNumber(""); List <byte>[] Res = new List <byte> [B.Number.Count()]; int count = 0; byte riporto = 0; foreach (byte Bbyte in B.Number) { riporto = 0; Res[count] = new List <byte>(); for (int x = 0; x < count; x++) { Res[count].Add(0); } foreach (byte Abyte in A.Number) { byte N = (byte)((Abyte * Bbyte) + riporto); byte n = 0; if (N > 9) { riporto = (byte)(N / 10); n = (byte)(N - (riporto * 10)); } else { riporto = 0; n = N; } Res[count].Add(n); } if (riporto > 0) { Res[count].Add(riporto); } count++; } foreach (List <byte> LB in Res) { T = T + new BigNumber(LB, false); } if (A.Negative ^ B.Negative) { T.Negative = true; } return(T); }
public static BigNumber operator -(BigNumber A, BigNumber B) { if (A.Negative && !B.Negative) { BigNumber C = -A; return(-(B + C)); } if (B.Negative && !A.Negative) { BigNumber C = -B; return(A + C); } if (B.Negative && A.Negative) { BigNumber C = -A; B = C; A = -B; } List <byte> LB = new List <byte>(); Boolean negative = false; if (A < B) { negative = true; BigNumber C = B; B = A; A = C; } byte riporto = 0; for (int x = 0; x < B.Number.Count(); x++) { byte n = 0; if (B.Number[x] > (A.Number[x] - riporto)) { n = (byte)((10 + A.Number[x]) - B.Number[x] - riporto); riporto = 1; } else { n = (byte)(A.Number[x] - B.Number[x] - riporto); riporto = 0; } LB.Add(n); } int y = B.Number.Count(); while (y < A.Number.Count()) { byte n = 0; if ((A.Number[y] - riporto) < 0) { n = (byte)((10 + A.Number[y]) - riporto); riporto = 1; } else { n = (byte)(A.Number[y] - riporto); riporto = 0; } LB.Add(n); y++; } BigNumber BN = new BigNumber(LB, false); if (negative) { BN.Negative = true; } CleanZeros(BN); return(BN); }
// END OVERRIDES // OPERATORS public static BigNumber operator +(BigNumber A, BigNumber B) { if (A.Negative && !B.Negative) { BigNumber C = -A; return(B - C); } if (B.Negative && !A.Negative) { BigNumber C = -B; return(A - C); } List <byte> Result = new List <byte>(); byte riporto = 0; if (A.Number.Count() >= B.Number.Count()) { int a = -1; for (int x = 0; x < B.Number.Count(); x += 1) { byte sum = (byte)(A.Number[x] + B.Number[x] + riporto); if (sum > 9) { sum -= 10; riporto = 1; } else { riporto = 0; } Result.Add(sum); a = x + 1; } while (a < A.Number.Count()) { if (riporto == 1) { A.Number[a] += 1; if (A.Number[a] > 9) { A.Number[a] -= 10; riporto = 1; } else { riporto = 0; } Result.Add(A.Number[a]); } else { Result.Add(A.Number[a]); } a++; } } else { int a = -1; for (int x = 0; x < A.Number.Count(); x += 1) { byte sum = (byte)(A.Number[x] + B.Number[x] + riporto); if (sum > 9) { sum -= 10; riporto = 1; } else { riporto = 0; } Result.Add(sum); a = x + 1; } while (a < B.Number.Count()) { if (riporto == 1) { B.Number[a] += 1; if (B.Number[a] > 9) { B.Number[a] -= 10; riporto = 1; } else { riporto = 0; } Result.Add(B.Number[a]); } else { Result.Add(B.Number[a]); } a++; } } Result.Add(riporto); BigNumber SS = new BigNumber(Result, (A.Negative && B.Negative)); return(SS); }