public void IntNextPrimeCandidate() { using (var a = new HugeInt("622288097498926496141095869268883999563096063592498055290460")) using (var random = MpirRandom.Default()) { Assert.AreEqual(a + 1, a.NextPrimeCandidate(random)); } }
public void RandomHugeFloatLimbsChunky() { using (var r = MpirRandom.Default()) using (var a = HugeFloat.Allocate(256)) { r.Seed(12345789); a.Value = r.GetFloatLimbsChunky(128 / MpirSettings.BITS_PER_LIMB, 100); Assert.AreEqual(Platform.Select("0.7FFFFFF8000007FFF@2C1", "0.7FFFFFFFFFC000000003FFFFF@2A1"), a.ToString(16)); } }
public void IntIsLikelyPrime() { using (var a = new HugeInt("622288097498926496141095869268883999563096063592498055290461")) using (var random = MpirRandom.Default()) { Assert.IsTrue(a.IsLikelyPrime(random, 0)); a.Value = a * 2; Assert.IsFalse(a.IsLikelyPrime(random, 0)); } }
public void RandomHugeFloatBits() { using (var r = MpirRandom.Default()) using (var a = HugeFloat.Allocate(256)) { r.Seed(12345789); a.Value = r.GetFloatBits(128); Assert.AreEqual("0.A0B96F29D0C8767A88F8937D6F3A00FD@0", a.ToString(16)); } }
public void RandomHugeFloatChunky() { using (var r = MpirRandom.Default()) using (var a = HugeFloat.Allocate(256)) { r.Seed(12345789); a.Value = r.GetFloatChunky(100); Assert.AreEqual(Platform.Select("0.7FFFFFFF0180000000000000000007FFFFFFFFFFFFFFFFFFF@-2EF", "0.7FFFFFFFFFFFF00000000001FFE000000000000000000000007FFFFFF@29"), a.ToString(16)); } }
public void RandomHugeFloat() { using (var r = MpirRandom.Default()) using (var a = HugeFloat.Allocate(256)) { r.Seed(12345789); a.Value = r.GetFloat(); Assert.AreEqual("0.9E056474F27BEDF9AE62FB31A30B68DFA0B96F29D0C8767A88F8937D6F3A00FD@0", a.ToString(16)); } }
public void RandomHugeIntBitsChunky() { using (var r = MpirRandom.Default()) using (var a = new HugeInt()) { r.Seed(12345789); a.Value = r.GetIntBitsChunky(256); Assert.AreEqual("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC0000000000000000000000000000007F", a.ToString(16)); } }
public void RandomHugeInt1() { using (var r = MpirRandom.Default()) using (var a = new HugeInt()) { r.Seed(12345789); a.Value = r.GetInt((a + 2345234534) ^ 10); Assert.AreEqual("1157052590454954734126533665801738434624905444540864190630086248362027895885687865247828410621", a.ToString()); } }
public void RandomHugeIntBits() { using (var r = MpirRandom.Default()) using (var a = new HugeInt()) { r.Seed(12345789); a.Value = r.GetIntBits(256); Assert.AreEqual("9E056474F27BEDF9AE62FB31A30B68DFA0B96F29D0C8767A88F8937D6F3A00FD", a.ToString(16)); } }
public void FloatTestAllExpressions() { var baseExpr = typeof(FloatExpression); var allExpressions = baseExpr.Assembly.GetTypes() .Where(x => baseExpr.IsAssignableFrom(x) && !x.IsAbstract) .ToList(); var one = Platform.Ui(1UL, 1U); using (var a = new HugeFloat(-9)) using (var b = new HugeFloat(4)) using (var c = new HugeInt(3)) using (var r = MpirRandom.Default()) { var expr = a + (-a * 2) * 3 * (a.Abs() * -2 + -64 + a * a) + (one * 116U) + a; VerifyPartialResult(r, expr, 44); expr = expr + a * 5 + (a + b) * (b + 1) * (b + -3) * b + (b * -a) - (b * (one * 25U)) - a + (b << 3) - ((a * b) << 1); VerifyPartialResult(r, expr, -52); expr = expr - 2 - 3U + (b - (a << 1)) + (b * b - 15U) * (b - a) * (a - 11) * (b - (one * 3U)) - (-340 - a) + ((one * 20U) - b); VerifyPartialResult(r, expr, 52); expr = expr + (-7 - 2 * a) + (28U - 4 * b) + -(a + b * 2) + (3 * a).Abs(); VerifyPartialResult(r, expr, 103); expr = 36 * (expr / a + expr / (3 * b) - a / b) - b / (a + 10) + 6; VerifyPartialResult(r, expr, -20); expr = expr + (b >> 1) + ((b / -7) + (a / (one * 7U))) * 7 + (7 / a) - ((one * 2U) / (b + 5)); VerifyPartialResult(r, expr, -32); expr = expr - (b + 13 + 64) / a / -3; VerifyPartialResult(r, expr, -35); expr = expr + b.SquareRoot() + HugeFloat.SquareRoot(25) + ((b - 2) ^ 3) - (-b).RelativeDifferenceFrom(a + 1); VerifyPartialResult(r, expr, -19); expr = expr - (a / 4).Floor() + (b / 3).Ceiling() - (a / b).Truncate(); VerifyPartialResult(r, expr, -12); expr = expr + (r.GetFloatBits(64) * 10).Ceiling(); VerifyPartialResult(r, expr, -10); //float random generation seems to give different results in Win32 and Win64. Thus, we're having to adjust the results for Win32. expr = expr + (r.GetFloatLimbsChunky(128 / MpirSettings.BITS_PER_LIMB, 256 / MpirSettings.BITS_PER_LIMB) << 233 >> Platform.Ui(0, 480)).Ceiling(); VerifyPartialResult(r, expr, -6); expr = expr + (r.GetFloat() * 10).Floor() - Platform.Ui(0, 3); VerifyPartialResult(r, expr, -2); expr = expr + (r.GetFloatChunky(3) << 101 >> Platform.Ui(177, 23)).Truncate(); VerifyPartialResult(r, expr, 13); MarkExpressionsUsed(allExpressions, expr); } Assert.AreEqual(0, allExpressions.Count, "Expression types not exercised: " + string.Join("", allExpressions.Select(x => Environment.NewLine + x.Name).OrderBy(x => x))); }
public void IntTestAllExpressions() { var baseExpr = typeof(IntegerExpression); var allExpressions = baseExpr.Assembly.GetTypes() .Where(x => baseExpr.IsAssignableFrom(x) && !x.IsAbstract) .ToList(); var one = Platform.Ui(1, 1); using (var a = new HugeInt(-9L)) using (var b = new HugeInt(4L)) using (var c = new HugeRational(6, 7)) using (var r = MpirRandom.Default()) { var expr = a + (-a * 2) * 3 * (a.Abs() * -2 + -64 + a * a) + (one * 116U) + a; VerifyPartialResult(r, expr, 44); expr = expr + a * 5 + (a + b) * (b + 1) * (b + -3) * b + (b * -a) - (b * (one * 25U)) - a + (b << 3) - ((a * b) << 1); VerifyPartialResult(r, expr, -52); expr = expr - 2 - 3U + (b - (a << 1)) + (b * b - (one * 15U)) * (b - a) * (a - 11) * (b - 3U) - (-340 - a) + ((one * 20U) - b); VerifyPartialResult(r, expr, 52); expr = expr + (-7 - 2 * a) + (28U - 4 * b) + -(a + b * 2) + (3 * a).Abs(); VerifyPartialResult(r, expr, 103); expr = expr / a + expr / (3 * b) - a / b - b / (a + 10) + a % b - (3 * b) % a + a % (2 * b) - (12 * b) % (-5 * a) + (a * 4 / 8).Rounding(RoundingModes.Floor) + (b * 3 % 7).Rounding(RoundingModes.Ceiling); VerifyPartialResult(r, expr, -20); expr = expr - (a * 5).DivideExactly(a) + (b * 7 * 5432198).DivideExactly(5432198) + (b >> 1); VerifyPartialResult(r, expr, 5); expr = expr + (b ^ 3) + a.PowerMod(2, b) + (a + 6).PowerMod(b - 1, b * 5) + (a * a * a).Root(3) + (b * b).SquareRoot(); VerifyPartialResult(r, expr, 78); expr = expr + ((b + 1) & -a) + (b | -a) - (b ^ a) + ~b; VerifyPartialResult(r, expr, 100); expr = expr + r.GetInt(b + 1) + r.GetIntBits(3) + r.GetIntBitsChunky(3) + (b * 2).NextPrimeCandidate(r) - b.Gcd(a - 1); VerifyPartialResult(r, expr, 124); expr = expr - a.Lcm(b * 3) - (b + 1).Lcm(2) - (-a).Invert(b + 7) - (1 - a).RemoveFactors(b / 2) - HugeInt.Power(2, 3) - HugeInt.Factorial(4); VerifyPartialResult(r, expr, 36); expr = expr - HugeInt.Primorial(6) + HugeInt.Binomial(4, 2) + HugeInt.Binomial(b, 3) + HugeInt.Fibonacci(6) + HugeInt.Lucas(7); VerifyPartialResult(r, expr, 53); expr = expr + c.Numerator + c.Denominator; VerifyPartialResult(r, expr, 66); MarkExpressionsUsed(allExpressions, expr); } Assert.AreEqual(0, allExpressions.Count, "Expression types not exercised: " + string.Join("", allExpressions.Select(x => Environment.NewLine + x.Name).OrderBy(x => x))); }
public void RandomSeedingDefault() { using (var r = MpirRandom.Default()) { uint seed = 12345789; uint max = 10000000; uint bits = 62; uint expected1 = 3801341; uint expected2 = 747743; var expected3 = Platform.Ui(3637762780660169521, 2925722417); TestRandom(r, seed, x => { Assert.AreEqual(expected1, x.GetLimb(max)); Assert.AreEqual(expected2, x.GetLimb(max)); Assert.AreEqual(expected3, x.GetLimbBits(bits)); }); } }
public void RationalTestAllExpressions() { var baseExpr = typeof(RationalExpression); var allExpressions = baseExpr.Assembly.GetTypes() .Where(x => baseExpr.IsAssignableFrom(x) && !x.IsAbstract) .ToList(); var one = Platform.Ui(1, 1); using (var a = new HugeRational(-9, 1)) using (var b = new HugeRational(4, 1)) using (var c = new HugeInt(3)) using (var r = MpirRandom.Default()) { var expr = a + (-a * 2) * 3 * (a.Abs() * -2 + -64 + a * a) + (one * 116U) + a; VerifyPartialResult(r, expr, 44); expr = expr + a * 5 + (a + b) * (b + 1) * (b + -3) * b + (b * -a) - (b * (one * 25U)) - a + (b << 3) - ((a * b) << 1); VerifyPartialResult(r, expr, -52); expr = expr - 2 - 3U + (b - (a << 1)) + (b * b - (one * 15U)) * (b - a) * (a - 11) * (b - 3U) - (-340 - a) + ((one * 20U) - b); VerifyPartialResult(r, expr, 52); expr = expr + (-7 - 2 * a) + (28U - 4 * b) + -(a + b * 2) + (3 * a).Abs(); VerifyPartialResult(r, expr, 103); expr = 36 * (expr / a + expr / (3 * b) - a / b) - b / (a + 10) + 6; VerifyPartialResult(r, expr, -20); expr = expr + (b >> 1) + ((b / -7) + (a / (one * 7U))) * 7 + (7 / a) - ((one * 2U) / (b + 5)); VerifyPartialResult(r, expr, -32); expr = expr + (((a / b).Invert() * 3) ^ 3) - (b + 13) / a / -3; VerifyPartialResult(r, expr, -35); expr = expr + c + (b - 2 * c) + (-4 * c - a) - (c - 1) * (b - 1) - (a / c) + (c * 2) / (b - 1); VerifyPartialResult(r, expr, -38); MarkExpressionsUsed(allExpressions, expr); } Assert.AreEqual(0, allExpressions.Count, "Expression types not exercised: " + string.Join("", allExpressions.Select(x => Environment.NewLine + x.Name).OrderBy(x => x))); }