public void CompareToCalculatedToDefaultPrecision() { using (var a = new HugeFloat(1)) using (var b = new HugeFloat(13)) using (var c = new HugeFloat("12345234589234059823475029384572323")) using (var d = HugeFloat.Allocate(256)) { ShiftLeftBy62(c); d.Value = c; var expr = a / b + c; Assert.AreEqual(0, c.CompareTo(expr)); //to precision of c Assert.AreEqual(0, expr.CompareTo(c)); //to precision of c Assert.IsFalse(expr > c); //to precision of c Assert.IsTrue(c == expr); //to precision of c Assert.AreEqual(0, (c + 0).CompareTo(expr)); //to default precision Assert.AreEqual(0, expr.CompareTo(c + 0)); //to default precision Assert.IsFalse(expr > c + 0); //to default precision Assert.IsTrue(c + 0 == expr); //to default precision HugeFloat.DefaultPrecision = 256; Assert.AreEqual(0, c.CompareTo(expr)); //to precision of c Assert.AreEqual(0, expr.CompareTo(c)); //to precision of c Assert.IsTrue(c == expr); //to precision of c Assert.IsFalse(expr > c); //to precision of c Assert.AreEqual(-1, d.CompareTo(expr)); //to precision of d Assert.AreEqual(1, expr.CompareTo(d)); //to precision of d Assert.IsFalse(d == expr); //to precision of d Assert.IsTrue(expr > d); //to precision of d Assert.AreEqual(-1, (c * 1).CompareTo(expr)); //to default precision Assert.AreEqual(1, expr.CompareTo(c + 0)); //to default precision Assert.IsFalse(c + 0 == expr); //to default precision Assert.IsTrue(expr > c + 0); //to default precision HugeFloat.DefaultPrecision = 128; } }
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 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 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 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 FloatReallocate() { using (var a = HugeFloat.Allocate(128)) { Assert.AreEqual(128UL, a._allocatedPrecision); Assert.AreEqual(128UL, a.Precision); a.Reallocate(256); Assert.AreEqual(256UL, a._allocatedPrecision); Assert.AreEqual(256UL, a.Precision); } }
public void FloatToStringTruncated() { var n = string.Concat("123456783".Select(c => new string(c, 30))); using (var a = HugeFloat.Allocate(2000)) { a.SetTo(n); Assert.AreEqual("0." + n + "@" + n.Length, a.ToString(10)); Assert.AreEqual("0." + n.Substring(0, 256) + "@" + n.Length, a.ToString()); a.Value = -a; Assert.AreEqual("-0." + n.Substring(0, 256) + "@" + n.Length, a.ToString()); } }
public void FloatFromString() { using (var a = HugeFloat.Allocate(256)) { var n = "98762934876529834765234123984761"; a.SetTo(n); Assert.AreEqual("0." + n + "@" + n.Length, a.ToString()); n = "-98ABCDEF876529834765234123984761"; a.SetTo(n, 16); Assert.AreEqual("-0." + n.Substring(1) + "@" + (n.Length - 1).ToString("x"), a.ToString(16)); } }
public void FloatAllocate() { using (var a = HugeFloat.Allocate(193)) { Assert.AreEqual(192U + MpirSettings.BITS_PER_LIMB, a.Precision); Assert.AreEqual(192U + MpirSettings.BITS_PER_LIMB, a._allocatedPrecision); Assert.AreEqual("0", a.ToString()); var n = "234095827340957823409582587"; a.SetTo(n); Assert.AreEqual("0." + n + "@" + n.Length, a.ToString()); } }
public void FloatAllocatedPrecision() { using (var a = new HugeFloat(1)) using (var b = HugeFloat.Allocate(256)) { var bAllocated = b.AllocatedPrecision; var aAllocated = a.AllocatedPrecision; Assert.IsTrue(bAllocated > aAllocated); a.Precision = 64; b.Precision = 64; Assert.AreEqual(bAllocated, b.AllocatedPrecision); Assert.AreEqual(aAllocated, a.AllocatedPrecision); } }
public void ExpressionsCalculatedToSpecificPrecisionForEquals() { using (var a = new HugeFloat(1)) using (var b = new HugeFloat(3)) using (var c = HugeFloat.Allocate(256)) using (var d = HugeFloat.Allocate(256)) { c.SetTo("12345234589234059823475029384572323452034958723049823408955"); Assert.IsTrue(c.Equals(c + a / b, 128)); Assert.IsFalse(c.Equals(c + a / b, 256)); d.SetTo("12345234589234059823475029384572323452034958723049823408955.333333333333333333333333333333333"); Assert.IsTrue(d.Equals(c + a / b, 256)); Assert.IsTrue(d.Equals(c + a / b, 128)); } }
public void EqualsToPrimitiveCalculatedToDefaultPrecision() { using (var a = new HugeFloat(1)) using (var b = new HugeFloat(13)) using (var c = new HugeFloat("12345234589234059823475029384572323")) using (var d = HugeFloat.Allocate(256)) { ShiftLeftBy62(c); d.Value = c; var expr = a / b + c - c; Assert.IsTrue(expr.Equals(Platform.Si(0, 0))); Assert.IsTrue(expr.Equals(Platform.Ui(0, 0))); Assert.IsTrue(expr.Equals(0.0)); HugeFloat.DefaultPrecision = 256; Assert.IsFalse(expr.Equals(Platform.Si(0, 0))); Assert.IsFalse(expr.Equals(Platform.Ui(0, 0))); Assert.IsFalse(expr.Equals(0.0)); HugeFloat.DefaultPrecision = 128; } }
public void FloatInputOutputStrHexLower() { using (var a = new HugeFloat("10123456789ABCDEF012345.6789ABCDE", 16)) using (var b = HugeFloat.Allocate(12800)) using (var ms = new MemoryStream()) { a.Reallocate(12800); a.Value = a ^ 100; using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true)) a.Write(writer, 16, 0, true, false); ms.Position = 0; using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true)) b.Read(reader, 16, false); Assert.AreEqual(a, b); Assert.AreEqual(ms.Length, ms.Position); Assert.AreEqual((char)0xFEFF + a.ToString(16, true), Encoding.UTF8.GetString(ms.ToArray())); } }
public void SignCalculatedToDefaultPrecision() { using (var a = new HugeFloat(1)) using (var b = new HugeFloat(13)) using (var c = new HugeFloat("12345234589234059823475029384572323")) using (var d = HugeFloat.Allocate(256)) { ShiftLeftBy62(c); var expr = (a / b + c) - c; d.Value = expr; Assert.AreEqual(0, expr.Sign()); Assert.AreEqual(1, d.Sign()); HugeFloat.DefaultPrecision = 256; Assert.AreEqual(1, expr.Sign()); Assert.AreEqual(1, d.Sign()); d.Precision = 128; Assert.AreEqual(1, d.Sign()); d.Value = expr; Assert.AreEqual(0, d.Sign()); HugeFloat.DefaultPrecision = 128; } }
public void FloatEqualsHugeFloatApproximately() { using (var a = HugeFloat.Allocate(128)) using (var b = HugeFloat.Allocate(128)) { a.SetTo("ABCDEF12948576AB49587.ACD34EFB345", 16); b.SetTo("ABCDEF12948576AB49587.ACD34EFB245", 16); Assert.IsTrue(a.Equals(b, 119)); Assert.IsFalse(a.Equals(b, 120)); Assert.IsTrue(a.Equals(b - 1, 83)); Assert.IsFalse(a.Equals(b - 1, 84)); Assert.IsTrue((a + 512).Equals(b, 74)); Assert.IsFalse((a + 512).Equals(b, 75)); //same mantissa, different exponent should always return false Assert.IsFalse(a.Equals(a >> 1, 119)); Assert.IsFalse(a.Equals(a << 1, 119)); Assert.IsFalse(a.Equals(a * 2, 119)); Assert.IsFalse(a.Equals(a / 2, 119)); } }
public void FloatSwap() { using (var a = HugeFloat.Allocate(192)) using (var b = HugeFloat.Allocate(128)) { a.SetTo(123.5); b.SetTo(432.25); var aValue = a._value(); var bValue = b._value(); var aPrec = a._allocatedPrecision; var bPrec = b._allocatedPrecision; a.Swap(b); Assert.AreNotEqual(aValue, bValue); Assert.AreEqual(aValue, a._value()); Assert.AreEqual(bValue, b._value()); Assert.IsTrue(432.25 == a); Assert.IsTrue(123.5 == b); Assert.AreNotEqual(aPrec, bPrec); Assert.AreEqual(bPrec, a._allocatedPrecision); Assert.AreEqual(aPrec, b._allocatedPrecision); } }