Re: [HACKERS] bug in numeric_power() function - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [HACKERS] bug in numeric_power() function
Date
Msg-id 200805072319.m47NJ2226930@momjian.us
Whole thread Raw
In response to Re: [HACKERS] bug in numeric_power() function  (Alvaro Herrera <alvherre@commandprompt.com>)
Responses Re: [HACKERS] bug in numeric_power() function  (Alvaro Herrera <alvherre@commandprompt.com>)
Re: [HACKERS] bug in numeric_power() function  (Bruce Momjian <bruce@momjian.us>)
List pgsql-patches
Alvaro Herrera wrote:
> Bruce Momjian wrote:
> > Tom Lane wrote:
> > > Bruce Momjian <bruce@momjian.us> writes:
> > > > I have developed the attached patch which fixes 0 ^ 123.3.
> > >
> > > Did you actually read the wikipedia entry you cited?
>
> But that's about 0^0, not about 0^123.3.  See this other subsection:
>
> http://en.wikipedia.org/wiki/Exponentiation#Powers_of_zero
>
> 0^123.3 is 0, not 1.

Ah, got it, and I updated the patch to remove the commment about
"discrete".

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
Index: src/backend/utils/adt/numeric.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v
retrieving revision 1.110
diff -c -c -r1.110 numeric.c
*** src/backend/utils/adt/numeric.c    21 Apr 2008 00:26:45 -0000    1.110
--- src/backend/utils/adt/numeric.c    7 May 2008 23:18:31 -0000
***************
*** 5170,5175 ****
--- 5170,5190 ----
      int            local_rscale;
      double        val;

+     /*
+      *    This avoids log(0) for cases of 0 raised to a non-integer.
+      *    Also, while 0 ^ 0 can be either 1 or indeterminate (error), we
+      *    treat it as one because most programming languages do this.
+      *    http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_power
+      */
+     if (cmp_var(base, &const_zero) == 0)
+     {
+         if (cmp_var(exp, &const_zero) == 0)
+             set_var_from_var(&const_one, result);
+         else
+             set_var_from_var(&const_zero, result);
+         return;
+     }
+
      /* If exp can be represented as an integer, use power_var_int */
      if (exp->ndigits == 0 || exp->ndigits <= exp->weight + 1)
      {
***************
*** 5266,5280 ****
      NumericVar    base_prod;
      int            local_rscale;

-     /* Detect some special cases, particularly 0^0. */
-
      switch (exp)
      {
          case 0:
-             if (base->ndigits == 0)
-                 ereport(ERROR,
-                         (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
-                          errmsg("zero raised to zero is undefined")));
              set_var_from_var(&const_one, result);
              result->dscale = rscale;    /* no need to round */
              return;
--- 5281,5289 ----

pgsql-patches by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [HACKERS] bug in numeric_power() function
Next
From: Alvaro Herrera
Date:
Subject: Re: [HACKERS] bug in numeric_power() function