Skip to content

Commit 5c62920

Browse files
committed
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising numbers to very large powers. Firstly, when raising a negative number to a very large integer power, the result should be well-defined, but the previous code would only cope if the exponent was small enough to go through power_var_int(). Otherwise it would throw an internal error, attempting to take the logarithm of a negative number. Fix this by adding suitable handling to the general case in power_var() to cope with negative bases, checking for integer powers there. Next, when raising a (positive or negative) number whose absolute value is slightly less than 1 to a very large power, the result should approach zero as the power is increased. However, in some cases, for sufficiently large powers, this would lose all precision and return 1 instead of 0. This was due to the way that the local_rscale was being calculated for the final full-precision calculation: local_rscale = rscale + (int) val - ln_dweight + 8 The first two terms on the right hand side are meant to give the number of significant digits required in the result ("val" being the estimated result weight). However, this failed to account for the fact that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE (1000), and the result weight might be less then -1000, causing their sum to be negative, leading to a loss of precision. Fix this by forcing the number of significant digits calculated to be nonnegative. It's OK for it to be zero (when the result weight is less than -1000), since the local_rscale value then includes a few extra digits to ensure an accurate result. Finally, add additional underflow checks to exp_var() and power_var(), so that they consistently return zero for cases like this where the result is indistinguishable from zero. Some paths through this code already returned zero in such cases, but others were throwing overflow errors. Dean Rasheed, reviewed by Yugo Nagata. Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
1 parent d9589eb commit 5c62920

File tree

3 files changed

+132
-16
lines changed

3 files changed

+132
-16
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,20 +3137,16 @@ numeric_power(PG_FUNCTION_ARGS)
31373137
/*
31383138
* The SQL spec requires that we emit a particular SQLSTATE error code for
31393139
* certain error conditions. Specifically, we don't return a
3140-
* divide-by-zero error code for 0 ^ -1.
3140+
* divide-by-zero error code for 0 ^ -1. Raising a negative number to a
3141+
* non-integer power must produce the same error code, but that case is
3142+
* handled in power_var().
31413143
*/
31423144
if (cmp_var(&arg1, &const_zero) == 0 &&
31433145
cmp_var(&arg2, &const_zero) < 0)
31443146
ereport(ERROR,
31453147
(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
31463148
errmsg("zero raised to a negative power is undefined")));
31473149

3148-
if (cmp_var(&arg1, &const_zero) < 0 &&
3149-
cmp_var(&arg2, &arg2_trunc) != 0)
3150-
ereport(ERROR,
3151-
(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
3152-
errmsg("a negative number raised to a non-integer power yields a complex result")));
3153-
31543150
/*
31553151
* Call power_var() to compute and return the result; note it handles
31563152
* scale selection itself.
@@ -8046,12 +8042,18 @@ exp_var(const NumericVar *arg, NumericVar *result, int rscale)
80468042
*/
80478043
val = numericvar_to_double_no_overflow(&x);
80488044

8049-
/* Guard against overflow */
8045+
/* Guard against overflow/underflow */
80508046
/* If you change this limit, see also power_var()'s limit */
80518047
if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
8052-
ereport(ERROR,
8053-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8054-
errmsg("value overflows numeric format")));
8048+
{
8049+
if (val > 0)
8050+
ereport(ERROR,
8051+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8052+
errmsg("value overflows numeric format")));
8053+
zero_var(result);
8054+
result->dscale = rscale;
8055+
return;
8056+
}
80558057

80568058
/* decimal weight = log10(e^x) = x * log10(e) */
80578059
dweight = (int) (val * 0.434294481903252);
@@ -8396,10 +8398,13 @@ log_var(const NumericVar *base, const NumericVar *num, NumericVar *result)
83968398
static void
83978399
power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
83988400
{
8401+
int res_sign;
8402+
NumericVar abs_base;
83998403
NumericVar ln_base;
84008404
NumericVar ln_num;
84018405
int ln_dweight;
84028406
int rscale;
8407+
int sig_digits;
84038408
int local_rscale;
84048409
double val;
84058410

@@ -8439,9 +8444,40 @@ power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
84398444
return;
84408445
}
84418446

8447+
init_var(&abs_base);
84428448
init_var(&ln_base);
84438449
init_var(&ln_num);
84448450

8451+
/*
8452+
* If base is negative, insist that exp be an integer. The result is then
8453+
* positive if exp is even and negative if exp is odd.
8454+
*/
8455+
if (base->sign == NUMERIC_NEG)
8456+
{
8457+
/*
8458+
* Check that exp is an integer. This error code is defined by the
8459+
* SQL standard, and matches other errors in numeric_power().
8460+
*/
8461+
if (exp->ndigits > 0 && exp->ndigits > exp->weight + 1)
8462+
ereport(ERROR,
8463+
(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
8464+
errmsg("a negative number raised to a non-integer power yields a complex result")));
8465+
8466+
/* Test if exp is odd or even */
8467+
if (exp->ndigits > 0 && exp->ndigits == exp->weight + 1 &&
8468+
(exp->digits[exp->ndigits - 1] & 1))
8469+
res_sign = NUMERIC_NEG;
8470+
else
8471+
res_sign = NUMERIC_POS;
8472+
8473+
/* Then work with abs(base) below */
8474+
set_var_from_var(base, &abs_base);
8475+
abs_base.sign = NUMERIC_POS;
8476+
base = &abs_base;
8477+
}
8478+
else
8479+
res_sign = NUMERIC_POS;
8480+
84458481
/*----------
84468482
* Decide on the scale for the ln() calculation. For this we need an
84478483
* estimate of the weight of the result, which we obtain by doing an
@@ -8472,11 +8508,17 @@ power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
84728508

84738509
val = numericvar_to_double_no_overflow(&ln_num);
84748510

8475-
/* initial overflow test with fuzz factor */
8511+
/* initial overflow/underflow test with fuzz factor */
84768512
if (Abs(val) > NUMERIC_MAX_RESULT_SCALE * 3.01)
8477-
ereport(ERROR,
8478-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8479-
errmsg("value overflows numeric format")));
8513+
{
8514+
if (val > 0)
8515+
ereport(ERROR,
8516+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
8517+
errmsg("value overflows numeric format")));
8518+
zero_var(result);
8519+
result->dscale = NUMERIC_MAX_DISPLAY_SCALE;
8520+
return;
8521+
}
84808522

84818523
val *= 0.434294481903252; /* approximate decimal result weight */
84828524

@@ -8487,8 +8529,12 @@ power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
84878529
rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
84888530
rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
84898531

8532+
/* significant digits required in the result */
8533+
sig_digits = rscale + (int) val;
8534+
sig_digits = Max(sig_digits, 0);
8535+
84908536
/* set the scale for the real exp * ln(base) calculation */
8491-
local_rscale = rscale + (int) val - ln_dweight + 8;
8537+
local_rscale = sig_digits - ln_dweight + 8;
84928538
local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
84938539

84948540
/* and do the real calculation */
@@ -8499,8 +8545,12 @@ power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
84998545

85008546
exp_var(&ln_num, result, rscale);
85018547

8548+
if (res_sign == NUMERIC_NEG && result->ndigits > 0)
8549+
result->sign = NUMERIC_NEG;
8550+
85028551
free_var(&ln_num);
85038552
free_var(&ln_base);
8553+
free_var(&abs_base);
85048554
}
85058555

85068556
/*

src/test/regress/expected/numeric.out

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,12 @@ select 1.000000000123 ^ (-2147483648);
16351635
0.7678656556403084
16361636
(1 row)
16371637

1638+
select 0.9999999999 ^ 23300000000000 = 0 as rounds_to_zero;
1639+
rounds_to_zero
1640+
----------------
1641+
t
1642+
(1 row)
1643+
16381644
-- cases that used to error out
16391645
select 0.12 ^ (-25);
16401646
?column?
@@ -1648,6 +1654,43 @@ select 0.5678 ^ (-85);
16481654
782333637740774446257.7719390061997396
16491655
(1 row)
16501656

1657+
select 0.9999999999 ^ 70000000000000 = 0 as underflows;
1658+
underflows
1659+
------------
1660+
t
1661+
(1 row)
1662+
1663+
-- negative base to integer powers
1664+
select (-1.0) ^ 2147483646;
1665+
?column?
1666+
--------------------
1667+
1.0000000000000000
1668+
(1 row)
1669+
1670+
select (-1.0) ^ 2147483647;
1671+
?column?
1672+
---------------------
1673+
-1.0000000000000000
1674+
(1 row)
1675+
1676+
select (-1.0) ^ 2147483648;
1677+
?column?
1678+
--------------------
1679+
1.0000000000000000
1680+
(1 row)
1681+
1682+
select (-1.0) ^ 1000000000000000;
1683+
?column?
1684+
--------------------
1685+
1.0000000000000000
1686+
(1 row)
1687+
1688+
select (-1.0) ^ 1000000000000001;
1689+
?column?
1690+
---------------------
1691+
-1.0000000000000000
1692+
(1 row)
1693+
16511694
--
16521695
-- Tests for raising to non-integer powers
16531696
--
@@ -1766,6 +1809,18 @@ select exp(1.0::numeric(71,70));
17661809
2.7182818284590452353602874713526624977572470936999595749669676277240766
17671810
(1 row)
17681811

1812+
select exp(-5000::numeric) = 0 as rounds_to_zero;
1813+
rounds_to_zero
1814+
----------------
1815+
t
1816+
(1 row)
1817+
1818+
select exp(-10000::numeric) = 0 as underflows;
1819+
underflows
1820+
------------
1821+
t
1822+
(1 row)
1823+
17691824
-- cases that used to generate inaccurate results
17701825
select exp(32.999);
17711826
exp

src/test/regress/sql/numeric.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,19 @@ select 3.789 ^ 35;
899899
select 1.2 ^ 345;
900900
select 0.12 ^ (-20);
901901
select 1.000000000123 ^ (-2147483648);
902+
select 0.9999999999 ^ 23300000000000 = 0 as rounds_to_zero;
902903

903904
-- cases that used to error out
904905
select 0.12 ^ (-25);
905906
select 0.5678 ^ (-85);
907+
select 0.9999999999 ^ 70000000000000 = 0 as underflows;
908+
909+
-- negative base to integer powers
910+
select (-1.0) ^ 2147483646;
911+
select (-1.0) ^ 2147483647;
912+
select (-1.0) ^ 2147483648;
913+
select (-1.0) ^ 1000000000000000;
914+
select (-1.0) ^ 1000000000000001;
906915

907916
--
908917
-- Tests for raising to non-integer powers
@@ -942,6 +951,8 @@ select 1.234 ^ 5678;
942951
select exp(0.0);
943952
select exp(1.0);
944953
select exp(1.0::numeric(71,70));
954+
select exp(-5000::numeric) = 0 as rounds_to_zero;
955+
select exp(-10000::numeric) = 0 as underflows;
945956

946957
-- cases that used to generate inaccurate results
947958
select exp(32.999);

0 commit comments

Comments
 (0)