From e1f55238dcc3694340e98137f09fdb20c5f45251 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 3 Dec 2026 20:49:59 +0000 Subject: [PATCH 1/2] Keep a complex number's exact parts exact through its arithmetic A sum, difference, product or quotient of two complex numbers was made through the decimals of their parts and rounded back onto a rational only within half the working digits, which a part past 10^50 is not: (10^50 + 1/3 + i) - 10^50 was 0.333...3 + i, (10^60 + i)(10^60 - i) was 10^120, and a quotient near a small rational was rounded onto it. Where every part of both numbers is a rational the arithmetic is now exact, as a real number's is, and integer powers with it. 10^84 i was a decimal past the digits a cosine needs, so exp(10^84 i), cos(10^84 + i) and Ci(10^100 + i) were NaN; they have values now, checked against mpmath at 700 digits, while a decimal argument there stays NaN. The difference of exponentials behind sinh wrote its exact exponent out as a decimal too, and sinh(10^100 i) was NaN; it takes the exponent as it is. (1 + i)^(10^6) is the integer 2^500000 where it was +oo, and a power of a base read as exact parts, (0.3 + 0.4i)^(-9), is the exact rational the decimal it was equals; the tests that pinned the decimals say so. Fixes #2685 Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 39 +++++++++++ .../Entity.Continuous.Complex.Definition.cs | 9 +++ .../Entity/Continuous/Number/Operators.cs | 37 +++++++++- .../Common/NumericsRegressionTest.cs | 69 +++++++++++++++++-- .../Core/AComplexOverflowIsNotNaNTest.cs | 18 +++-- .../UnitTests/Core/AWholePowerOfAUnitTest.cs | 9 ++- 6 files changed, 164 insertions(+), 17 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 26f3beafd..49059b68e 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -318,6 +318,45 @@ integer. Exact large integers are unchanged | `"integral(exp(x), x, 0, 300)".Simplify()` | `1942426395…4199999…9` | `e ^ 300 - 1` | | `"2^340 * sqrt(2)".Evaled` | an `Integer` | `3.167477390…E+102`, a `Real` | +### A complex number with exact parts keeps them exact + +**Silent: wrong numbers become right ones.** A sum, difference, product or quotient of two complex numbers, and so a +whole power of one, was made through the decimals of their parts and rounded back onto a rational only within half the +working digits, which a part past `10^50` is not: `(10^50 + 1/3 + i) - 10^50` was `0.333...3 + i`, and +`(10^60 + i) (10^60 - i)` was `10^120` where it is `10^120 + 1`. A quotient near a small rational was rounded onto it: +`(10^50 - 1/3 - i/7) / (10^50 + 1/7 - 10^50 i)` was `1/2 + 1/2 i`. Where every part of both is a rational, the +arithmetic is now exact, as a real one's is. And an exact `10^84 i` was a decimal, past the digits a cosine needs, so +its exponential was `NaN`; it is exact now, and the functions of an exact number past the working digits have values. +A decimal there, `10^84 sqrt(2) i`, has none, as before. Measured on master (34d481139) and on this branch; the true +values are mpmath's at 700 to 750 digits ([#2685](https://github.com/asc-community/AngouriMath/issues/2685)). + +| `.Evaled` | Was (master) | Now | +|---|---|---| +| `(10^50 + 1/3 + i) - 10^50` | `0.3333333333333333333333333333333333333333333333333 + i` | `1/3 + i` | +| `(10^60 + i) * (10^60 - i)` | `1.000…000E+120` | `10^120 + 1`, as an integer | +| `(10^60 + i) / (10^60 - i)` | `1 + 2E-60i` | `(10^120 - 1)/(10^120 + 1) + 2 10^60/(10^120 + 1) i`, as rationals | +| `(10^29 + i) / (10^29 - i)` | `1 + 1.9999…98E-29i` | `(10^58 - 1)/(10^58 + 1) + 2 10^29/(10^58 + 1) i`, as rationals | +| `(10^50 - 1/3 - i / 7) / (10^50 + 1/7 - 10^50 * i)` | `1/2 + 1/2 * i` | the exact quotient, `1/2 - 9.5…E-52 + (1/2 - 3.1…E-51) i` | +| `(10^50 + 1/3 + i) ^ 2` | `1.0000…0666…E+100 + 200000000000000000000000000000000000000000000000000.666…i` | `(10^50 + 1/3)^2 - 1 + 2 (10^50 + 1/3) i`, as rationals | +| `(0.3 + 0.4i)^(-9)` | `-241.716953088 - 451.350102016i` | `-472103424/1953125 - 881543168/1953125 * i`, the same number | +| `(1 + i)^(10^6)` | `+oo` | `2^500000`, as an integer, as `2^(10^6)` is one | +| `exp(10^84 * i)` | `NaN` | `0.7550644793… + 0.6556505411…i` | +| `cos(10^84 + i)` | `NaN` | `1.1651253760… - 0.7705212985…i` | +| `tan(10^150 + i)` | `NaN` | `0.1995074892… + 1.2276265478…i` | +| `sinh(10^100 * i)` | `NaN` | `-0.3723761236…i` | +| `Ci(10^100 + i)` | `NaN` | `-5.7460638528…E-101 - 1.0906829626…E-100i` | +| `Si(10^100 + i)` | `NaN` | `1.5707963267… - 4.3761686501…E-101i` | +| `Ci(10^200 + i)` | `NaN` | `1.4955097442…E-200 - 2.8955450281…E-201i` | + +A value whose parts were decimals now prints them as fractions, which past `10^50` have as many digits as the numbers +they are made from. Of 5300 sums, differences, products, quotients and powers of parts like `10^k ± 1/3`, `10^k i` and +`i/7` for `k` from 1 to 300, master had 664 exact, 310 of them rounded onto a wrong rational, and 4636 decimals, 262 of +them wrong past a relative `1e-90`; this branch has all 5300 exact and right. Of 1830 values of `cos`, `sin`, `tan`, +`exp`, `cosh`, `sinh`, `ln` and `sqrt` of `10^k + i`, `10^k i` and `10^k + i/3`, and of `Ci`, `Si` and `Ei` of +`10^k ± i`, for `k` from 0 to 300, master had 734 `NaN`; this branch has 557 of them right to a relative `1e-99` of each +part, and none that was right on master changed. The 177 left `NaN` are `cos`, `sin` and `tan` of +`10^k i` from `k = 10`, `NaN` on master too, where `cosh(10^k)` is past the exponents. + ### A comparison of exact numbers is exact, and one the decimals cannot decide stays written **Not silent: wrong `True` and `False` answers change.** A comparison of two evaluated numbers went through their diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs index df8b9e3b6..bf4b8810b 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs @@ -101,6 +101,15 @@ public static Complex Create(Real real, Real imaginary) => /// public static Complex Create(EDecimal real, EDecimal imaginary) => Create(real, imaginary, EDecimal.One, EDecimal.One, fromMachineNumbers: false); + /// + /// The number with exact parts and , which is the + /// rational where is 0. Exact parts are kept as they + /// are, at any size: writes them out to the working digits. + /// https://github.com/asc-community/AngouriMath/issues/2685 + /// + internal static Complex FromExactParts(ERational real, ERational imaginary) + => imaginary.IsZero ? Rational.Create(real) : new Complex(Rational.Create(real), Rational.Create(imaginary)); + /// /// A number an arithmetic operation produced, each part zero only as a residual of the cancellation that /// produced it, as reads one. diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs index b2cdd779c..b7de321bb 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs @@ -115,7 +115,9 @@ internal static T OpSum(T a, T b) where T : Number => (a, b) => Integer.Create(CtxAdd(a.EInteger, b.EInteger)), (a, b) => Rational.Create(CtxAdd(a.ERational, b.ERational)), (a, b) => Real.CreateAtScale(CtxAdd(a.EDecimal, b.EDecimal), CancelledScale(a.EDecimal, b.EDecimal)), - (a, b) => Complex.CreateAtScales(CtxAdd(a.RealPart.EDecimal, b.RealPart.EDecimal), + (a, b) => ExactParts(a, b) is { } p + ? Complex.FromExactParts(p.Ar.Add(p.Br), p.Ai.Add(p.Bi)) + : Complex.CreateAtScales(CtxAdd(a.RealPart.EDecimal, b.RealPart.EDecimal), CtxAdd(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal), CancelledScale(a.RealPart.EDecimal, b.RealPart.EDecimal), CancelledScale(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal)) @@ -126,7 +128,9 @@ internal static T OpSub(T a, T b) where T : Number => (a, b) => Integer.Create(CtxSubtract(a.EInteger, b.EInteger)), (a, b) => Rational.Create(CtxSubtract(a.ERational, b.ERational)), (a, b) => Real.CreateAtScale(CtxSubtract(a.EDecimal, b.EDecimal), CancelledScaleOfADifference(a.EDecimal, b.EDecimal)), - (a, b) => Complex.CreateAtScales(CtxSubtract(a.RealPart.EDecimal, b.RealPart.EDecimal), + (a, b) => ExactParts(a, b) is { } p + ? Complex.FromExactParts(p.Ar.Subtract(p.Br), p.Ai.Subtract(p.Bi)) + : Complex.CreateAtScales(CtxSubtract(a.RealPart.EDecimal, b.RealPart.EDecimal), CtxSubtract(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal), CancelledScaleOfADifference(a.RealPart.EDecimal, b.RealPart.EDecimal), CancelledScaleOfADifference(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal)) @@ -139,6 +143,8 @@ internal static T OpMul(T a, T b) where T : Number => (a, b) => Real.CreateAtScale(CtxMultiply(a.EDecimal, b.EDecimal), EDecimal.Zero), (a, b) => { + if (ExactParts(a, b) is { } p) + return Complex.FromExactParts(p.Ar.Multiply(p.Br).Subtract(p.Ai.Multiply(p.Bi)), p.Ar.Multiply(p.Bi).Add(p.Ai.Multiply(p.Br))); // A whole zero against a whole infinity is 0 * oo, which is NaN for a real // and is NaN here: the reading below, that a zero *part* times an infinite // part is zero, is for one part against another and read 0 * (i * oo) as @@ -158,6 +164,20 @@ static EDecimal ModifiedMultiply(EDecimal a, EDecimal b) => } ); + /// + /// The four parts of and where every one is a rational, and null + /// otherwise. Two numbers whose parts are exact have an exact sum, difference, product and quotient, and + /// they are made from the parts as a real one is: through the decimals, a part past ten to the half of the + /// working digits was no longer rounded back onto the rational it is, so that (10^50 + 1/3 + i) - 10^50 + /// was 0.333...3 + i, (10^60 + i) (10^60 - i) was 10^120 where it is 10^120 + 1, and + /// 10^84 i was a decimal, whose exponential has no value past the digits of its argument. + /// https://github.com/asc-community/AngouriMath/issues/2685 + /// + private static (ERational Ar, ERational Ai, ERational Br, ERational Bi)? ExactParts(Complex a, Complex b) + => a.RealPart is Rational { ERational: var ar } && a.ImaginaryPart is Rational { ERational: var ai } + && b.RealPart is Rational { ERational: var br } && b.ImaginaryPart is Rational { ERational: var bi } + ? (ar, ai, br, bi) : null; + /// /// Whether both parts are finite, read from the parts: is a cache made on /// an entity's first read of it, and asked of every power built, and of every exact value an evaluation @@ -192,6 +212,14 @@ internal static Complex OpDiv(T a, T b) where T : Number => (a, b) => Real.CreateAtScale(CtxDivide(a.EDecimal, b.EDecimal), EDecimal.Zero), (a, b) => { + if (ExactParts(a, b) is { } p) + { + var squaredModulus = p.Br.Multiply(p.Br).Add(p.Bi.Multiply(p.Bi)); + if (squaredModulus.IsZero) + return Real.NaN; + return Complex.FromExactParts(p.Ar.Multiply(p.Br).Add(p.Ai.Multiply(p.Bi)).Divide(squaredModulus), + p.Ai.Multiply(p.Br).Subtract(p.Ar.Multiply(p.Bi)).Divide(squaredModulus)); + } /* * (a + ib) / (c + id) = (a + ib) * (1 / (c + id)) * 1 / (c + id) = (c2 + d2) / (c + id) / (c2 + d2) = (c - id) / (c2 + d2) @@ -1338,7 +1366,10 @@ internal static Complex ExpDifference(Complex v, Complex d) using (MathS.Settings.DowncastingEnabled.Set(false)) using (MathS.Settings.DecimalPrecisionContext.Set(work)) { - power = Exp(Complex.Create(x, y)); + // An exact d as it is: written out as decimals, 2 * 10^100 i was past the working digits, whose + // exponential has no value, and sinh(10^100 i) was NaN where it is i sin(10^100). + // https://github.com/asc-community/AngouriMath/issues/2685 + power = Exp(d.IsExact ? d : Complex.Create(x, y)); lower = Exp(v); product = lower * Complex.CreateAtScales(power.RealPart.EDecimal.Subtract(EDecimal.One, work), power.ImaginaryPart.EDecimal, EDecimal.Zero, EDecimal.Zero); } diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index 3dd11b0ef..cfd3b0469 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -760,19 +760,74 @@ public void Issue2683_ACancellationOfARootOrOfAConstantKeepsItsDigits(string exp } } - // A complex number's part past 10^50 is a decimal, whose cosine and sine from 10^100 leave Ci's parts below their - // residual, as cos(10^100 + i) has no value: such a part is not known, and is not 0. Ci(10^100 + i) was 0 and - // Si(10^100 + i) had no imaginary part. https://github.com/asc-community/AngouriMath/issues/2683 + // A real part that is a decimal past the working digits, as 10^100 sqrt(2) is, leaves Ci's parts below the residual + // of its own cosine and sine, as cos(10^100 sqrt(2) + i) has no value: such a part is not known, and is not 0. + // Ci(10^100 + i) was 0 and Si(10^100 + i) had no imaginary part. An exact 10^100 + i is not such a decimal, and + // has values: see Issue2685_AnExactArgumentPastTheWorkingDigitsHasAValue. + // https://github.com/asc-community/AngouriMath/issues/2683 [Theory] - [InlineData("Ci(10^100 + i)")] - [InlineData("Si(10^100 + i)")] - [InlineData("Ci(10^110 + i)")] - [InlineData("Ci(10^200 + i)")] + [InlineData("Ci(10^100 * sqrt(2) + i)")] + [InlineData("Si(10^100 * sqrt(2) + i)")] + [InlineData("Ci(10^110 * sqrt(2) + i)")] + [InlineData("Ci(10^200 * sqrt(2) + i)")] + [InlineData("exp(10^84 * sqrt(2) * i)")] + [InlineData("cos(10^84 * sqrt(2) + i)")] public void Issue2683_ATrigonometricIntegralOfADecimalPastItsDigitsIsNaN(string expression) { Assert.Equal(MathS.NaN, expression.ToEntity().Evaled); } + // A complex number whose parts are exact keeps them exact through its arithmetic, as a real one does: through + // the decimals, a part past half the working digits was no longer rounded back onto the rational it is, so + // (10^50 + 1/3 + i) - 10^50 was 0.333... + i and (10^60 + i) (10^60 - i) was 10^120. Each part is a rational, + // equal to the one the real arithmetic gives, which is exact whatever the size. + // https://github.com/asc-community/AngouriMath/issues/2685 + [Theory] + [InlineData("(10^50 + 1/3 + i) - 10^50", "1/3", "1")] + [InlineData("(10^60 + i) * (10^60 - i)", "10^120 + 1", "0")] + [InlineData("(10^60 + i) / (10^60 - i)", "(10^120 - 1) / (10^120 + 1)", "2 * 10^60 / (10^120 + 1)")] + [InlineData("(10^50 + 1/3 + i) ^ 3", "(10^50 + 1/3)^3 - 3 * (10^50 + 1/3)", "3 * (10^50 + 1/3)^2 - 1")] + [InlineData("(1/3 - 10^200 * i) ^ (-2)", "(1/9 - 10^400) / (1/9 + 10^400)^2", "2/3 * 10^200 / (1/9 + 10^400)^2")] + [InlineData("(10^300 + 1/7 - 10^300 * i) / (2/7 + 10^300 * i)", "((10^300 + 1/7) * 2/7 - 10^600) / (4/49 + 10^600)", "(-10^300 * 2/7 - (10^300 + 1/7) * 10^300) / (4/49 + 10^600)")] + [InlineData("10^84 * i", "0", "10^84")] + public void Issue2685_AComplexNumberWithExactPartsKeepsThemExact(string expression, string real, string imaginary) + { + var value = Assert.IsAssignableFrom(expression.ToEntity().Evaled); + foreach (var (part, truth) in new[] { (value.RealPart, real), (value.ImaginaryPart, imaginary) }) + { + var expected = Assert.IsAssignableFrom(truth.ToEntity().Evaled); + var rational = Assert.IsAssignableFrom(part); + Assert.Equal(expected.ERational, rational.ERational); + } + } + + // And an exact argument past the working digits has a value where a decimal there has none: 10^84 i was a decimal, + // and exp(10^84 i), cos(10^84 + i), Ci(10^100 + i) and sinh(10^100 i) were NaN. Each part is held to a relative 1e-90 + // of mpmath's at 700 digits, and a part that is 0 to 0. https://github.com/asc-community/AngouriMath/issues/2685 + [Theory] + [InlineData("exp(10^84 * i)", "7.5506447931088255859574947892712086645644329417196052228044697373554307624044103367414712181515837341171812993e-1", "6.5565054112917942792797268989053279009385205615579723390778685052442854526542794062619408852133109162553257403e-1")] + [InlineData("cos(10^84 + i)", "1.1651253760614781607253275479286113734409474544136741875176773646825035808922447050665743521441465668487740973", "-7.7052129854821600439669876084162148455452808437342256857016321357711519444123209105109605870822493269949864637e-1")] + [InlineData("exp(10^300 * i)", "-1.6821444437424507285187566443555584453305088766805226227941988583778103907903891805772328326882360802562230044e-1", "-9.8575042516037699660904753142989546907771531256102699315923718449577797700556983677843100496286458264540164584e-1")] + [InlineData("sin(10^300 + i / 3)", "-1.0410232927132762153162201173574983506512120826759678561743090940066032285256467971198278891450669304999102191", "-5.7115626181364841815935582501586382580043654470499675371954935990189387143757463739495392944553647551224170289e-2")] + [InlineData("sinh(10^100 * i)", "0", "-3.7237612366127668826208669555316429571966788356743470236441538829671922404375644118873660041620302321867558548e-1")] + [InlineData("cosh(10^200 * i)", "-2.4638717555926673433214673054404426775422227835344393341116952790039111414120325477521202292600653453744313832e-1", "0")] + [InlineData("tan(10^150 + i)", "1.9950748928578086511408322926336960416648379811704488404184583355086377532174997193485325486513142691580831012e-1", "1.2276265478874887692963997807960521164660695638528500235584687333322108078048456300363565589479669985294719326")] + [InlineData("Ci(10^100 + i)", "-5.7460638528928255144145613851270435442664653608926669609099623123915534923794205046742155156534552906464037741e-101", "-1.0906829626429481962867842163262319258573022874930963915323804525785680561876596379483765926556150419702502835e-100")] + [InlineData("Si(10^100 + i)", "1.5707963267948966192313216916397514420985846996875529104874722961539082031431044993140174126710585341342845648", "-4.3761686501118418287003729711934961979787057034930842239446801766708931529840853440604829562746415100436427639e-101")] + [InlineData("Ci(10^110 + i)", "-1.3783805582460430368985190209033222070304247465072247569034230415033609002964576658530782933844376432362034183e-110", "-5.2828777913664348236885639321348465875881466276694553413933224058490493973445534482789508077176266950070531111e-111")] + [InlineData("Ci(10^200 + i)", "1.4955097442547313856432373394891025790954154925187286125773129147139581995876477709551215664448480983012969192e-200", "-2.8955450281577513097454400577482058352994123196098639957203083637675665536741499457980141117109904015558752934e-201")] + [InlineData("Si(10^300 - i)", "1.5707963267948966192313216916397514420985846996875529104874722961539082031431044993140174126710585339910740433", "1.158455076283359822585387641834343772364149308669968811684943706876409722851503432596075533287702567638934922e-300")] + public void Issue2685_AnExactArgumentPastTheWorkingDigitsHasAValue(string expression, string trueReal, string trueImaginary) + { + var value = Assert.IsAssignableFrom(expression.ToEntity().Evaled); + foreach (var (part, truth) in new[] { (value.RealPart.EDecimal, trueReal), (value.ImaginaryPart.EDecimal, trueImaginary) }) + { + var expected = EDecimal.FromString(truth); + Assert.True(part.IsFinite && part.Subtract(expected).Abs().CompareTo(expected.Abs().Multiply(EDecimal.FromString("1E-90"))) <= 0, + $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i"); + } + } + // arsinh(a) is ln(a + sqrt(a^2 + 1)), and for a small a the square root is within a^2/2 of 1: the downcasting // took it for 1 from a below 10^-25, at the working digits and at those the logarithm near 1 is evaluated again // with, and the logarithm was ln(1 + a), short of a^2/2 -- 25 digits of arsinh(10^-25), 60 of arsinh(10^-60). diff --git a/Sources/Tests/UnitTests/Core/AComplexOverflowIsNotNaNTest.cs b/Sources/Tests/UnitTests/Core/AComplexOverflowIsNotNaNTest.cs index 471ce5aa0..1aab5ca12 100644 --- a/Sources/Tests/UnitTests/Core/AComplexOverflowIsNotNaNTest.cs +++ b/Sources/Tests/UnitTests/Core/AComplexOverflowIsNotNaNTest.cs @@ -38,9 +38,15 @@ public void AProductPastTheRangeHasItsParts(string input) Assert.True(complex.ImaginaryPart.EDecimal.IsPositiveInfinity(), $"{input} has the imaginary part {complex.ImaginaryPart}"); } + // An exact base has an exact power, as a real one does, whose parts are past the range only when written out as + // decimals: (1 + i)^(10^6) is the integer 2^500000, as 2^(10^6) is an integer. It was +oo while the parts of a + // complex product were decimals. https://github.com/asc-community/AngouriMath/issues/2685 [Fact] - public void APowerOfOnePlusIPastTheRangeIsPositiveInfinity() - => Assert.Equal(Real.PositiveInfinity, Within(() => "(1 + i)^(10^6)".ToEntity().Evaled, "(1 + i)^(10^6)")); + public void APowerOfOnePlusIPastTheRangeIsExact() + { + var value = Assert.IsAssignableFrom(Within(() => "(1 + i)^(10^6)".ToEntity().Evaled, "(1 + i)^(10^6)")); + Assert.Equal(EInteger.One.ShiftLeft(500000), value.EInteger); + } [Theory] [InlineData(3, 4, 2000)] @@ -70,10 +76,14 @@ public void APowerWithinTheRangeIsFinite(string input) [InlineData("(3 + 4 * i)^2000 + x")] public void AnExactSimplificationLeavesItAsWritten(string input) { - Assert.True(Within(() => input.ToEntity().InnerSimplified, input) is not Number { IsFinite: false }, $"{input} was simplified to a number that is not finite"); - Assert.True(Within(() => input.ToEntity().Simplify(), input) is not Number { IsFinite: false }, $"{input} was simplified to a number that is not finite"); + Assert.True(IsNoInfinity(Within(() => input.ToEntity().InnerSimplified, input)), $"{input} was simplified to a number that is not finite"); + Assert.True(IsNoInfinity(Within(() => input.ToEntity().Simplify(), input)), $"{input} was simplified to a number that is not finite"); } + // Left as written, or the exact number it is: (1 + i)^(10^6) is the integer 2^500000, which IsFinite reads as a + // decimal past the range. https://github.com/asc-community/AngouriMath/issues/2685 + private static bool IsNoInfinity(Entity value) => value is not Number { IsFinite: false } || value is Rational; + /// The exact part rounded into the context: the same infinity past it, and the same sign within. private static void AssertRoundedInto(EInteger exact, EDecimal evaluated, string input) { diff --git a/Sources/Tests/UnitTests/Core/AWholePowerOfAUnitTest.cs b/Sources/Tests/UnitTests/Core/AWholePowerOfAUnitTest.cs index ae403016b..ad76f8b56 100644 --- a/Sources/Tests/UnitTests/Core/AWholePowerOfAUnitTest.cs +++ b/Sources/Tests/UnitTests/Core/AWholePowerOfAUnitTest.cs @@ -38,10 +38,13 @@ public void IsItsCycleOnASmallStack(string input, string expected) Assert.Equal(MathS.FromString(expected).EvalNumerical(), value); } - // The multiplications are the recursion's, in its order, so a decimal power keeps every digit it had. + // The multiplications are the recursion's, in its order, so a decimal power keeps every digit it had. A base whose + // parts are exact, as 1.1 + 0.2i is read, has an exact power, the same number as the decimal it was written as + // to every digit: -0.16276672053742730437722112 is -242541313972215808/1490116119384765625. + // https://github.com/asc-community/AngouriMath/issues/2685 [Theory] - [InlineData("(1.1 + 0.2i)^(-13)", "-0.16276672053742730437722112 - 0.16876781705731410143215616i")] - [InlineData("(0.3 + 0.4i)^(-9)", "-241.716953088 - 451.350102016i")] + [InlineData("(1.1 + 0.2i)^(-13)", "-242541313972215808/1490116119384765625 - 251483644630482944/1490116119384765625 * i")] + [InlineData("(0.3 + 0.4i)^(-9)", "-472103424/1953125 - 881543168/1953125 * i")] [InlineData("2.5^(-37)", "137438953472/72759576141834259033203125")] [InlineData("i^(-7)", "i")] public void IsWhatItWas(string input, string expected) From 7a04bea3b94488e7e8d41f09419b5bd9a122612f Mon Sep 17 00:00:00 2001 From: Rafael Date: Thu, 3 Dec 2026 22:05:57 +0000 Subject: [PATCH 2/2] Record SolveHard's lower allocation in the performance baseline The kernel performance gate on this PR's run (47205062545) reported SolveHard at 6,209,376 B/op, 32.4% below the 9,183,808 recorded at 6a300d9e, and every other gated row within 0.9%. Its equation's answers, and SolveMediumHard's, are character for character those of master. The file is that run's own measurement, copied over the baseline. Co-Authored-By: Claude Opus 5.5 --- .../DotnetBenchmark/performance-baseline.json | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/Sources/Tests/DotnetBenchmark/performance-baseline.json b/Sources/Tests/DotnetBenchmark/performance-baseline.json index 4631bf112..6c1f7da30 100644 --- a/Sources/Tests/DotnetBenchmark/performance-baseline.json +++ b/Sources/Tests/DotnetBenchmark/performance-baseline.json @@ -1,94 +1,94 @@ { "comment": "Allocated bytes and mean nanoseconds per operation for the popular use cases of https://github.com/asc-community/AngouriMath/issues/746. Checked by PerformanceGate.cs; see Sources/AngouriMath/Docs/WhatsNew/version_performance_control.md for when updating it is legitimate.", "benchmark": "CommonFunctionsInterVersion", - "commit": "6a300d9e0972febc6121b5262a2ab53e7a0c5875", - "measuredOn": "2026-11-29", + "commit": "3c40ee59ee7daa6f5206aecff9a4168ff009c0b3", + "measuredOn": "2026-12-03", "runtime": ".NET 10.0.15", "machine": "Ubuntu 26.04.1 LTS, X64, 4 logical cores", "cases": { "CompileEasy": { - "allocatedBytes": 11610, - "meanNanoseconds": 371164.9079402043 + "allocatedBytes": 11626, + "meanNanoseconds": 344126.05332728795 }, "CompileHard": { - "allocatedBytes": 20002, - "meanNanoseconds": 449587.0790264423 + "allocatedBytes": 20020, + "meanNanoseconds": 412609.3494140625 }, "Derivate": { - "allocatedBytes": 24363, - "meanNanoseconds": 10749.053087087777 + "allocatedBytes": 24563, + "meanNanoseconds": 11312.487815455386 }, "EvalEasy": { "allocatedBytes": 0, - "meanNanoseconds": 4.677396980779512 + "meanNanoseconds": 4.936430067888328 }, "EvalPolynomialFresh": { "allocatedBytes": 11768, - "meanNanoseconds": 6405.616791788737 + "meanNanoseconds": 6707.986476135254 }, "EvalPolynomialFresh15Digits": { "allocatedBytes": 11344, - "meanNanoseconds": 6738.561392211914 + "meanNanoseconds": 6906.78533063616 }, "EvalTranscendentalFresh": { - "allocatedBytes": 34128, - "meanNanoseconds": 24908.826869084285 + "allocatedBytes": 34192, + "meanNanoseconds": 25228.853013258715 }, "EvalTrig": { "allocatedBytes": 69368, - "meanNanoseconds": 50154.798732212614 + "meanNanoseconds": 50369.36917317708 }, "EvalTrigPrecise": { "allocatedBytes": 336082, - "meanNanoseconds": 647531.088297526 + "meanNanoseconds": 617807.6335100447 }, "ParseEasy": { "allocatedBytes": 18200, - "meanNanoseconds": 8551.643139648437 + "meanNanoseconds": 8853.872195434571 }, "ParseHard": { - "allocatedBytes": 58944, - "meanNanoseconds": 36917.50787760417 + "allocatedBytes": 58824, + "meanNanoseconds": 38975.81295776367 }, "RunEasy": { "allocatedBytes": 0, - "meanNanoseconds": 23.513746988773345 + "meanNanoseconds": 29.61389327843984 }, "RunHard": { "allocatedBytes": 0, - "meanNanoseconds": 322.97689793904624 + "meanNanoseconds": 377.49524835745495 }, "RunMedium": { "allocatedBytes": 0, - "meanNanoseconds": 200.21510960261028 + "meanNanoseconds": 224.6864652633667 }, "SimplifyEasy": { - "allocatedBytes": 28120, - "meanNanoseconds": 40918.72411237444 + "allocatedBytes": 28121, + "meanNanoseconds": 42829.61381748744 }, "SimplifyHard": { - "allocatedBytes": 134601944, - "meanNanoseconds": 103344955.84615384 + "allocatedBytes": 134913208, + "meanNanoseconds": 117878599.10714285 }, "SolveEasy": { - "allocatedBytes": 419507, - "meanNanoseconds": 300131.19301060267 + "allocatedBytes": 420399, + "meanNanoseconds": 325486.75798152044 }, "SolveEasyMedium": { - "allocatedBytes": 65798, - "meanNanoseconds": 28011.18859354655 + "allocatedBytes": 65717, + "meanNanoseconds": 29347.054384358726 }, "SolveHard": { - "allocatedBytes": 9183808, - "meanNanoseconds": 103956216.07142857 + "allocatedBytes": 6209376, + "meanNanoseconds": 96410912.92307693 }, "SolveMedium": { - "allocatedBytes": 452169, - "meanNanoseconds": 412951.9587965745 + "allocatedBytes": 456039, + "meanNanoseconds": 412897.28899739584 }, "SolveMediumHard": { - "allocatedBytes": 1404280, - "meanNanoseconds": 11842235.96763393 + "allocatedBytes": 1408652, + "meanNanoseconds": 13213132.880580356 } }, "ungated": {