Skip to content

Compute the terms of the Ln series in the package's exponent range - #169

Open
shounakjoshi88-a11y wants to merge 1 commit into
cockroachdb:masterfrom
shounakjoshi88-a11y:fix/ln-series-in-the-package-exponent-range
Open

shounakjoshi88-a11y wants to merge 1 commit into
cockroachdb:masterfrom
shounakjoshi88-a11y:fix/ln-series-in-the-package-exponent-range

Conversation

@shounakjoshi88-a11y

Copy link
Copy Markdown

Ln never returned for some inputs:

c := &apd.Context{Precision: 10, MaxExponent: 999, MinExponent: -5, Traps: apd.DefaultTraps}
var d apd.Decimal
c.Ln(&d, apd.NewFromString("0.95"))   // never returns

Python's decimal gives -0.05129329439 for the same context, so the result is nowhere near the exponent range.

The terms of the power series of Ln are much smaller than the result:

ln(1+x) = 2 sum [ 1 / (2n+1) * (x / (x+2))^(2n+1) ]

so a term can be below the exponent range of the context. The series was computed in the caller's range, so such a term was subnormal, the Subnormal trap fired, and the ErrDecimal made the later steps no-ops. The exit test then read a term that was never divided again:

for n := 1; ; n++ {
	ed.Mul(&tmp3, &tmp3, &tmp2)
	ed.Mul(&tmp3, &tmp3, &tmp2)

	// tmp4 = 2n+1
	tmp4.SetFinite(int64(2*n+1), 0)

	ed.Quo(&tmp4, &tmp3, &tmp4)

	ed.Add(&tmp1, &tmp1, &tmp4)

	if tmp4.Abs(&tmp4).Cmp(&eps) <= 0 {
		break
	}
}

tmp4 kept the value of 2n+1, which is never above eps, so the loop never ended. The iteration below it has an ed.Err() check and returns, which is why only the power series hung.

The series is now computed with the package's exponent range, and without the traps that its intermediate results trip:

nc := c.WithPrecision(p)
nc.Rounding = RoundHalfEven
// The terms of the power series are much smaller than the result, so
// they are computed with the package's exponent range: a term below the
// caller's Emin would be subnormal, and the trap would stop the series
// before it converged.
nc.MinExponent = MinExponent
nc.MaxExponent = MaxExponent
// The intermediate results of the series are inexact and can be
// subnormal, which must not stop the computation. The conditions of the
// caller's context are applied to the result.
nc.Traps &^= Inexact | Rounded | Subnormal | Underflow | Clamped

and the loop checks the error of the computation, like the iteration below it does:

	// A trap makes the later steps no-ops, and tmp4 would then keep
	// the value of 2n+1, which is never divided again, so the loop
	// would never end.
	if err := ed.Err(); err != nil {
		return 0, err
	}

The result is still rounded in the caller's context, so the conditions of that context are the ones that are reported.

The new test in context_test.go covers the inputs that hung, with the values Python's decimal gives for the same context:

0.95     -0.05129329439
0.9      -0.1053605157
1.1      0.09531017980
1.001    0.0009995003331
0.99999  -0.00001000005000
9E8      20.61790532

and the Inexact trap of the context is still honoured: the series converges and the trap fires on the result.

The test fails on the unmodified tree, where the series never ends:

context_test.go:58: Ln did not return
--- FAIL: TestLnSeriesWithSubnormalTerms (10.00s)

and passes with the change:

$ go test ./...
ok  	github.com/cockroachdb/apd/v3	2.396s

The GDA conformance suite (TestGDA) is green as well.

This is independent of #160, which replaces the iteration with a different algorithm: this change is about the exponent range the series is computed in, not about the rounding of the result.

The terms of the power series of Ln are much smaller than the result, so
a term can be below the exponent range of the context. The series was
computed in the caller's range, so such a term was subnormal, the trap
fired, and the ErrDecimal made the later steps no-ops. The exit test
reads the term, which is never divided again once the trap has fired, so
it kept the value of 2n+1 and the loop never ended.

Ln(0.95) with a precision of 10 and a MinExponent of -5 never returned.

The series is now computed with the package's exponent range and
without the traps that its intermediate results trip, and the conditions
of the caller's context are applied to the result. The series loop also
checks the error of the computation, like the iteration below it does, so
that a failure ends the loop instead of spinning.

The values are the ones Python's decimal gives for the same context.
@shounakjoshi88-a11y

Copy link
Copy Markdown
Author

The Go workflow on this pull request is in action_required, which needs a maintainer to approve it before it can run on a fork pull request. If you approve it, it should pass - the same commands run green locally:

$ go test ./...
ok  	github.com/cockroachdb/apd/v3	2.396s

The GDA conformance suite (TestGDA) is green as well.

gofmt reports nothing on context_test.go. context.go is not gofmt-clean in this environment, but that is so on the unmodified tree as well: the newer gofmt wants a blank line before the //gcassert:inline directives, which are all over the file. The change itself is formatted the way the file is.

The new test fails on the unmodified tree, where the series never ends:

context_test.go:58: Ln did not return
--- FAIL: TestLnSeriesWithSubnormalTerms (10.00s)

and passes with the change. The values it checks are the ones Python's decimal gives for the same context.

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.

1 participant