private static bool _isZero(Integer n) { return n._digits != null && n._digits.Count == 1 && n._digits[0] == 0; }
public bool Equals(Integer i) { if (i._sign != _sign) { return false; } return _magCmpEQ(_digits, i._digits); }
public override bool Equals(object obj) { if (obj is Integer) { return Equals((Integer)obj); } else { Integer i = new Integer(); try { i = new Integer(obj.ToString()); } catch { return false; } return Equals(i); } }
public static Integer New(Integer n) { Integer r = new Integer(); r._sign = n._sign; r._digits = new List<byte>(n._digits.ToArray()); return r; }
public static Integer Subtract(Integer n1, Integer n2) { return Add(n1, -n2); }
public static Integer Multiply(Integer n1, Integer n2) { return new Integer(_magMul(n1._digits, n2._digits), n1._sign == n2._sign ? Sign.Positive : Sign.Negative); }
//Factory Methods public static Integer New(string s) { if (_valid(s)) { Integer n = new Integer(); int i = 0; if (s.StartsWith("+") || s.StartsWith("-")) { n._sign = _toSign(s[0]); i = 1; } else { n._sign = Sign.Positive; } while (i < s.Length && s[i] == '0') { i++; } if (i == s.Length) { return n; } n._digits = new List<byte>(s.Length - i); for (int j = 0; j < s.Length - i; j++) { n._digits.Add(0); } for (int x = s.Length - 1; x >= i; x--) { n._digits[s.Length - 1 - x] = _byte(s[x]); } return n; } else { throw new Exception("String could not be parsed into a valid Integer!"); } }
public static Integer Divide(Integer n1, Integer n2) { return new Integer(_magDiv(n1._digits, n2._digits), n1._sign == n2._sign ? Sign.Positive : Sign.Negative); }
//Mathematical Operators public static Integer Add(Integer n1, Integer n2) { if (n1._sign == n2._sign) { return new Integer(_magAdd(n1._digits, n2._digits), n1._sign); } else { if (_magCmpEQ(n1._digits, n2._digits)) { return new Integer(new List<byte>(new byte[] { 0 }), Sign.Positive); } List<byte> larger, smaller; Sign sign = Sign.Positive; if (_magCmpGT(n1._digits, n2._digits)) { larger = n1._digits; smaller = n2._digits; sign = n1._sign; } else { larger = n2._digits; smaller = n1._digits; sign = n2._sign; } return new Integer(_magSub(larger, smaller), sign); } }
public Integer(Integer copy) { _copyInit(New(copy)); }
public static Integer operator -(Integer n) { Integer res = new Integer(n); res._sign = res._sign == Sign.Positive ? Sign.Negative : Sign.Positive; return res; }
private void _copyInit(Integer n) { _sign = n._sign; _digits = n._digits; }