From 4859f91d40bb3f14c7c7fa8d1659a0bfcebe7226 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Wed, 30 Sep 2026 09:24:43 +0000 Subject: [PATCH] A complex number's modulus is made under the downcasting setting in force Complex.Abs() turned the downcasting on to answer a Real, so with the downcasting off a modulus within its tolerance of a small rational came back as that rational: |75 + 316.22776601683796i|, which is 325 + 2.6e-17, was 325. It now builds the square root under the setting in force, as any other number is made. With the downcasting on nothing changes: the rounding is the setting's own. The complex division took its modulus through Abs(), and #1616 gave it a branch of its own for the downcasting off. With Abs() keeping the setting, the division calls it again, and the two agree by construction. #1616's comment, its breaking-changes entry and its test gave that modulus as 325 - 3e-16. It is 325 + 2.6e-17 (mpmath, 150 digits), a relative 8e-20 and not 2e-18, and all three are corrected here. The fix they describe stands: the modulus is still within the downcasting's tolerance of 325. Numeric gains the modulus with the downcasting off, through Abs() and through abs(...), against mpmath to 90 decimal places, and with it on, 325 as before. Closes #1615. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 11 ++++++---- .../Entity.Continuous.Complex.Definition.cs | 14 +++++++----- .../Entity/Continuous/Number/Operators.cs | 22 +++++++++---------- .../Core/FactorialFarFromZeroTest.cs | 2 +- Sources/Tests/UnitTests/Core/Numeric.cs | 22 +++++++++++++++++++ 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b71dcb1c9..03e1be289 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -130,11 +130,13 @@ zero it now takes the reflection formula, `x! = pi x/(sin(pi x) (-x)!)`. Its wor bounded the exponent where the caller's did, so an intermediate outside that range lost its digits though the value fit. And the complex division read a divisor's modulus through `Abs()`, which turns the downcasting on, so even with it off a modulus within its tolerance of a small rational -was that rational: `75 + 316.22776601683796i`, whose modulus is `325 - 3e-16`, divided as by 325. +was that rational: `75 + 316.22776601683796i`, whose modulus is `325 + 2.6e-17`, divided as by 325. The factorial's series, which runs with the downcasting off, cancels such quotients almost -entirely, which made `(316.22776601683796i)!` 24 orders of magnitude out. With the downcasting off -the division now takes the modulus as it is; with it on, the rounding is the setting's own and -stays ([#1614](https://github.com/asc-community/AngouriMath/issues/1614)). +entirely, which made `(316.22776601683796i)!` 24 orders of magnitude out. `Abs()` now makes the +modulus under the downcasting setting in force, so with it off the modulus is as it is, and so is +the division's; with it on, the rounding is the setting's own and stays +([#1614](https://github.com/asc-community/AngouriMath/issues/1614), +[#1615](https://github.com/asc-community/AngouriMath/issues/1615)). | Input | Was (2.5.0) | Now | |---|---|---| @@ -143,6 +145,7 @@ stays ([#1614](https://github.com/asc-community/AngouriMath/issues/1614)). | `"1/(75 + 316.22776601683796i)"`, downcasting off | `0.00071005917159763313609… - 0.00299387234098781500591…i` | `0.00071005917159763302214990… - 0.00299387234098781452548…i` | | `"(-38.0785)!"` at 30 digits, downcasting off | `-3.13914586443552289958547548164E-35` | `7.03406751543900910211608330734E-43` | | `"(-60.5)!"` at 30 digits, downcasting off | `5.86118478907722232671451280188E-81` | `2.93059239453861116335725639905E-81` | +| `"abs(75 + 316.22776601683796i)"`, downcasting off | `325` | `325.000000000000026076735749…` | ### A quotient of polynomials is split into coprime blocks before a root is peeled off 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 2137e6c09..ed16435d0 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs @@ -105,12 +105,16 @@ public void Deconstruct(out Real realPart, out Real imaginaryPart) => /// Returns the absolute value of this complex number, to be precise, /// if this = a + ib, this.Abs() -> sqrt(a^2 + b^2) /// + /// + /// Made under the downcasting setting in force, as any other number is. It turned the + /// downcasting on to answer a , so with it off a modulus within its + /// tolerance of a small rational came back as that rational: the modulus of + /// 75 + 316.22776601683796i, 325 + 2.6e-17, was 325. + /// https://github.com/asc-community/AngouriMath/issues/1615 + /// public new virtual Real Abs() - => // we need forcing downcasting so that we could - // downcast to a Real - MathS.Settings.DowncastingEnabled.As(true, - () => Sqrt(RealPart.EDecimal * RealPart.EDecimal + ImaginaryPart.EDecimal * ImaginaryPart.EDecimal).Downcast() - ); + => Real.Create((RealPart.EDecimal * RealPart.EDecimal + ImaginaryPart.EDecimal * ImaginaryPart.EDecimal) + .SqrtByIntegerRoot(MathS.Settings.DecimalPrecisionContext)); /// /// The phase of a complex number (aka angle) diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs index 127602fea..da379c455 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs @@ -136,18 +136,16 @@ internal static Complex OpDiv(T a, T b) where T : Number => * => ans = (a + ib) * (c - id) / (c2 + d2) */ var conj = b.Conjugate; - // With the downcasting off, the modulus as it is. Abs() turns the downcasting - // on to answer a Real, and so read a modulus within its tolerance of a small - // rational as that rational: 75 + 316.22776601683796i, whose modulus is - // 325 - 3e-16, was divided as if by one of 325. A relative 2e-18 is nothing to - // one quotient. It put a factorial out by 24 orders of magnitude, whose series - // of such quotients cancels its terms almost entirely. With the downcasting on, - // that reading is the setting's own, and it keeps the quotients of a divisor - // like 1 + i sqrt(3) exact. https://github.com/asc-community/AngouriMath/issues/1614 - var bAbs = MathS.Settings.DowncastingEnabled - ? b.Abs().EDecimal - : (b.RealPart.EDecimal * b.RealPart.EDecimal + b.ImaginaryPart.EDecimal * b.ImaginaryPart.EDecimal) - .SqrtByIntegerRoot(MathS.Settings.DecimalPrecisionContext); + // The modulus under the downcasting setting in force, which Abs() now keeps. + // With it off, the modulus as it is: 75 + 316.22776601683796i, whose modulus + // is 325 + 2.6e-17, was divided as if by one of 325. A relative 8e-20 is + // nothing to one quotient, and it put a factorial 24 orders of magnitude out, + // whose series of such quotients cancels its terms almost entirely. With it on, + // the rounding is the setting's own, and it keeps the quotients of a divisor + // like 1 + i sqrt(3) exact. + // https://github.com/asc-community/AngouriMath/issues/1614 + // https://github.com/asc-community/AngouriMath/issues/1615 + var bAbs = b.Abs().EDecimal; // The squared modulus of a divisor of 3 * 10^125 is 10^251 and its // reciprocal is under the context's exponent floor, so the quotient // came out zero where it was 6 * 10^-7 -- a right antiderivative's diff --git a/Sources/Tests/UnitTests/Core/FactorialFarFromZeroTest.cs b/Sources/Tests/UnitTests/Core/FactorialFarFromZeroTest.cs index 580198dca..a2b094839 100644 --- a/Sources/Tests/UnitTests/Core/FactorialFarFromZeroTest.cs +++ b/Sources/Tests/UnitTests/Core/FactorialFarFromZeroTest.cs @@ -59,7 +59,7 @@ public void PastTheCeilingOfTheWorkingContext() /// /// (316.22776601683796i)! was 1e-190, where it is 8e-215. One of its /// series' quotients divides by 75 + 316.22776601683796i, whose modulus is - /// 325 - 3e-16, and the division read that modulus as 325. + /// 325 + 2.6e-17, and the division read that modulus as 325. /// [Fact] public void WhereADivisorsModulusIsNearlyWhole() diff --git a/Sources/Tests/UnitTests/Core/Numeric.cs b/Sources/Tests/UnitTests/Core/Numeric.cs index 1cc691112..7710e0097 100644 --- a/Sources/Tests/UnitTests/Core/Numeric.cs +++ b/Sources/Tests/UnitTests/Core/Numeric.cs @@ -429,5 +429,27 @@ public void AComplexDivisionWithALargeDivisor(string ar, string ai, string br, s Assert.True(quotient.RealPart.EDecimal.Subtract(D(qr)).Abs().CompareTo(D(qr).Abs().Multiply(tolerance).Add(EDecimal.Create(1, -180))) <= 0, $"real part {quotient.RealPart} for {qr}"); Assert.True(quotient.ImaginaryPart.EDecimal.Subtract(D(qi)).Abs().CompareTo(D(qi).Abs().Multiply(tolerance).Add(EDecimal.Create(1, -180))) <= 0, $"imaginary part {quotient.ImaginaryPart} for {qi}"); } + + /// + /// The modulus is made under the downcasting setting in force. It turned the downcasting on + /// for itself, so with it off |75 + 316.22776601683796i|, which is 325 + 2.6e-17, + /// came back as 325. With it on, 325 is the setting's own rounding. + /// #1615 + /// + [Fact] + public void AModulusIsMadeUnderTheDowncastingInForce() + { + // sqrt(75^2 + 316.22776601683796^2), from mpmath at 150 digits. + var expected = EDecimal.FromString("325.0000000000000260767357490722475692366964200883807015522337353559864579715873660423128636761679756827129766030459244723400170041"); + using (MathS.Settings.DowncastingEnabled.Set(false)) + { + var modulus = Complex.Create(EDecimal.FromInt32(75), EDecimal.FromString("316.22776601683796")).Abs(); + Assert.True(modulus.EDecimal.Subtract(expected).Abs().CompareTo(EDecimal.Create(1, -90)) < 0, $"{modulus}"); + var evaluated = "abs(75 + 316.22776601683796i)".ToEntity().EvalNumerical(); + Assert.True(evaluated.RealPart.EDecimal.Subtract(expected).Abs().CompareTo(EDecimal.Create(1, -90)) < 0, $"{evaluated}"); + } + using (MathS.Settings.DowncastingEnabled.Set(true)) + Assert.Equal(Integer.Create(325), Complex.Create(EDecimal.FromInt32(75), EDecimal.FromString("316.22776601683796")).Abs()); + } } }