using Shouldly; using Xunit; namespace Just.PreciseMath.Tests; public class ArithmeticRangeTests { [Fact] public void AdditionRangeMatchesTheOriginalExponentGuard() { // Independent BCL predicate: include both signed zeros, subnormals, and // the first/last significands of every finite exponent field. foreach (double value in FiniteExponentSamples()) { bool expected = Math.ILogB(value) <= 1020; PreciseMathHelper.IsAdditionWithinFastRange(value).ShouldBe(expected, $"{value:R}"); } } [Fact] public void DivisionRangeMatchesTheOriginalExponentGuard() { // Public operators handle zero before this guard; ILogB(0) is the // int.MinValue sentinel, whose absolute value is not representable. foreach (double value in FiniteExponentSamples()) { if (value != 0.0) { bool expected = Math.Abs(Math.ILogB(value)) <= 450; PreciseMathHelper.IsDivisionWithinFastRange(value).ShouldBe(expected, $"{value:R}"); } } } [Fact] public void MultiplicationRangeMatchesEveryNormalExponentPair() { // Exhaust all normal exponent combinations. The original guard depends // only on these exponents, never on the sign or fractional significand. double[] values = new double[2046]; for (int i = 0; i < values.Length; ++i) { values[i] = Math.ScaleB(1.0, i - 1022); } foreach (double left in values) { foreach (double right in values) { AssertMultiplicationRange(left, right); } } } [Fact] public void MultiplicationRangePreservesSubnormalAndSignedOperandPaths() { // Include all subnormal binades and both significand edges, paired // with large normals that can bring the exponent sum into range. double[] partners = [double.Epsilon, Math.BitDecrement(Math.ScaleB(1.0, -1022)), Math.ScaleB(1.0, -1022), Math.ScaleB(1.0, -901), Math.ScaleB(1.0, -900), Math.ScaleB(1.0, -1), 1.0, 2.0, Math.ScaleB(1.0, 900), Math.ScaleB(1.0, 901), Math.ScaleB(1.0, 1023), double.MaxValue]; foreach (double value in FiniteExponentSamples()) { if (value == 0.0) { continue; } foreach (double partner in partners) { AssertMultiplicationRange(value, partner); AssertMultiplicationRange(partner, value); AssertMultiplicationRange(value, -partner); AssertMultiplicationRange(-partner, value); } } } [Theory] [InlineData("+")] [InlineData("-")] [InlineData("*")] public void FiniteKernelsPreservePreviousComponentBits(string operation) { // Differential characterization, not an independent accuracy oracle. // Freeze the previous four-TwoSum/FMA expressions and public normalization; // independent exact/rational accuracy cases remain in the arithmetic suites. List values = [new(0.0), new(-0.0)]; int[] exponents = [-1074, -1022, -901, -900, -899, -451, -450, -1, 0, 1, 450, 451, 899, 900, 901, 1020, 1021, 1023]; foreach (int exponent in exponents) { foreach (double significand in new[] { 1.0, Math.BitIncrement(1.0), 1.5, Math.BitDecrement(2.0) }) { foreach (double sign in new[] { -1.0, 1.0 }) { double high = sign * Math.ScaleB(significand, exponent); foreach (double low in new[] { 0.0, Math.ScaleB(high, -53), -Math.ScaleB(high, -53), double.Epsilon, -double.Epsilon }) { DoubleDouble value = DoubleDouble.FromComponents(high, low); if (DoubleDouble.IsFinite(value)) { values.Add(value); } } } } } foreach (DoubleDouble left in values) { foreach (DoubleDouble right in values) { DoubleDouble expected; DoubleDouble actual; if (operation == "*") { if (left.High == 0.0 || right.High == 0.0) { continue; } int exponent = Math.ILogB(left.High) + Math.ILogB(right.High); if (exponent < -900 || exponent > 900) { continue; } (double product, double error) = PreciseMathHelper.TwoMultiply(left.High, right.High); error = Math.FusedMultiplyAdd(left.High, right.Low, error); error = Math.FusedMultiplyAdd(left.Low, right.High, error); error = Math.FusedMultiplyAdd(left.Low, right.Low, error); Math.Abs(error).ShouldBeLessThan(Math.Abs(product)); expected = DoubleDouble.FromComponents(product, error); actual = left * right; } else { if (Math.ILogB(left.High) > 1020 || Math.ILogB(right.High) > 1020) { continue; } DoubleDouble operand = operation == "-" ? -right : right; if (left.High == 0.0 && operand.High == 0.0) { expected = new DoubleDouble(left.High + operand.High); } else { (double high, double highError) = PreciseMathHelper.TwoAdd(left.High, operand.High); (double low, double lowError) = PreciseMathHelper.TwoAdd(left.Low, operand.Low); (double middle, double middleError) = PreciseMathHelper.TwoAdd(highError, low); (double sum, double sumError) = PreciseMathHelper.TwoAdd(high, middle); double correction = sumError + (middleError + lowError); double.IsFinite(sum + correction).ShouldBeTrue(); expected = DoubleDouble.FromComponents(sum, correction); } actual = operation == "-" ? left - right : left + right; } BitConverter.DoubleToInt64Bits(actual.High).ShouldBe(BitConverter.DoubleToInt64Bits(expected.High)); BitConverter.DoubleToInt64Bits(actual.Low).ShouldBe(BitConverter.DoubleToInt64Bits(expected.Low)); } } } private static void AssertMultiplicationRange(double left, double right) { int exponent = Math.ILogB(left) + Math.ILogB(right); bool expected = exponent >= -900 && exponent <= 900; bool actual = PreciseMathHelper.IsMultiplicationWithinFastRange(left, right); // Only format diagnostics on failure in this exhaustive matrix. if (actual != expected) { actual.ShouldBe(expected, $"({left:R}, {right:R})"); } } private static IEnumerable FiniteExponentSamples() { ulong[] fractions = [0, 1, 0x0008_0000_0000_0000, 0x000f_ffff_ffff_ffff]; foreach (ulong sign in new[] { 0UL, 0x8000_0000_0000_0000UL }) { for (int bit = 0; bit < 52; ++bit) { yield return BitConverter.UInt64BitsToDouble(sign | (1UL << bit)); yield return BitConverter.UInt64BitsToDouble(sign | ((1UL << (bit + 1)) - 1)); } for (ulong exponent = 0; exponent < 0x7ff; ++exponent) { foreach (ulong fraction in fractions) { yield return BitConverter.UInt64BitsToDouble(sign | (exponent << 52) | fraction); } } } } }