Fix ln() rounding up on a result just below a rounding tie - #261
Open
spokodev wants to merge 1 commit into
Open
Conversation
For an argument whose natural log lands just below a rounding tie (the digit after the rounding position is followed by a long run of 9s, i.e. `...d 4999...`), ln() rounded up instead of down, violating the documented "correctly rounded" guarantee. naturalLogarithm/naturalExponential drive checkRoundingDigits with the `repeating` flag. In that branch the argument-reduction reconstruction can land the working value on a false exact-tie form (`...5000` / `...0000`), but the repeating branch only detected a trailing `...9999` run — unlike the non-repeating branch, which also detects `...5000`/`...0000` ties. So the near-tie was treated as resolved and finalise() rounded the false tie up. Mirror the tie/zero detection the non-repeating branch already has. Verified: a near-tie sweep of ln and exp across the five half-rounding modes (20000 cases) vs an 80-digit oracle now reports 0 mis-roundings (several were wrong before), and the full test suite passes.
Owner
|
Hi. I have seen this and will be on it shortly. Thank you. |
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.
Problem
ln()is documented as correctly rounded, but for an argument whose naturallog lands just below a rounding tie it rounds up instead of down:
The true value, computed at high precision, is
1.44906776015093163434999999999999999999999999975859147…— the 21stsignificant digit is
4followed by a long run of9s, i.e. strictly belowthe
…3435halfway point, so every half-rounding mode must round down to…343. mpmath and this library atprecision: 60agree.Affects all five HALF rounding modes (the default). Incidence on random input is
tiny but the failures are deterministic and reachable.
Root cause
naturalLogarithmandnaturalExponentialextend precision in a loop guarded bycheckRoundingDigits(…, repeating). When the argument-reduction reconstructionlands the working value on a false exact-tie form (
…5000/…0000), therepeating branch of
checkRoundingDigitsonly detects a trailing…9999run — unlike the non-repeating branch, which also detects
…5000/…0000ties:So the near-tie was reported as resolved and
finaliserounded the false tie up.Fix
Add the same
…5000/…0000tie/zero detection to the repeating branch, mirroringthe non-repeating branch (only
naturalLogarithm/naturalExponentialreach it).Applied identically to
decimal.jsanddecimal.mjs.Verification
lncases.(20000 cases) against an 80-digit oracle reports 0 mis-roundings; several
of the same inputs were mis-rounded before the fix.
Test
Added five correctly-rounded
lncases totest/modules/ln.js(results ending…d 4999…). They fail onmaster(round up) and pass with the fix.