public void UInt128Overflow() { UInt128B two = 2; unchecked { Assert.IsTrue(UInt128B.MaxValue + UInt128B.One == UInt128B.Zero); // 99 + 1 = [1]00 Assert.IsTrue(UInt128B.MaxValue + UInt128B.MaxValue == UInt128B.MaxValue - UInt128B.One); // 99 + 99 = [1]98 Assert.IsTrue(UInt128B.Zero - UInt128B.One == UInt128B.MaxValue); // [1]00 - 1 = 99 Assert.IsTrue(UInt128B.Zero - UInt128B.MaxValue == UInt128B.One); // [1]00 - 99 = 1 Assert.IsTrue(UInt128B.MaxValue * two == UInt128B.MaxValue - UInt128B.One); // 99 * 2 = [1]98 Assert.IsTrue(UInt128B.MaxValue * UInt128B.MaxValue == UInt128B.One); // 99 * 99 = [98]01 } }
public void BTest() { UInt128B m2 = UInt128B.Multiply(UInt128B.MaxValue, UInt128B.MaxValue); UInt128B n2 = UInt128B.Add(UInt128B.MaxValue, UInt128B.MaxValue); uint r; UInt128B.DivRem(UInt64.MaxValue, 12345, out r); UInt128B n = UInt128B.Parse("123456789012345678901234567891234567890"); UInt128B d = UInt128B.Parse("9876543210987654321"); UInt128B q = n / d; }
public void UInt128Equality() { UInt128B zero = UInt128B.Zero; UInt128B one = UInt128B.One; UInt128B m = UInt64.MaxValue; UInt128B n = m + 1; Assert.IsTrue(UInt128B.MinValue == UInt128B.Zero); Assert.IsTrue(UInt128.Equals(UInt128.MaxValue, UInt128.MaxValue)); Assert.IsTrue(UInt128B.One.Equals((object)UInt128B.One)); Assert.IsTrue(zero != one); Assert.IsTrue(!one.Equals(zero)); Assert.IsTrue(m + one == n); Assert.IsTrue(n.Equals(m + one)); Assert.IsTrue(!m.Equals(null)); Assert.IsTrue((m + one).GetHashCode() == n.GetHashCode()); }
public void UInt128Comparison() { UInt128B zero = UInt128B.Zero; UInt128B one = UInt128B.One; UInt128B n = ((UInt128B)UInt64.MaxValue) + 1; UInt128B p = UInt128B.MaxValue - 1; Assert.IsTrue(zero < one); Assert.IsTrue(zero < n); Assert.IsTrue(one > zero); Assert.IsTrue(one < n); Assert.IsTrue(n > one); Assert.IsTrue(n < p); Assert.IsTrue(p >= n); Assert.IsTrue(n.CompareTo(n) == 0); Assert.IsTrue(n.CompareTo(p) == -1); }
public void CompareToBigInteger() { Random rng = new Random(1); UInt128[] values = GetRandomUInt128(rng, 1000).ToArray(); Stopwatch t1 = Stopwatch.StartNew(); for (int i = 0; i < values.Length; i++) { for (int j = 0; j < values.Length; j++) { UInt128 q = UInt128.DivRem(values[i], values[j], out _); //UInt128 sum = values[i] - values[j]; } } t1.Stop(); Console.WriteLine(t1.ElapsedMilliseconds); BigInteger[] bigs = new BigInteger[values.Length]; for (int i = 0; i < bigs.Length; i++) { bigs[i] = BigInteger.Parse(values[i].ToString()); } Stopwatch t2 = Stopwatch.StartNew(); for (int i = 0; i < values.Length; i++) { for (int j = 0; j < values.Length; j++) { BigInteger r; BigInteger q = BigInteger.DivRem(bigs[i], bigs[j], out r); //BigInteger q = BigInteger.Add(bigs[i], bigs[j]); //BigInteger sum = bigs[i] - bigs[j]; } } t2.Stop(); Console.WriteLine(t2.ElapsedMilliseconds); UInt128B[] bValues = new UInt128B[values.Length]; for (int i = 0; i < bValues.Length; i++) { uint s0 = (uint)rng.Next(); uint s1 = (uint)rng.Next(); uint s2 = (uint)rng.Next(); uint s3 = (uint)rng.Next(); bValues[i] = new UInt128B(s3, s2, s1, s0); } Stopwatch t3 = Stopwatch.StartNew(); for (int i = 0; i < values.Length; i++) { for (int j = 0; j < values.Length; j++) { //UInt128B p = bValues[i] - bValues[j]; UInt128B q = bValues[i] / bValues[j]; } } t3.Stop(); Console.WriteLine(t3.ElapsedMilliseconds); }