/// <summary> /// Creator: abs(Integer) /// </summary> /// <param name="x">The Integer</param> /// <returns>The new Integer</returns> public static Integer Abs(Integer x) { if (x.Sign) { x = x.Negate(); } return(x); }
/// <summary> /// Creator: performs this/q (PRE: q>0) /// </summary> /// <param name="q">A byte divisor</param> /// <param name="r">ref remainder</param> /// <returns></returns> internal Integer Quotient(byte d, ref int r) //q>0 { Integer a = this; r = 0; if (a.bytes.Length == 0) { return(a); } bool sgn = Sign; if (sgn) { a = a.Negate(); } byte[] qu = new byte[a.bytes.Length]; for (int j = 0; j < a.bytes.Length; j++) { int n = (r << 8) + a.bytes[j]; qu[j] = (byte)(n / d); r = n % d; } if (qu[0] == 0) { byte[] b = new byte[a.bytes.Length - 1]; for (int j = 0; j < b.Length; j++) { b[j] = qu[j + 1]; } qu = b; } if (qu.Length > 0 && qu[0] > 127) { byte[] b = new byte[qu.Length + 1]; for (int j = 0; j < qu.Length; j++) { b[j + 1] = qu[j]; } b[0] = (byte)0; qu = b; } a = new Integer(qu); if (sgn) { a = a.Negate(); } return(a); }
/// <summary> /// Creator: Integer / Integer /// </summary> /// <param name="a">Dividend</param> /// <param name="b">Divisor</param> /// <returns></returns> public static Integer operator /(Integer a, Integer b) { bool sa = a.Sign; bool sb = b.Sign; bool s = (sa != sb); if (sa) { a = a.Negate(); } if (sb) { b = b.Negate(); } if (b.IsZero() || a < b) { return(Zero); } if (b == a) { return(One); } var ds = new List <Integer>(); for (; ;) { ds.Add(b); Integer c = b.Shift8(); if (c > a) { break; } b = c; } int d = 0; // first work out the most significant digit while (b <= a) { a = a - b; // b is ds[ds.Count-1] d++; } // fix the sign int j = (d > 127) ? 1 : 0; var nb = new byte[ds.Count + j]; nb[j++] = (byte)d; // now do the rest of the digits for (int i = ds.Count - 2; i >= 0; i--) { var dv = ds[i]; d = 0; while (dv <= a) { a = a - dv; d++; } nb[j++] = (byte)d; } var r = new Integer(nb); if (s) { r = r.Negate(); } return(r); }
/// <summary> /// Negate a Decimal /// </summary> /// <param name="a">The Decimal to negate</param> /// <returns>The resulting Decimal</returns> public Numeric Negate() { return(new Numeric(mantissa.Negate(), scale)); }