/// <summary> /// Gets this value, times 2 raised to v. /// </summary> public GmpInteger Pow2(uint v) { GmpInteger retVal = new GmpInteger(); Interop.mpz_mul_2exp(ref retVal._Storage, ref _Storage, v); return(retVal); }
/// <summary> /// Gets absolute value of this value. Returns it in a new instance of BigFastInteger. /// </summary> public GmpInteger Abs() { GmpInteger retVal = new GmpInteger(); Interop.mpz_abs(ref retVal._Storage, ref _Storage); return(retVal); }
/// <summary> /// Gets negated value of the op. /// </summary> public static GmpInteger operator -(GmpInteger op) { GmpInteger retVal = new GmpInteger(); Interop.mpz_neg(ref retVal._Storage, ref op._Storage); return(retVal); }
/// <summary> /// Returns lhs / rhs. /// </summary> public static GmpInteger operator /(GmpInteger lhs, GmpInteger rhs) { GmpInteger retVal = new GmpInteger(); Interop.mpz_tdiv_q(ref retVal._Storage, ref lhs._Storage, ref rhs._Storage); return(retVal); }
static void Main(string[] args) { using (new DisposeContext()) { // Arithmatic GmpInteger lhs = new GmpInteger("111111111111111111111111111111111111111111111111111111111111111111111111111"); GmpInteger rhs = new GmpInteger("111111111111111111111111111111111111111111111111111111111111111111111111111"); GmpInteger one = new GmpInteger("1"); GmpInteger add = lhs + rhs; GmpInteger sub = lhs - rhs; GmpInteger neg = -lhs; GmpInteger abs = neg.Abs(); GmpInteger pow = one.Pow2(64); GmpInteger lhsPlus1 = lhs + one; GmpInteger mod = lhsPlus1 % lhs; GmpInteger mul = lhs * rhs; GmpInteger div = lhs / pow; Console.WriteLine($"lhs = {lhs}"); Console.WriteLine($"rhs = {rhs}"); Console.WriteLine($"one = {one}"); Console.WriteLine($"lhs + rhs = {add}"); Console.WriteLine($"lhs - rhs = {sub}"); Console.WriteLine($"neg(lhs) = {neg}"); Console.WriteLine($"abs(neg(lhs)) = {abs}"); Console.WriteLine($"pow(1,64) = {pow}"); Console.WriteLine($"lhs * rhs = {mul}"); Console.WriteLine($"(lhs + 1) % lhs = {mod}"); Console.WriteLine($"lhs / pow(1,65) = {div}"); Console.WriteLine($"lhs = rhs: {lhs == rhs}"); Console.WriteLine($"lhs + rhs > 1: {add > one}"); Console.WriteLine($"lhs + rhs < 1: {add < one}"); Console.WriteLine($"lhs + rhs >= 1: {add >= one}"); Console.WriteLine($"lhs + rhs <= 1: {add <= one}"); Console.WriteLine($"lhs == null: {lhs == null}"); Console.WriteLine($"null == lhs: {null == lhs}"); byte[] exportedValue = null; bool isNegative = false; pow.Export(out exportedValue, out isNegative); Console.WriteLine($"export pow(1,64) = {BitConverter.ToString(exportedValue)}"); Console.WriteLine($"export pow(1,64) is negative = {isNegative}"); // Import and export GmpInteger impTest = new GmpInteger(new byte[] { 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }); Console.WriteLine($"new byte[] {{ 0x1,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }} = {impTest}"); HashSet<GmpInteger> set = new HashSet<GmpInteger>(); set.Add(lhs); set.Add(lhs); set.Add(new GmpInteger("1111")); set.Add(new GmpInteger("1111")); set.Add(new GmpInteger("11111111")); Console.WriteLine($"HashSet<GmpInteger> len = {set.Count}"); } }
/// <summary> /// Gets the number as a byte[] for saving purposes. The sign is returned seperately because GMP uses sign/magniture format. /// Data is exported with most signaficant byte first. /// </summary> public void Export(out byte[] data, out bool isNegative) { // Calculate buffer size based on information from https://gmplib.org/manual/Integer-Import-and-Export.html. const int numb = 8; // * size - nail; int count = (Interop.mpz_sizeinbase(ref _Storage, 2) + numb - 1) / numb; data = new byte[count]; int dataCount = 0; Interop.mpz_export(data, ref dataCount, 1, 1, 0, 0, ref _Storage); GmpInteger zero = new GmpInteger(); isNegative = zero.CompareTo(this) > 0; }
/// <summary> /// Returns lhs / rhs. /// </summary> public static GmpInteger operator /(GmpInteger lhs, GmpInteger rhs) { GmpInteger retVal = new GmpInteger(); Interop.mpz_tdiv_q(ref retVal._Storage, ref lhs._Storage, ref rhs._Storage); return retVal; }
/// <summary> /// Gets negated value of the op. /// </summary> public static GmpInteger operator -(GmpInteger op) { GmpInteger retVal = new GmpInteger(); Interop.mpz_neg(ref retVal._Storage, ref op._Storage); return retVal; }