Skip to content

Commit 0de8f9b

Browse files
committed
Fix corner-case loss of precision in numeric_power().
This fixes a loss of precision that occurs when the first input is very close to 1, so that its logarithm is very small. Formerly, during the initial low-precision calculation to estimate the result weight, the logarithm was computed to a local rscale that was capped to NUMERIC_MAX_DISPLAY_SCALE (1000). However, the base may be as close as 1e-16383 to 1, hence its logarithm may be as small as 1e-16383, and so the local rscale needs to be allowed to exceed 16383, otherwise all precision is lost, leading to a poor choice of rscale for the full-precision calculation. Fix this by removing the cap on the local rscale during the initial low-precision calculation, as we already do in the full-precision calculation. This doesn't change the fact that the initial calculation is a low-precision approximation, computing the logarithm to around 8 significant digits, which is very fast, especially when the base is very close to 1. Patch by me, reviewed by Alvaro Herrera. Discussion: https://postgr.es/m/CAEZATCV-Ceu%2BHpRMf416yUe4KKFv%3DtdgXQAe5-7S9tD%3D5E-T1g%40mail.gmail.com
1 parent 086cda1 commit 0de8f9b

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8050,9 +8050,13 @@ power_var(NumericVar *base, NumericVar *exp, NumericVar *result)
80508050
*/
80518051
ln_dweight = estimate_ln_dweight(base);
80528052

8053+
/*
8054+
* Set the scale for the low-precision calculation, computing ln(base) to
8055+
* around 8 significant digits. Note that ln_dweight may be as small as
8056+
* -SHRT_MAX, so the scale may exceed NUMERIC_MAX_DISPLAY_SCALE here.
8057+
*/
80538058
local_rscale = 8 - ln_dweight;
80548059
local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
8055-
local_rscale = Min(local_rscale, NUMERIC_MAX_DISPLAY_SCALE);
80568060

80578061
ln_var(base, &ln_base, local_rscale);
80588062

src/test/regress/expected/numeric.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,12 @@ select coalesce(nullif(0.9999999999 ^ 23300000000000, 0), 0) as rounds_to_zero;
16061606
0
16071607
(1 row)
16081608

1609+
select round(((1 - 1.500012345678e-1000) ^ 1.45e1003) * 1e1000);
1610+
round
1611+
----------------------------------------------------------
1612+
25218976308958387188077465658068501556514992509509282366
1613+
(1 row)
1614+
16091615
-- cases that used to error out
16101616
select 0.12 ^ (-25);
16111617
?column?

src/test/regress/sql/numeric.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ select 1.2 ^ 345;
899899
select 0.12 ^ (-20);
900900
select 1.000000000123 ^ (-2147483648);
901901
select coalesce(nullif(0.9999999999 ^ 23300000000000, 0), 0) as rounds_to_zero;
902+
select round(((1 - 1.500012345678e-1000) ^ 1.45e1003) * 1e1000);
902903

903904
-- cases that used to error out
904905
select 0.12 ^ (-25);

0 commit comments

Comments
 (0)