public void BitIncrementTest() { MultiPrecision <Pow2.N8>[] vs = { MultiPrecision <Pow2.N8> .NegativeInfinity, MultiPrecision <Pow2.N8> .MinValue, "-1e100", "-1e-100", -MultiPrecision <Pow2.N8> .Epsilon, MultiPrecision <Pow2.N8> .MinusZero, MultiPrecision <Pow2.N8> .Zero, MultiPrecision <Pow2.N8> .Epsilon, "1e-100", "1e100", MultiPrecision <Pow2.N8> .MaxValue, MultiPrecision <Pow2.N8> .PositiveInfinity }; foreach (var v in vs) { var vinc = MultiPrecision <Pow2.N8> .BitIncrement(v); Console.WriteLine(v); Console.WriteLine(vinc); Console.WriteLine(v.ToHexcode()); Console.WriteLine(vinc.ToHexcode()); Console.Write("\n"); } }
public static IEnumerable <MultiPrecision <N> > EnumerateNeighbor <N>(MultiPrecision <N> v, int n = 10) where N : struct, IConstant { List <MultiPrecision <N> > vs = new List <MultiPrecision <N> >(); MultiPrecision <N> u = v; for (int i = 0; i < n; i++) { u = MultiPrecision <N> .BitDecrement(u); vs.Add(u); } vs.Reverse(); vs.Add(v); u = v; for (int i = 0; i < n; i++) { u = MultiPrecision <N> .BitIncrement(u); vs.Add(u); } return(vs); }
public void AcosUnnormalValueTest() { MultiPrecision <Pow2.N8>[] vs = new MultiPrecision <Pow2.N8>[] { MultiPrecision <Pow2.N8> .NaN, MultiPrecision <Pow2.N8> .BitDecrement(-1), MultiPrecision <Pow2.N8> .BitIncrement(1), MultiPrecision <Pow2.N8> .PositiveInfinity, MultiPrecision <Pow2.N8> .NegativeInfinity, }; foreach (MultiPrecision <Pow2.N8> v in vs) { MultiPrecision <Pow2.N8> y = MultiPrecision <Pow2.N8> .Acos(v); Assert.IsTrue(y.IsNaN); } }
public void NearlyEqualBitsTest() { const int g = 8, n = 5; MultiPrecision <Pow2.N8>[][] vss = new MultiPrecision <Pow2.N8>[][] { new MultiPrecision <Pow2.N8>[] { -MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(4)), -MultiPrecision <Pow2.N8> .BitDecrement(4), -4, -MultiPrecision <Pow2.N8> .BitIncrement(4), -MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(4)), }, new MultiPrecision <Pow2.N8>[] { MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(4)), MultiPrecision <Pow2.N8> .BitDecrement(4), 4, MultiPrecision <Pow2.N8> .BitIncrement(4), MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(4)), }, new MultiPrecision <Pow2.N8>[] { -MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(3)), -MultiPrecision <Pow2.N8> .BitDecrement(3), -3, -MultiPrecision <Pow2.N8> .BitIncrement(3), -MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(3)), }, new MultiPrecision <Pow2.N8>[] { MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(3)), MultiPrecision <Pow2.N8> .BitDecrement(3), 3, MultiPrecision <Pow2.N8> .BitIncrement(3), MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(3)), }, new MultiPrecision <Pow2.N8>[] { -MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(2)), -MultiPrecision <Pow2.N8> .BitDecrement(2), -2, -MultiPrecision <Pow2.N8> .BitIncrement(2), -MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(2)), }, new MultiPrecision <Pow2.N8>[] { MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(2)), MultiPrecision <Pow2.N8> .BitDecrement(2), 2, MultiPrecision <Pow2.N8> .BitIncrement(2), MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(2)), }, new MultiPrecision <Pow2.N8>[] { -MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .Sqrt2)), -MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .Sqrt2), -MultiPrecision <Pow2.N8> .Sqrt2, -MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .Sqrt2), -MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .Sqrt2)), }, new MultiPrecision <Pow2.N8>[] { MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .Sqrt2)), MultiPrecision <Pow2.N8> .BitDecrement(MultiPrecision <Pow2.N8> .Sqrt2), MultiPrecision <Pow2.N8> .Sqrt2, MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .Sqrt2), MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .BitIncrement(MultiPrecision <Pow2.N8> .Sqrt2)), }, }; for (int i = 0; i < g; i++) { for (int j = 0; j < n; j++) { MultiPrecision <Pow2.N8> v = vss[i][j]; for (int k = 0; k < g; k++) { for (int m = 0; m < n; m++) { MultiPrecision <Pow2.N8> u = vss[k][m]; if (i == k) { Console.WriteLine(v.ToHexcode()); Console.WriteLine(u.ToHexcode()); int dist = Math.Abs(j - m); Console.WriteLine($"dist {dist}"); if (MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 0)) { Console.WriteLine("match"); Assert.IsTrue(dist == 0); } else if (MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 1)) { Console.WriteLine("nearly 1bits"); Assert.IsTrue(dist == 1); } else if (MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 2)) { Console.WriteLine("nearly 2bits"); Assert.IsTrue(dist >= 2 && dist < 4); } else if (MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 3)) { Console.WriteLine("nearly 3bits"); Assert.IsTrue(dist >= 3 && dist < 8); } else { Console.WriteLine("nearly more 4bits"); } Console.WriteLine(string.Empty); } else { Assert.IsFalse(MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 0), "bits 0"); Assert.IsFalse(MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 1), "bits 1"); Assert.IsFalse(MultiPrecision <Pow2.N8> .NearlyEqualBits(v, u, 2), "bits 2"); } } } } } }
public void EllipticENearOneTest() { const int exponent = -32; { MultiPrecision <Pow2.N4>[] ks = { MultiPrecision <Pow2.N4> .BitDecrement(1), MultiPrecision <Pow2.N4> .BitDecrement(1 - MultiPrecision <Pow2.N4> .Ldexp(1,exponent)), 1 - MultiPrecision <Pow2.N4> .Ldexp(1, exponent), MultiPrecision <Pow2.N4> .BitIncrement(1 - MultiPrecision <Pow2.N4> .Ldexp(1,exponent)), }; foreach (var k in ks) { MultiPrecision <Pow2.N4> y4 = MultiPrecision <Pow2.N4> .EllipticE(k); MultiPrecision <Pow2.N8> y8 = MultiPrecision <Pow2.N8> .EllipticE(k.Convert <Pow2.N8>()); Console.WriteLine(k.ToHexcode()); Console.WriteLine(y4); Console.WriteLine(y4.ToHexcode()); Console.WriteLine(y8.ToHexcode()); Assert.IsTrue(MultiPrecision <Pow2.N4> .NearlyEqualBits(y4, y8.Convert <Pow2.N4>(), 32)); } } { MultiPrecision <Pow2.N8>[] ks = { MultiPrecision <Pow2.N8> .BitDecrement(1), MultiPrecision <Pow2.N8> .BitDecrement(1 - MultiPrecision <Pow2.N8> .Ldexp(1,exponent)), 1 - MultiPrecision <Pow2.N8> .Ldexp(1, exponent), MultiPrecision <Pow2.N8> .BitIncrement(1 - MultiPrecision <Pow2.N8> .Ldexp(1,exponent)), }; foreach (var k in ks) { MultiPrecision <Pow2.N8> y8 = MultiPrecision <Pow2.N8> .EllipticE(k); MultiPrecision <Pow2.N16> y16 = MultiPrecision <Pow2.N16> .EllipticE(k.Convert <Pow2.N16>()); Console.WriteLine(k.ToHexcode()); Console.WriteLine(y8); Console.WriteLine(y8.ToHexcode()); Console.WriteLine(y16.ToHexcode()); Assert.IsTrue(MultiPrecision <Pow2.N8> .NearlyEqualBits(y8, y16.Convert <Pow2.N8>(), 64)); } } { MultiPrecision <Pow2.N16>[] ks = { MultiPrecision <Pow2.N16> .BitDecrement(1), MultiPrecision <Pow2.N16> .BitDecrement(1 - MultiPrecision <Pow2.N16> .Ldexp(1,exponent)), 1 - MultiPrecision <Pow2.N16> .Ldexp(1, exponent), MultiPrecision <Pow2.N16> .BitIncrement(1 - MultiPrecision <Pow2.N16> .Ldexp(1,exponent)), }; foreach (var k in ks) { MultiPrecision <Pow2.N16> y16 = MultiPrecision <Pow2.N16> .EllipticE(k); MultiPrecision <Pow2.N32> y32 = MultiPrecision <Pow2.N32> .EllipticE(k.Convert <Pow2.N32>()); Console.WriteLine(k.ToHexcode()); Console.WriteLine(y16); Console.WriteLine(y16.ToHexcode()); Console.WriteLine(y32.ToHexcode()); Assert.IsTrue(MultiPrecision <Pow2.N16> .NearlyEqualBits(y16, y32.Convert <Pow2.N16>(), 128)); } } { MultiPrecision <Pow2.N32>[] ks = { MultiPrecision <Pow2.N32> .BitDecrement(1), MultiPrecision <Pow2.N32> .BitDecrement(1 - MultiPrecision <Pow2.N32> .Ldexp(1,exponent)), 1 - MultiPrecision <Pow2.N32> .Ldexp(1, exponent), MultiPrecision <Pow2.N32> .BitIncrement(1 - MultiPrecision <Pow2.N32> .Ldexp(1,exponent)), }; foreach (var k in ks) { MultiPrecision <Pow2.N32> y32 = MultiPrecision <Pow2.N32> .EllipticE(k); MultiPrecision <Pow2.N64> y64 = MultiPrecision <Pow2.N64> .EllipticE(k.Convert <Pow2.N64>()); Console.WriteLine(k.ToHexcode()); Console.WriteLine(y32); Console.WriteLine(y32.ToHexcode()); Console.WriteLine(y64.ToHexcode()); Assert.IsTrue(MultiPrecision <Pow2.N32> .NearlyEqualBits(y32, y64.Convert <Pow2.N32>(), 256)); } } }