Skip to content

Return the series error from Context.Exp - #164

Open
sebxt wants to merge 1 commit into
cockroachdb:masterfrom
sebxt:fix/exp-drops-series-error
Open

sebxt wants to merge 1 commit into
cockroachdb:masterfrom
sebxt:fix/exp-drops-series-error

Conversation

@sebxt

@sebxt sebxt commented Oct 19, 2026

Copy link
Copy Markdown

Context.Exp checked the error from its series loop with if err != ed.Err(), where err was the nil result of an earlier call. When the loop failed, Exp returned 0, nil, left d unchanged and reported no condition.

This changes the check to if err := ed.Err(); err != nil, matching the other error checks in context.go.

Repro: with Context{Precision: 30, MaxExponent: 20, MinExponent: -20, Traps: DefaultTraps}, Exp(d, 1E-20) left d at its previous value and returned no error, because the series term 1E-20/2 trips the Subnormal trap. It now returns the subnormal error.

Added TestExpSeriesErr, which fails before this change and passes after. go test ./..., go test -race ./... and go vet -unsafeptr=false ./... pass.

Fixes #163

Signed-off-by: Sebastian Turner <sebastiant3484@gmail.com>

@mw5h mw5h left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified this at 59db5fe. The change is correct and I could not make it regress anything; details below plus two suggestions and one adjacent bug worth its own issue.

Verification. git blame puts the line at 1eddda3 (2017, "Refactor quo, exp; improve ln, log10, pow"). err there is the result of ra.Float64() that was just checked != nil, so the condition always read if nil != ed.Err() { return 0, nil }. grep '!= ed.Err' finds no other instance; every other ed.Err() site in the file uses the if err := ed.Err(); err != nil idiom. The early-return condition is unchanged by the PR and d is not written before that point in either version, so a call that previously completed cannot now fail early. A differential run of 4200 calls (600 random inputs × 7 contexts) on master vs. this branch shows 134 differing lines, all differing only in err (nil → inexact ×133 with Inexact trapped, nil → subnormal ×1 in the issue's context), with identical d and Condition everywhere. TestExpSeriesErr fails on master (got 42 with no error) and passes here. CockroachDB's DecimalCtx and CUE's BaseContext.WithPrecision(34) cannot reach this branch on real inputs because the Stage 1 shortcut returns 1 for tiny |x| and neither traps Inexact/Rounded (checked Exp(1E-2000) etc. under the CRDB context: 1 inexact, rounded, no error, both branches). Clean merge onto master; go test, -race, and go vet -unsafeptr=false pass.

1. The new test accepts any error for any reason. It only asserts on d when err == nil, so it would also pass if Exp returned too many iterations or an inexact error for the wrong reason. Since the whole point of the fix is which error surfaces, I'd assert it:

_, err := c.Exp(d, x)
switch {
case err == nil:
    if d.String() != expected { t.Fatalf(...) }
case !strings.Contains(err.Error(), "subnormal"):
    t.Fatalf("unexpected error %v", err)
}

Also worth one case for the far more common manifestation of this bug: BaseContext.WithPrecision(30) with Traps |= Inexact, Exp(d, 1). On master that returns (0, nil) and leaves d untouched (silent garbage for anyone trapping Inexact); here it returns inexact.

2. Optional: return ed.Flags rather than 0. Pow and integerPower return ed.Flags, err in the same situation and goError always returns the flags alongside the trap error. Exp's other internal error paths also return 0, so this is locally consistent and not a regression; just more informative for callers that inspect the Condition.

3. For the record: the post-fix result for the issue's repro is a subnormal trap, while GDA/Python give 1.00000000000000000001000000000 with only Inexact+Rounded. The Subnormal comes from the intermediate r/2 = 5E-21 because the series runs under c.WithPrecision(cp), which inherits the caller's exponent limits. An error is strictly better than silent garbage, so no objection here; a robust follow-up would run the series under widened limits like Pow/Cbrt do, but Exp currently relies on nc's MaxExponent inside integerPower for Overflow, so that is out of scope.

4. Adjacent pre-existing bug, not this PR: Ln's power-series loop (for n := 1; ; n++ around context.go:781 here) never terminates once ed holds an error. tmp4.SetFinite(2n+1, 0) bypasses ed, the subsequent ed.Quo/ed.Add become no-ops, so tmp4 stays at 2n+1 >= 3 and tmp4.Abs(&tmp4).Cmp(&eps) <= 0 can never fire; unlike the Halley loop below it there is no ed.Err() check inside. Reproduced on master and here: Ln(1.00000000000000000001) under the issue's context times out, and under a CockroachDB-shaped context (precision 20, MinExponent -2000, Subnormal trapped) Ln("1." + 1999 zeros + "1") never returns, i.e. SELECT ln(1.000…0001) hangs. Fix is an if err := ed.Err(); err != nil { return 0, err } inside the loop. I mention it because issue #163 says the other ed.Err() checks handle this correctly, and this loop is the one place that does not. Deserves a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context.Exp returns success and leaves the result unset when its series step fails

2 participants