Skip to content

Round a computed value onto zero only where its terms cancelled - #2072

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
a-value-is-zero-only-where-its-terms-cancelled
Oct 22, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
a-value-is-zero-only-where-its-terms-cancelled

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Part of #1944. Follows #2066.

The defect. A number an evaluation produced was rounded onto zero below half the working digits outright, 1e-50 at the default hundred, whatever had produced it. On 2.6.0:

  • e^(-155) was 0 at a hundred digits.
  • The modulus of a complex number near 1e-136 was 0, and a quotient by that modulus was NaN.
  • The derivative of a right antiderivative of 1/sqrt(a + i a sinh(c + d x)) at x = 227283/1250 was NaN at up to two hundred digits. At three hundred digits it was 1.5329…, where it is 2.0185e-68 (1 - i).

The change. A residual of cancellation is a difference below half the working digits of its terms. That is now the only value rounded onto zero by size.

  • Sums. A sum of opposite signs is measured against one of its terms.
  • Operations that cancel nothing. A product, a quotient, a power, a root, an exponential or a modulus is never rounded onto zero.
  • Cosines and sines. Their residual is measured against the modulus it multiplies.
  • Unchanged. sin(pi), e^(i pi) + 1 and sqrt(2) sqrt(3) - sqrt(6) are still 0. Incorrect work with large values in the matrix #561's matrix times its inverse is still the identity.
  • Division. A quotient of decimals is divided to the working precision's places, as before, and divided again to its digits where that lost them. To places, 3.5e-68 kept thirty-three digits, and a quotient below 1e-100 was 0.

What it does not do. The derivative above is right from two hundred digits. Its terms cancel in their sixty-eighth digit, so below about twice that many digits the difference is under half of them and reads as a residual. At a hundred digits it comes out as the real part without the imaginary one, where it was NaN. An evaluation that cancels more digits than half the working precision cannot be told apart from exact cancellation without carrying an error bound through the arithmetic.

Allocation. It matches master's: 12,731 B/op against 12,743 on the kernel gate's polynomial at a hundred digits, and 12,077 against 12,077 at fifteen. The scale is either term, which costs nothing, where comparing the two rescaled a mantissa. A quotient is divided again only when it lost digits, where dividing every quotient to digits cost 680 bytes more each.

AResidualIsZeroOnlyWhereItCancelledTest, against mpmath, covers:

  • small products, powers and exponentials at a hundred digits;
  • a small quotient with its imaginary part;
  • the root of a small negative;
  • residuals that are still exact;
  • the derivative at 200 and 300 digits;
  • the integrand at 50 and 100 digits.

Results:

  • Unit suite: passes (16,765).
  • Corpus: the full Rubi corpus, run against the 2.6.0 run on the same harness (c614b2c8), is 48,305 of 48,305, with no row moved and no answer's allocation grown.
  • Build: the library builds for netstandard2.0.
  • BREAKING-CHANGES: the entry gives the values on 2.6.0 and now.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

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.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
@Rafael-SOWNet Rafael-SOWNet added this to the 2.7.0 milestone Oct 22, 2026
@Rafael-SOWNet
Rafael-SOWNet merged commit 1c1c506 into master Oct 22, 2026
34 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Oct 22, 2026
Rafael-SOWNet added a commit that referenced this pull request Oct 22, 2026
A function's value was rounded onto zero below half the working digits
outright. That floor is for the residual a function leaves at its own
zeros: sin(pi) evaluates to about 1e-100. That residual is relative to the
argument, though, and a function of an exact small argument cancels
nothing, so sin(10^(-60)), arctan(10^(-60)), erf(10^(-60)) and
Si(10^(-60)) were all 0 at a hundred digits.

The sine, cosine, tangent, logarithm, arcsine, arccosine and arctangent of
a real argument, the error functions, and Si and Shi now create their
value at the argument's scale, as #2072 made arithmetic do with what
cancelled. sin(pi), sin(100 pi), cos(pi/2), tan(pi) and arccos(1) are still
0. The argument is passed as it is and its modulus taken only on the path
that tests for a residual, so the kernel workloads allocate as before.

sinh(10^(-60)) is still 0: it is evaluated from e^(10^(-60)), which is 1
within half the digits, a cancellation the working precision cannot
resolve.

Fixes #2074.


Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant