Skip to content

Commit 4a586bd

Browse files
committed
Add regression test for various power expressions with a zero base, and
adjust source code to be more modular.
1 parent 6b4e9d1 commit 4a586bd

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Copyright (c) 1998-2008, PostgreSQL Global Development Group
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.111 2008/05/08 19:25:38 momjian Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.112 2008/05/08 22:17:54 momjian Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -5170,21 +5170,6 @@ power_var(NumericVar *base, NumericVar *exp, NumericVar *result)
51705170
int local_rscale;
51715171
double val;
51725172

5173-
/*
5174-
* This avoids log(0) for cases of 0 raised to a non-integer.
5175-
* Also, while 0 ^ 0 can be either 1 or indeterminate (error), we
5176-
* treat it as one because most programming languages do this.
5177-
* http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_power
5178-
*/
5179-
if (cmp_var(base, &const_zero) == 0)
5180-
{
5181-
if (cmp_var(exp, &const_zero) == 0)
5182-
set_var_from_var(&const_one, result);
5183-
else
5184-
set_var_from_var(&const_zero, result);
5185-
return;
5186-
}
5187-
51885173
/* If exp can be represented as an integer, use power_var_int */
51895174
if (exp->ndigits == 0 || exp->ndigits <= exp->weight + 1)
51905175
{
@@ -5217,6 +5202,17 @@ power_var(NumericVar *base, NumericVar *exp, NumericVar *result)
52175202
free_var(&x);
52185203
}
52195204

5205+
/*
5206+
* This avoids log(0) for cases of 0 raised to a non-integer.
5207+
* 0 ^ 0 handled by power_var_int().
5208+
*/
5209+
if (cmp_var(base, &const_zero) == 0)
5210+
{
5211+
set_var_from_var(&const_zero, result);
5212+
result->dscale = NUMERIC_MIN_SIG_DIGITS; /* no need to round */
5213+
return;
5214+
}
5215+
52205216
init_var(&ln_base);
52215217
init_var(&ln_num);
52225218

@@ -5284,6 +5280,11 @@ power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
52845280
switch (exp)
52855281
{
52865282
case 0:
5283+
/*
5284+
* While 0 ^ 0 can be either 1 or indeterminate (error), we
5285+
* treat it as 1 because most programming languages do this.
5286+
* http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_power
5287+
*/
52875288
set_var_from_var(&const_one, result);
52885289
result->dscale = rscale; /* no need to round */
52895290
return;

src/test/regress/expected/float8.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ SELECT '' AS bad, f.f1 * '1e200' from FLOAT8_TBL f;
349349
ERROR: value out of range: overflow
350350
SELECT '' AS bad, f.f1 ^ '1e200' from FLOAT8_TBL f;
351351
ERROR: value out of range: overflow
352+
SELECT 0 ^ 0 + 0 ^ 1 + 0 ^ 0.0 + 0 ^ 0.5;
353+
?column?
354+
----------
355+
2
356+
(1 row)
357+
352358
SELECT '' AS bad, ln(f.f1) from FLOAT8_TBL f where f.f1 = '0.0' ;
353359
ERROR: cannot take logarithm of zero
354360
SELECT '' AS bad, ln(f.f1) from FLOAT8_TBL f where f.f1 < '0.0' ;

src/test/regress/sql/float8.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ SELECT '' AS bad, f.f1 * '1e200' from FLOAT8_TBL f;
129129

130130
SELECT '' AS bad, f.f1 ^ '1e200' from FLOAT8_TBL f;
131131

132+
SELECT 0 ^ 0 + 0 ^ 1 + 0 ^ 0.0 + 0 ^ 0.5;
133+
132134
SELECT '' AS bad, ln(f.f1) from FLOAT8_TBL f where f.f1 = '0.0' ;
133135

134136
SELECT '' AS bad, ln(f.f1) from FLOAT8_TBL f where f.f1 < '0.0' ;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy