Skip to content

Commit 1c1c506

Browse files
Round a computed value onto zero only where its terms cancelled (#2072)
A number an evaluation produced was rounded onto zero below half the working digits outright, 1e-50 at a hundred, whatever produced it. So e^(-155) was 0 at a hundred digits, the modulus of a complex number near 1e-136 was 0 and a quotient by it NaN, and the derivative of a right antiderivative of 1/sqrt(a + i a sinh(c + d x)) at a large argument was NaN at up to two hundred digits and 1.5329 at three hundred, where it is 2.0185e-68 (1 - i). A residual of cancellation is a difference below half the working digits of its terms, and that is now the only value rounded onto zero by size: a sum of opposite signs is measured against one of its terms, and a product, a quotient, a power, a root, an exponential or a modulus, which cancel nothing, is never rounded onto zero; a cosine's or a sine's residual is measured against the modulus it multiplies. sin(pi), e^(i pi) + 1 and sqrt(2) sqrt(3) - sqrt(6) are still 0, and #561's matrix times its inverse is still the identity. A quotient of decimals is divided to the working precision's places, as it was, and divided again to its digits where that lost them: 3.5e-68 kept thirty-three digits, and a quotient below 1e-100 was 0. The derivative is right from two hundred digits. Its terms cancel in their sixty-eighth digit, so below about twice that the difference is under half the digits and reads as a residual: at a hundred it is the real part without the imaginary one, where it was NaN. The scale is either term, which costs nothing, where comparing the two rescaled a mantissa: allocation per evaluation is master's, measured on the kernel gate's polynomial at a hundred and at fifteen digits. Part of #1944. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent db5573d commit 1c1c506

5 files changed

Lines changed: 252 additions & 54 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,34 @@ is rounded as before: `(Entity)(1m / 3)` is still `1/3` ([#1944](https://github.
144144
| The derivative of the antiderivative of `1/sqrt(a + i*a*sinh(c + d*x))` at `a, c, d, x = 21/10, 19/10, 17/10, 227283/1250`, at 2000 digits | `2.0185464…E-68` | `2.0185464…E-68 - 2.0185464…E-68i` |
145145
| `"1/3 + 1e-60".EvalNumerical()`, `"sin(pi)".EvalNumerical()`, `(Entity)(1.0 / 3)` | `1/3`, `0`, `1/3` | `1/3`, `0`, `1/3` |
146146

147+
### A computed value is zero only where its terms cancelled
148+
149+
**Silent: wrong answers, and values where there was NaN.** A number an evaluation produced was rounded onto zero below
150+
half the working digits outright, `1e-50` at the default hundred. That held for any number, whatever produced it. So
151+
`e^(-155)` was `0` at a hundred digits, the modulus of a complex number near `1e-136` was `0`, and a quotient by that
152+
modulus was `NaN`. The derivative of a right antiderivative of `1/sqrt(a + i a sinh(c + d x))` at a large argument was
153+
therefore `NaN` at up to two hundred digits and `1.5329…` at three hundred, where it is `2.0185…E-68 - 2.0185…E-68i`.
154+
155+
A number is now rounded onto zero only as a residual of the cancellation that produced it: a difference below half the
156+
working digits of its larger term. A product, a quotient, a power, a root, an exponential or a modulus cancels nothing
157+
and is not rounded onto zero, and a cosine's or a sine's residual is measured against the modulus it multiplies.
158+
`sin(pi)`, `e^(i pi) + 1` and `sqrt(2) sqrt(3) - sqrt(6)` are still `0`. A quotient of decimals is rounded to the
159+
working precision's number of decimal places, as it was, and one that has lost digits that way is divided again to the
160+
precision's digits: a quotient of `3.5e-68` kept thirty-three digits, and one below `1e-100` was `0`. The derivative above is right from two hundred digits; its terms cancel in their sixty-eighth digit,
161+
so at fewer than about twice that many digits the difference is below half of them and reads as a residual
162+
([#1944](https://github.com/asc-community/AngouriMath/issues/1944)).
163+
164+
| Input, at the default hundred digits unless stated | Was (2.6.0) | Now |
165+
|---|---|---|
166+
| `"e^(-155)".EvalNumerical()` | `0` | `4.8345416380533358…E-68` |
167+
| `"e^(-60) * e^(-60)".EvalNumerical()` | `0` | `7.6676480737219996…E-53` |
168+
| `"1/(10^67*sqrt(2) + 10^67*sqrt(2)*i)".EvalNumerical()` | `0` | `3.5355339059327376…E-68 - 3.5355339059327376…E-68i` |
169+
| `"sqrt(-e^(-300))".EvalNumerical()` | `0` | `7.1750959731644104…E-66i` |
170+
| The derivative above, at 300 digits | `1.5329402…` | `2.0185464…E-68 - 2.0185464…E-68i` |
171+
| The derivative above, at 200 digits | `NaN` | `2.0185464…E-68 - 2.0185464…E-68i` |
172+
| The derivative above, at 100 digits | `NaN` | `2.0185464…E-68`, without its imaginary part: a residual at that precision |
173+
| `"sin(pi)".EvalNumerical()`, `"(1 + sqrt(2) i) (1 - sqrt(2) i)".EvalNumerical()` | `0`, `3` | `0`, `3` |
174+
147175
---
148176

149177
## 2.6.0 — since 2.5.0

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,36 @@ public static Complex Create(Real real, Real imaginary) =>
8080
/// <summary>
8181
/// Creates an instance of Complex
8282
/// </summary>
83-
public static Complex Create(EDecimal real, EDecimal imaginary) => Create(real, imaginary, fromMachineNumbers: false);
83+
public static Complex Create(EDecimal real, EDecimal imaginary) => Create(real, imaginary, EDecimal.One, EDecimal.One, fromMachineNumbers: false);
84+
85+
/// <summary>
86+
/// A number an arithmetic operation produced, each part zero only as a residual of the cancellation that
87+
/// produced it, as <see cref="Real.CreateAtScale(EDecimal, EDecimal)"/> reads one.
88+
/// https://github.com/asc-community/AngouriMath/issues/1944
89+
/// </summary>
90+
internal static Complex CreateAtScales(EDecimal real, EDecimal imaginary, EDecimal realScale, EDecimal imaginaryScale)
91+
=> Create(real, imaginary, realScale, imaginaryScale, fromMachineNumbers: false);
8492

8593
/// <summary>
8694
/// A number from a pair of <see cref="double"/>s, each part rounded as
8795
/// <see cref="Real.FromMachineNumber(EDecimal)"/> rounds it.
8896
/// </summary>
89-
internal static Complex FromMachineNumbers(EDecimal real, EDecimal imaginary) => Create(real, imaginary, fromMachineNumbers: true);
97+
internal static Complex FromMachineNumbers(EDecimal real, EDecimal imaginary) => Create(real, imaginary, EDecimal.One, EDecimal.One, fromMachineNumbers: true);
9098

91-
private static Complex Create(EDecimal real, EDecimal imaginary, bool fromMachineNumbers)
99+
private static Complex Create(EDecimal real, EDecimal imaginary, EDecimal realScale, EDecimal imaginaryScale, bool fromMachineNumbers)
92100
{
93101
if (!MathS.Settings.DowncastingEnabled)
94102
return new Complex(real, imaginary);
95-
Real Part(EDecimal part) => fromMachineNumbers ? Real.FromMachineNumber(part) : Real.Create(part);
103+
Real Part(EDecimal part, EDecimal scale) => fromMachineNumbers ? Real.FromMachineNumber(part) : Real.CreateAtScale(part, scale);
96104
if (imaginary.IsZero)
97-
return Part(real);
105+
return Part(real, realScale);
98106
if (real.IsNaN() || imaginary.IsNaN())
99107
return Real.NaN;
100-
if (imaginary.IsFinite && imaginary.Abs().LessThan(MathS.Settings.DowncastingTolerance))
101-
return Part(real);
108+
if (imaginary.IsFinite && !imaginaryScale.IsZero && imaginary.Abs().LessThan(
109+
ReferenceEquals(imaginaryScale, EDecimal.One) ? MathS.Settings.DowncastingTolerance : MathS.Settings.DowncastingTolerance.Multiply(imaginaryScale.Abs())))
110+
return Part(real, realScale);
102111
else
103-
return new Complex(Part(real), Part(imaginary));
112+
return new Complex(Part(real, realScale), Part(imaginary, imaginaryScale));
104113
}
105114

106115
/// <summary>
@@ -122,8 +131,10 @@ public void Deconstruct(out Real realPart, out Real imaginaryPart) =>
122131
/// https://github.com/asc-community/AngouriMath/issues/1615
123132
/// </remarks>
124133
public new virtual Real Abs()
125-
=> Real.Create((RealPart.EDecimal * RealPart.EDecimal + ImaginaryPart.EDecimal * ImaginaryPart.EDecimal)
126-
.SqrtByIntegerRoot(MathS.Settings.DecimalPrecisionContext));
134+
// A modulus is never a residual of cancellation: the sum of two squares cancels nothing.
135+
// https://github.com/asc-community/AngouriMath/issues/1944
136+
=> Real.CreateAtScale((RealPart.EDecimal * RealPart.EDecimal + ImaginaryPart.EDecimal * ImaginaryPart.EDecimal)
137+
.SqrtByIntegerRoot(MathS.Settings.DecimalPrecisionContext), EDecimal.Zero);
127138

128139
/// <summary>
129140
/// The phase of a complex number (aka angle)
@@ -136,7 +147,9 @@ public void Deconstruct(out Real realPart, out Real imaginaryPart) =>
136147
public static Complex CreatePolar(EDecimal magnitude, EDecimal phase)
137148
{
138149
var context = MathS.Settings.DecimalPrecisionContext;
139-
return Create(magnitude.Multiply(phase.Cos(context), context), magnitude.Multiply(phase.Sin(context), context));
150+
// The residual of a cosine or a sine is the magnitude's: cos(pi/2) leaves 1e-100 of it.
151+
var scale = magnitude.Abs();
152+
return CreateAtScales(magnitude.Multiply(phase.Cos(context), context), magnitude.Multiply(phase.Sin(context), context), scale, scale);
140153
}
141154

142155
/// <summary>-oo + -ooi</summary>

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,30 @@ private protected Real() : base(null, null) { }
6565
/// such logarithms lost its imaginary part.
6666
/// https://github.com/asc-community/AngouriMath/issues/1944
6767
/// </remarks>
68-
public static Real Create(EDecimal value) => Create(value, fromMachineNumber: false);
68+
public static Real Create(EDecimal value) => Create(value, fromMachineNumber: false, EDecimal.One);
69+
70+
/// <summary>
71+
/// A value an arithmetic operation produced, which is a residual of cancellation, and zero, only below
72+
/// half the working digits of <paramref name="cancelledScale"/>, the size of the terms that cancelled;
73+
/// zero for a value no cancellation produced -- a product, a quotient or a power of nonzero values --
74+
/// which is never rounded onto zero. With the floor at half the working digits outright, every value
75+
/// below 1e-50 was zero at a hundred digits, e^(-155) among them, and a modulus near 1e-136 was zero, so
76+
/// that a quotient by it was NaN. https://github.com/asc-community/AngouriMath/issues/1944
77+
/// </summary>
78+
internal static Real CreateAtScale(EDecimal value, EDecimal cancelledScale)
79+
=> Create(value, fromMachineNumber: false, cancelledScale);
6980

7081
/// <summary>
7182
/// A number from a <see cref="float"/>, a <see cref="double"/> or a <see cref="decimal"/>, which carries
7283
/// that type's digits and not the working precision's: it is the small rational it is near within
7384
/// <see cref="MathS.Settings.PrecisionErrorZeroRange"/>, ten to the minus sixteen, so that <c>1m/3</c>
7485
/// is <c>1/3</c>. https://github.com/asc-community/AngouriMath/issues/1944
7586
/// </summary>
76-
internal static Real FromMachineNumber(EDecimal value) => Create(value, fromMachineNumber: true);
87+
internal static Real FromMachineNumber(EDecimal value) => Create(value, fromMachineNumber: true, EDecimal.One);
7788

7889
// The tolerance is made only past the cheap check below, which almost every value stops at: made at
7990
// the call, it was 580 more bytes on each evaluation of a cubic at a hundred digits.
80-
private static Real Create(EDecimal value, bool fromMachineNumber)
91+
private static Real Create(EDecimal value, bool fromMachineNumber, EDecimal cancelledScale)
8192
{
8293
if (!MathS.Settings.DowncastingEnabled)
8394
return new Real(value);
@@ -91,11 +102,14 @@ private static Real Create(EDecimal value, bool fromMachineNumber)
91102
if (!Rational.MayBeASmallRational(value, MathS.Settings.FloatToRationalIterCount, MathS.Settings.DowncastingToleranceAsDouble))
92103
return new Real(value);
93104
var (intPart, intRest) = value.SplitDecimal();
94-
// If the difference between value & round(value) is zero (see Number.IsZero), we consider value as an integer
105+
// If the difference between value & round(value) is zero (see Number.IsZero), we consider value as an integer.
106+
// Onto zero only as a residual of the cancellation that produced it, below the tolerance times the
107+
// scale of what cancelled; a negative one reaches zero by rounding up.
95108
var tolerance = MathS.Settings.DowncastingTolerance;
96-
if (intRest.LessThan(tolerance))
109+
bool IsAResidual() => value.IsZero || !cancelledScale.IsZero && value.Abs().LessThan(tolerance.Multiply(cancelledScale.Abs()));
110+
if (intRest.LessThan(tolerance) && (!intPart.IsZero || IsAResidual()))
97111
return Integer.Create(intPart);
98-
if (intRest.GreaterThan(1 - tolerance))
112+
if (intRest.GreaterThan(1 - tolerance) && (!intPart.Increment().IsZero || IsAResidual()))
99113
return Integer.Create(intPart.Increment());
100114

101115
var attempt = Rational.FindRationalWithin(value, fromMachineNumber ? MathS.Settings.PrecisionErrorZeroRange : tolerance);

0 commit comments

Comments
 (0)