Repository navigation
The factorial far from zero, and a quotient by a number whose modulus is nearly whole - #1616
Merged
Merged
Conversation
… is nearly whole The numerical factorial is Spouge's approximation with a a tenth over the precision's digits, and it holds right of zero. It was used left of zero too, where it lost its digits as the argument neared -a and past -a had none: at 30 digits (-38.0785)! was -3.1e-35, where it is 7.03e-43, and at 50 digits (-60.5)! was right to 17 of its 50. Left of zero both paths, real and complex, now take the reflection formula x! = pi x/(sin(pi x) (-x)!), which asks the approximation at -x. Its working context doubled the caller's digits and kept the caller's exponent range, so an intermediate outside that range lost its digits though the value fit: at the default precision (400.5)! was +oo, where it is 1.28e870. The working context now has no bound on the exponent. It is one per precision, so that the constants the library caches per context instance stay few; the complex path built a new one every call. (316.22776601683796i)! was 1e-190, where it is 8.3e-215, below the default precision's floor. That one was the complex division. It read the divisor's modulus through Abs(), which turns the downcasting on to answer a Real, so even with the downcasting off a modulus within its tolerance of a small rational came back as that rational. One quotient of the factorial's series, which runs with the downcasting off, divides by 75 + 316.22776601683796i, whose modulus is 325 - 3e-16. A relative 2e-18 in one quotient put the factorial 24 orders of magnitude out, because the series cancels its terms almost entirely. With the downcasting off, the division now takes the modulus's square root itself, with the same digits wherever nothing was rounded. With it on, the rounding is the setting's own and stays. It keeps the quotients of a divisor like 1 + i sqrt(3) exact, and taking the exact modulus there too cost SolveEasy 5.5% more allocation in the gate. The same defect gave 1/(75 + 316.22776601683796i), with the downcasting off, an imaginary part wrong from the 17th digit. Abs() itself is #1615. FactorialFarFromZeroTest, against mpmath at 120 digits: six rows, all failing without this. A seventh, the reflection off the real line, passes either way. BREAKING-CHANGES.md records each changed answer as measured on 2.5.0. Closes #1614. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rafael-SOWNet
added a commit
that referenced
this pull request
Sep 30, 2026
…orce (#1619) 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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three defects, found while measuring #1607's compiled factorial against the library's own.
Left of zero. The numerical factorial is Spouge's approximation, with
aa tenth over the precision's digits, and it holds right of zero. It was used left of zero too, where it lost its digits as the argument neared-a, and past-ahad none. At 30 digits(-38.0785)!was-3.1e-35, where it is7.03e-43; at 50 digits(-60.5)!was right to 17 of its 50. Left of zero, both paths, real and complex, now take the reflection formulax! = pi x/(sin(pi x) (-x)!), which asks the approximation at-x.Outside the caller's exponent range. The working context doubled the caller's digits and kept the caller's exponent range, so an intermediate outside that range lost its digits though the value fit. At the default precision
(400.5)!was+oo, where it is1.28e870. The working context now has no bound on the exponent. It is one per precision, so that the constants the library caches per context instance stay few; the complex path built a new one every call.A modulus near a small rational.
(316.22776601683796i)!was1e-190, where it is8.3e-215, below the default precision's floor. The cause was the complex division, which read the divisor's modulus throughAbs().Abs()turns the downcasting on to answer aReal, so even with the downcasting off, a modulus within its tolerance of a small rational came back as that rational. One quotient of the factorial's series, which runs with the downcasting off, divides by75 + 316.22776601683796i, whose modulus is325 + 2.6e-17. A relative8e-20in one quotient put the factorial 24 orders of magnitude out, because the series cancels its terms almost entirely.With the downcasting off, the division now takes the modulus's square root itself, with the same digits wherever nothing was rounded. With it on, the rounding is the setting's own, and it stays: it keeps the quotients of a divisor like
1 + i sqrt(3)exact. Taking the exact modulus there as well costSolveEasy5.5% more allocation in the gate's first run. The same defect gave1/(75 + 316.22776601683796i), with the downcasting off, an imaginary part wrong from the 17th digit.Abs()itself is Complex.Abs() rounds a modulus to a nearby rational with the downcasting off #1615.Measured
(400.5)!+oo1.2818906666758219960…E+8701.2818906666758219961e+870(316.22776601683796i)!7.2E-191 + 2.2E-190i0, below the floor;-8.3285461561778…E-215 - 7.33…E-216iunbounded-8.3285461561778e-215 - 7.33e-216i1/(75 + 316.22776601683796i), downcasting off0.00071005917159763313609… - 0.00299387234098781500591…i0.0007100591715976330221… - 0.0029938723409878145254…i(-38.0785)!at 30 digits-3.139E-357.03406751543900910211608330734E-437.0340675154390091021e-43(-60.5)!at 30 digits5.861E-812.93059239453861116335725639905E-812.9305923945386111634e-81FactorialFarFromZeroTest, against mpmath at 120 digits. Six rows fail without this. A seventh, the reflection off the real line, passes either way and checks the new path.BREAKING-CHANGES.md: each changed answer, as measured on 2.5.0.DerivateandEvalPolynomialFresh15Digitsran 1.27x and 1.26x the baseline time, which the gate reports without gating; neither divides a complex number or takes a factorial. The first run, on d25469b2, failedSolveEasyat +5.5% allocation; see above.Closes #1614.
🤖 Generated with Claude Code