Fix corner-case loss of precision in numeric ln().
authorDean Rasheed <dean.a.rasheed@gmail.com>
Sun, 1 Mar 2020 14:49:25 +0000 (14:49 +0000)
committerDean Rasheed <dean.a.rasheed@gmail.com>
Sun, 1 Mar 2020 14:49:25 +0000 (14:49 +0000)
commit43a899f41f46918a0bf442edb091b08c214c68f8
tree0225a0e199c74cadb8df19adba58796025b0b986
parentdba915333967609cc7ca73f0904dfdc08db0a052
Fix corner-case loss of precision in numeric ln().

When deciding on the local rscale to use for the Taylor series
expansion, ln_var() neglected to account for the fact that the result
is subsequently multiplied by a factor of 2^(nsqrt+1), where nsqrt is
the number of square root operations performed in the range reduction
step, which can be as high as 22 for very large inputs. This could
result in a loss of precision, particularly when combined with large
rscale values, for which a large number of Taylor series terms is
required (up to around 400).

Fix by computing a few extra digits in the Taylor series, based on the
weight of the multiplicative factor log10(2^(nsqrt+1)). It remains to
be proven whether or not the other 8 extra digits used for the Taylor
series is appropriate, but this at least deals with the obvious
oversight of failing to account for the effects of the final
multiplication.

Per report from Justin AnyhowStep. Reviewed by Tom Lane.

Discussion: https://postgr.es/m/16280-279f299d9c06e56f@postgresql.org
src/backend/utils/adt/numeric.c
src/test/regress/expected/numeric_big.out
src/test/regress/sql/numeric_big.sql