Skip to content

Commit 6b4e9d1

Browse files
committed
Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0
return 1, rather than error. This was already the float8 behavior.
1 parent eabd1b2 commit 6b4e9d1

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 16 additions & 7 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.110 2008/04/21 00:26:45 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.111 2008/05/08 19:25:38 momjian Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -5170,6 +5170,21 @@ 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+
51735188
/* If exp can be represented as an integer, use power_var_int */
51745189
if (exp->ndigits == 0 || exp->ndigits <= exp->weight + 1)
51755190
{
@@ -5266,15 +5281,9 @@ power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
52665281
NumericVar base_prod;
52675282
int local_rscale;
52685283

5269-
/* Detect some special cases, particularly 0^0. */
5270-
52715284
switch (exp)
52725285
{
52735286
case 0:
5274-
if (base->ndigits == 0)
5275-
ereport(ERROR,
5276-
(errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
5277-
errmsg("zero raised to zero is undefined")));
52785287
set_var_from_var(&const_one, result);
52795288
result->dscale = rscale; /* no need to round */
52805289
return;

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