Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// </returns>
/// <remarks>
/// Made under the downcasting setting in force, as any other number is. It turned the
/// downcasting on to answer a <see cref="Real"/>, so with it off a modulus within its
/// tolerance of a small rational came back as that rational: the modulus of
/// <c>75 + 316.22776601683796i</c>, <c>325 + 2.6e-17</c>, was 325.
/// https://github.com/asc-community/AngouriMath/issues/1615
/// </remarks>
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>()
);
=> Real.Create((RealPart.EDecimal * RealPart.EDecimal + ImaginaryPart.EDecimal * ImaginaryPart.EDecimal)
.SqrtByIntegerRoot(MathS.Settings.DecimalPrecisionContext));

/// <summary>
/// The phase of a complex number (aka angle)
Expand Down
22 changes: 10 additions & 12 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,16 @@ internal static Complex OpDiv<T>(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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Tests/UnitTests/Core/FactorialFarFromZeroTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void PastTheCeilingOfTheWorkingContext()
/// <summary>
/// <c>(316.22776601683796i)!</c> was <c>1e-190</c>, where it is <c>8e-215</c>. One of its
/// series' quotients divides by <c>75 + 316.22776601683796i</c>, whose modulus is
/// <c>325 - 3e-16</c>, and the division read that modulus as 325.
/// <c>325 + 2.6e-17</c>, and the division read that modulus as 325.
/// </summary>
[Fact]
public void WhereADivisorsModulusIsNearlyWhole()
Expand Down
22 changes: 22 additions & 0 deletions Sources/Tests/UnitTests/Core/Numeric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}

/// <summary>
/// The modulus is made under the downcasting setting in force. It turned the downcasting on
/// for itself, so with it off <c>|75 + 316.22776601683796i|</c>, which is <c>325 + 2.6e-17</c>,
/// came back as 325. With it on, 325 is the setting's own rounding.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1615">#1615</a>
/// </summary>
[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());
}
}
}
Loading