Re: [HACKERS] bug in numeric_power() function

Lists: pgsql-hackerspgsql-patches
From: "Richard Wang" <ruc_wang(at)hotmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: bug in numeric_power() function
Date: 2008-03-12 02:37:34
Message-ID: fr7flt$2j04$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

I expected 0 ^ 123.3 to be 0, but it reported error as follows

postgres=# select 0 ^ 123.3;
ERROR: cannot take logarithm of zero

I find that there is a bug in numeric_power() function
the function caculates a ^ b based on the algorithm e ^ (lna * b)
as you see, ln0 is not valid


From: "Dann Corbit" <DCorbit(at)connx(dot)com>
To: "Richard Wang" <ruc_wang(at)hotmail(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: bug in numeric_power() function
Date: 2008-03-12 05:05:07
Message-ID: D425483C2C5C9F49B5B7A41F8944154701000CF1@postal.corporate.connx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

> -----Original Message-----
> From: pgsql-hackers-owner(at)postgresql(dot)org [mailto:pgsql-hackers-
> owner(at)postgresql(dot)org] On Behalf Of Richard Wang
> Sent: Tuesday, March 11, 2008 7:38 PM
> To: pgsql-hackers(at)postgresql(dot)org
> Subject: [HACKERS] bug in numeric_power() function
>
> I expected 0 ^ 123.3 to be 0, but it reported error as follows
>
> postgres=# select 0 ^ 123.3;
> ERROR: cannot take logarithm of zero
>
> I find that there is a bug in numeric_power() function
> the function caculates a ^ b based on the algorithm e ^ (lna * b)
> as you see, ln0 is not valid

It seems an obvious work-around that:
if (b == 0) return 1;
if (a == 0) return 0;
could be inserted at the top.

Aside:
Having the ^ operator overloaded for exponentiation surprises me.

Does it really have the right precedence for performing exponentiation?
(E.g. you cannot do this in C++ because ^ will have the precedence of
xor, which is wrong).


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Richard Wang" <ruc_wang(at)hotmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: bug in numeric_power() function
Date: 2008-03-12 05:16:32
Message-ID: 12538.1205298992@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

"Richard Wang" <ruc_wang(at)hotmail(dot)com> writes:
> I expected 0 ^ 123.3 to be 0, but it reported error as follows
> postgres=# select 0 ^ 123.3;
> ERROR: cannot take logarithm of zero

Hmm, seems like the numeric and float8 power operators don't agree
about 0^0 either...

regards, tom lane


From: "Richard Wang" <ruc_wang(at)hotmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: bug in numeric_power() function
Date: 2008-03-14 01:25:57
Message-ID: frck80$2rcv$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

I also have a question here:
isn't dpow enough for calculation?
Why added numeric_power?
"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> :12538(dot)1205298992(at)sss(dot)pgh(dot)pa(dot)us(dot)(dot)(dot)
> "Richard Wang" <ruc_wang(at)hotmail(dot)com> writes:
>> I expected 0 ^ 123.3 to be 0, but it reported error as follows
>> postgres=# select 0 ^ 123.3;
>> ERROR: cannot take logarithm of zero
>
> Hmm, seems like the numeric and float8 power operators don't agree
> about 0^0 either...
>
> regards, tom lane
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Richard Wang <ruc_wang(at)hotmail(dot)com>
Cc: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 20:12:24
Message-ID: 200805072012.m47KCOZ04930@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Richard Wang wrote:
> I expected 0 ^ 123.3 to be 0, but it reported error as follows
>
> postgres=# select 0 ^ 123.3;
> ERROR: cannot take logarithm of zero
>
> I find that there is a bug in numeric_power() function
> the function caculates a ^ b based on the algorithm e ^ (lna * b)
> as you see, ln0 is not valid

I have developed the attached patch which fixes 0 ^ 123.3. It also
fixes the case for 0 ^ 0.0 so it returns 1 instead of an error --- see
the C comment for why one is the proper return value. float pow()
already returned one in this case:

test=> select 0 ^ 0;
?column?
----------
1
(1 row)

test=> select 0 ^ 0.0;
?column?
----------
1
(1 row)

test=> select 0 ^ 3.4;
?column?
----------
1
(1 row)

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

Attachment Content-Type Size
/pgpatches/power text/x-diff 1.4 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Richard Wang <ruc_wang(at)hotmail(dot)com>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 21:43:52
Message-ID: 19085.1210196632@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian <bruce(at)momjian(dot)us> writes:
> I have developed the attached patch which fixes 0 ^ 123.3.

Did you actually read the wikipedia entry you cited?

regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-patches(at)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Bruce Momjian <bruce(at)momjian(dot)us>, Richard Wang <ruc_wang(at)hotmail(dot)com>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 22:22:03
Message-ID: 200805080022.04016.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > I have developed the attached patch which fixes 0 ^ 123.3.
>
> Did you actually read the wikipedia entry you cited?

Considering that 0::float8 ^ 0::float8 yields 1, making the numeric operator
do the same might not be completely unreasonable, but I find the rationale
that it is better for discrete mathematics fairly ludicrous on multiple
levels.


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Richard Wang <ruc_wang(at)hotmail(dot)com>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 22:22:05
Message-ID: 200805072222.m47MM5c04761@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > I have developed the attached patch which fixes 0 ^ 123.3.
>
> Did you actually read the wikipedia entry you cited?

Yes:

The evaluation of 0^0 presents a problem, because different mathematical
reasoning leads to different results. The best choice for its value
depends on the context. According to Benson (1999), "The choice whether
to define 00 is based on convenience, not on correctness."[2] There are
two principal treatments in practice, one from discrete mathematics and
the other from analysis.

...

The computer programming languages that evaluate 00 to be 1[8] include
J, Java, Python, Ruby, Haskell, ML, Scheme, MATLAB, bc, R programming
language, and Microsoft Windows' Calculator.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Richard Wang <ruc_wang(at)hotmail(dot)com>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 22:32:21
Message-ID: 20080507223221.GT20150@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian wrote:
> Tom Lane wrote:
> > Bruce Momjian <bruce(at)momjian(dot)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.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Richard Wang <ruc_wang(at)hotmail(dot)com>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 23:19:02
Message-ID: 200805072319.m47NJ2226930@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Alvaro Herrera wrote:
> Bruce Momjian wrote:
> > Tom Lane wrote:
> > > Bruce Momjian <bruce(at)momjian(dot)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(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

Attachment Content-Type Size
/pgpatches/power text/x-diff 1.5 KB

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Richard Wang <ruc_wang(at)hotmail(dot)com>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 23:32:29
Message-ID: 20080507233229.GV20150@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian wrote:

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

The page also says that 0^x is undefined when x is negative, not sure
about that one but I don't see it in your patch.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Richard Wang <ruc_wang(at)hotmail(dot)com>, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-07 23:41:53
Message-ID: 200805072341.m47NfrJ13270@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Alvaro Herrera wrote:
> Bruce Momjian wrote:
>
> > Ah, got it, and I updated the patch to remove the commment about
> > "discrete".
>
> The page also says that 0^x is undefined when x is negative, not sure
> about that one but I don't see it in your patch.

That one was already handled:

test=> select 0 ^ -1;
ERROR: invalid argument for power function
test=> select 0 ^ -1.0;
ERROR: invalid argument for power function

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Richard Wang <ruc_wang(at)hotmail(dot)com>
Subject: Re: [HACKERS] bug in numeric_power() function
Date: 2008-05-08 19:26:03
Message-ID: 200805081926.m48JQ3h05598@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


Applied.

---------------------------------------------------------------------------

Bruce Momjian wrote:
> Alvaro Herrera wrote:
> > Bruce Momjian wrote:
> > > Tom Lane wrote:
> > > > Bruce Momjian <bruce(at)momjian(dot)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(at)momjian(dot)us> http://momjian.us
> EnterpriseDB http://enterprisedb.com
>
> + If your life is a hard drive, Christ can be your backup. +

>
> --
> Sent via pgsql-patches mailing list (pgsql-patches(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-patches

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +