|
| 1 | +// Copyright (c) 2023-2026 ktsu-dev contributors |
| 2 | + |
| 3 | +namespace ktsu.PreciseNumber.Test; |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Globalization; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Covers the functions that add or subtract one, or a reciprocal, against an intermediate whose |
| 11 | +/// exponent lies far from zero: <c>ExpM1</c>, <c>LogP1</c>, <c>Sinh</c>, <c>Cosh</c>, <c>Asinh</c> |
| 12 | +/// and <c>Acosh</c>. |
| 13 | +/// </summary> |
| 14 | +/// <remarks> |
| 15 | +/// Exact addition aligns both operands to the smaller exponent, so <c>e^-1e7 - 1</c> used to build a |
| 16 | +/// four-million-digit significand for an answer that rounds to <c>-1</c>, and <c>ExpM1(-1e10)</c> |
| 17 | +/// overflowed the exponent of <c>e^-1e10</c> before it got that far. The pinned digits below were |
| 18 | +/// produced by the exact path before it was bounded, so they show the bounded path rounds the same. |
| 19 | +/// </remarks> |
| 20 | +[TestClass] |
| 21 | +public class PreciseNumberLargeArgumentTests |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Longer than any of these calls takes once bounded, and far shorter than any took before. |
| 25 | + /// </summary> |
| 26 | + private static readonly TimeSpan Budget = TimeSpan.FromSeconds(5); |
| 27 | + |
| 28 | + private static PreciseNumber Parse(string text) => |
| 29 | + PreciseNumber.Parse(text, CultureInfo.InvariantCulture); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Runs a call on the thread pool and fails as soon as it overruns <see cref="Budget"/>, rather |
| 33 | + /// than holding the test run until a call that takes minutes finishes. |
| 34 | + /// </summary> |
| 35 | + private static PreciseNumber WithinBudget(Func<PreciseNumber> call, string description) |
| 36 | + { |
| 37 | + Task<PreciseNumber> task = Task.Run(call); |
| 38 | + Assert.IsTrue(task.Wait(Budget), $"{description} took longer than {Budget.TotalSeconds} seconds"); |
| 39 | + return task.Result; |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void TestExpM1OfAVeryNegativeArgumentIsMinusOne() |
| 44 | + { |
| 45 | + Assert.AreEqual(PreciseNumber.NegativeOne, PreciseNumber.ExpM1(Parse("-1E10")), "ExpM1(-1e10) is not -1"); |
| 46 | + Assert.AreEqual(PreciseNumber.NegativeOne, PreciseNumber.Exp2M1(Parse("-1E10")), "Exp2M1(-1e10) is not -1"); |
| 47 | + Assert.AreEqual(PreciseNumber.NegativeOne, PreciseNumber.Exp10M1(Parse("-1E10")), "Exp10M1(-1e10) is not -1"); |
| 48 | + Assert.AreEqual(PreciseNumber.NegativeOne, PreciseNumber.ExpM1(Parse("-1E10"), 200), "ExpM1(-1e10) at 200 digits is not -1"); |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void TestExpM1OfALargeArgumentFinishesQuickly() |
| 53 | + { |
| 54 | + Assert.AreEqual(PreciseNumber.NegativeOne, WithinBudget(() => PreciseNumber.ExpM1(Parse("-1E7")), "ExpM1(-1e7)"), "ExpM1(-1e7) is not -1"); |
| 55 | + Assert.AreEqual(PreciseNumber.NegativeOne, WithinBudget(() => PreciseNumber.Exp10M1(Parse("-1E7")), "Exp10M1(-1e7)"), "Exp10M1(-1e7) is not -1"); |
| 56 | + |
| 57 | + PreciseNumber expected = PreciseNumber.Exp(Parse("1E6")); |
| 58 | + PreciseNumber actual = WithinBudget(() => PreciseNumber.ExpM1(Parse("1E6")), "ExpM1(1e6)"); |
| 59 | + Assert.AreEqual(expected, actual, "ExpM1(1e6) differs from Exp(1e6), though the one is far below its last digit"); |
| 60 | + } |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public void TestLogP1OfAHugeArgumentFinishesQuickly() |
| 64 | + { |
| 65 | + PreciseNumber x = Parse("1E1000000"); |
| 66 | + Assert.AreEqual(PreciseNumber.Log(x), WithinBudget(() => PreciseNumber.LogP1(x), "LogP1(1e1000000)"), "LogP1(1e1000000) differs from Log(1e1000000)"); |
| 67 | + } |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + public void TestSinhAndCoshOfALargeArgumentFinishQuickly() |
| 71 | + { |
| 72 | + PreciseNumber sinh = WithinBudget(() => PreciseNumber.Sinh(Parse("1E7")), "Sinh(1e7)"); |
| 73 | + PreciseNumber cosh = WithinBudget(() => PreciseNumber.Cosh(Parse("-1E7")), "Cosh(-1e7)"); |
| 74 | + |
| 75 | + // At this size e^-x is millions of digits below e^x, so both are e^x / 2 to every digit kept. |
| 76 | + Assert.AreEqual(sinh, cosh, "Sinh(1e7) and Cosh(-1e7) disagree"); |
| 77 | + Assert.AreEqual(-sinh, WithinBudget(() => PreciseNumber.Sinh(Parse("-1E7")), "Sinh(-1e7)"), "Sinh is not odd at 1e7"); |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void TestAsinhAndAcoshOfAHugeArgumentFinishQuickly() |
| 82 | + { |
| 83 | + PreciseNumber x = Parse("1E1000000"); |
| 84 | + |
| 85 | + // Both are ln(2x) once 1/x is far below the last digit kept. |
| 86 | + PreciseNumber expected = PreciseNumber.Log(Parse("2E1000000")); |
| 87 | + Assert.AreEqual(expected, WithinBudget(() => PreciseNumber.Asinh(x), "Asinh(1e1000000)"), "Asinh(1e1000000) is not ln(2e1000000)"); |
| 88 | + Assert.AreEqual(-expected, WithinBudget(() => PreciseNumber.Asinh(-x), "Asinh(-1e1000000)"), "Asinh(-1e1000000) is not -ln(2e1000000)"); |
| 89 | + Assert.AreEqual(expected, WithinBudget(() => PreciseNumber.Acosh(x), "Acosh(1e1000000)"), "Acosh(1e1000000) is not ln(2e1000000)"); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void TestBoundedSumsKeepTheDigitsTheExactSumsGave() |
| 94 | + { |
| 95 | + Assert.AreEqual(Parse("-0.99999999999999999999999999999999999999999996279924"), PreciseNumber.ExpM1(Parse("-100"), 50), "ExpM1(-100) changed"); |
| 96 | + Assert.AreEqual(Parse("1.9424263952412559365842088360176992193662086219516E130"), PreciseNumber.ExpM1(Parse("300"), 50), "ExpM1(300) changed"); |
| 97 | + Assert.AreEqual(Parse("3.1622776601683793319988935444327185337195551393252E100"), PreciseNumber.Exp10M1(Parse("100.5"), 50), "Exp10M1(100.5) changed"); |
| 98 | + Assert.AreEqual(Parse("692.08386071786388396574754062165634523662942268717"), PreciseNumber.LogP1(Parse("3.7E300"), 50), "LogP1(3.7e300) changed"); |
| 99 | + Assert.AreEqual(Parse("9.712131976206279682921044180088496096831043109758E129"), PreciseNumber.Sinh(Parse("300"), 50), "Sinh(300) changed"); |
| 100 | + Assert.AreEqual(Parse("1.60125985730181641832516084312451811087207753341E130"), PreciseNumber.Cosh(Parse("-300.5"), 50), "Cosh(-300.5) changed"); |
| 101 | + Assert.AreEqual(Parse("-692.3849658106478055799981957385354499198560479429"), PreciseNumber.Asinh(Parse("-2.5E300"), 50), "Asinh(-2.5e300) changed"); |
| 102 | + Assert.AreEqual(Parse("693.44967654764023392316347631621299731774945936819"), PreciseNumber.Acosh(Parse("7.25E300"), 50), "Acosh(7.25e300) changed"); |
| 103 | + Assert.AreEqual(Parse("92.796550900321772670136890308832744872119559679511"), PreciseNumber.Acosh(Parse("1E40"), 50), "Acosh(1e40) changed"); |
| 104 | + } |
| 105 | +} |
0 commit comments