Unix Technical Forum

pgsql: Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^

This is a discussion on pgsql: Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ within the pgsql Committers forums, part of the PostgreSQL category; --> Log Message: ----------- Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Committers

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-10-2008, 02:05 PM
Bruce Momjian
 
Posts: n/a
Default pgsql: Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^

Log Message:
-----------
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.

Modified Files:
--------------
pgsql/src/backend/utils/adt:
numeric.c (r1.110 -> r1.111)
(http://anoncvs.postgresql.org/cvsweb...1.110&r2=1.111)

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-10-2008, 02:05 PM
Neil Conway
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, rather thanan error, and have 0 ^

On Thu, 2008-05-08 at 19:25 +0000, Bruce Momjian wrote:
> Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0
> return 1, rather than error.


A regression test for this behavior would be useful, I think.

-Neil



--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-10-2008, 02:05 PM
Bruce Momjian
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, ratherthan an error, and have 0 ^

Neil Conway wrote:
> On Thu, 2008-05-08 at 19:25 +0000, Bruce Momjian wrote:
> > Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0
> > return 1, rather than error.

>
> A regression test for this behavior would be useful, I think.


Done, plus I wasn't happy with the original patch so I redid it to be
more modular, also attached.

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

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


--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-10-2008, 02:05 PM
Tom Lane
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^

momjian@postgresql.org (Bruce Momjian) writes:
> Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0
> return 1, rather than error.


This is wrongly described, and the implementation is still not correct
either, because it should throw an error for negative exponents.
Would you please *read* that wikipedia page you keep citing?
http://en.wikipedia.org/wiki/Exponen...Powers_of_zero

regards, tom lane

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-10-2008, 02:05 PM
Bruce Momjian
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, ratherthan an error, and have 0 ^

Tom Lane wrote:
> momjian@postgresql.org (Bruce Momjian) writes:
> > Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0
> > return 1, rather than error.

>
> This is wrongly described, and the implementation is still not correct
> either, because it should throw an error for negative exponents.
> Would you please *read* that wikipedia page you keep citing?
> http://en.wikipedia.org/wiki/Exponen...Powers_of_zero


I think this is fixed in the version I just committed:

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

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

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

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-10-2008, 02:05 PM
Simon Riggs
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, rather thanan error, and have 0 ^

On Thu, 2008-05-08 at 18:34 -0400, Bruce Momjian wrote:
> Tom Lane wrote:
> > momjian@postgresql.org (Bruce Momjian) writes:
> > > Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^ 0.0
> > > return 1, rather than error.

> >
> > This is wrongly described, and the implementation is still not correct
> > either, because it should throw an error for negative exponents.
> > Would you please *read* that wikipedia page you keep citing?
> > http://en.wikipedia.org/wiki/Exponen...Powers_of_zero

>
> I think this is fixed in the version I just committed:
>
> test=> select 0 ^ (-1);
> ERROR: invalid argument for power function
> test=> select 0 ^ (-1.0);
> ERROR: invalid argument for power function


Hopefully this only occurs for 0 ^ (n)? A negative exponent isn't a
problem for y ^ x when y <> 0 and x < 0. Just checking you don't just
throw out an error for any negative exponent, which is what "invalid
argument" sounds like, to me.

Wikipedia says that exponentiation of zero to a negative power implies
division by zero, so shouldn't we throw a "division by zero" error?

--
Simon Riggs
2ndQuadrant http://www.2ndQuadrant.com


--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-10-2008, 02:05 PM
Tom Lane
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^

Simon Riggs <simon@2ndquadrant.com> writes:
> Wikipedia says that exponentiation of zero to a negative power implies
> division by zero, so shouldn't we throw a "division by zero" error?


I think it should be a specific message like "zero raised to a negative
power is undefined". It's not like it's going to take us any extra code
to know that we are faced with that case.

BTW, I realized that SQL:2003 spells it all out for us in explicit
detail:

12)If <power function> is specified, then let NVEB be the <numeric value
expression base>, then let VB be the value of NVEB, let NVEE be the
<numeric value expression exponent>, and let VE be the value of NVEE.

Case:

a) If either VB or VE is the null value, then the result is the null value.

b) If VB is 0 (zero) and VE is negative, then an exception condition is
raised: data exception Ñ invalid argument for power function.

c) If VB is 0 (zero) and VE is 0 (zero), then the result is 1 (one).

d) If VB is 0 (zero) and VE is positive, then the result is 0 (zero).

e) If VB is negative and VE is not equal to an exact numeric value with
scale 0 (zero), then an exception condition is raised: data exception
Ñ invalid argument for power function.

f) If VB is negative and VE is equal to an exact numeric value with
scale 0 (zero) that is an even number, then the result is the result of
EXP(NVEE*LN(-NVEB))

g) If VB is negative and VE is equal to an exact numeric value with
scale 0 (zero) that is an odd number, then the result is the result of
-EXP(NVEE*LN(-NVEB))

h) Otherwise, the result is the result of
EXP(NVEE*LN(NVEB))


regards, tom lane

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-10-2008, 02:05 PM
Bruce Momjian
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, ratherthan an error, and have 0 ^

Tom Lane wrote:
> Simon Riggs <simon@2ndquadrant.com> writes:
> > Wikipedia says that exponentiation of zero to a negative power implies
> > division by zero, so shouldn't we throw a "division by zero" error?

>
> I think it should be a specific message like "zero raised to a negative
> power is undefined". It's not like it's going to take us any extra code
> to know that we are faced with that case.
>
> BTW, I realized that SQL:2003 spells it all out for us in explicit
> detail:


....

> b) If VB is 0 (zero) and VE is negative, then an exception condition is
> raised: data exception Ñ invalid argument for power function.


Well, this indicates we shouldn't return "zero raised to a negative
power is undefined", but rather the power error we are giving now, or
are you saying we should return the "power" error code but an error
message mentioning zero?

> c) If VB is 0 (zero) and VE is 0 (zero), then the result is 1 (one).


I have updated the C comments to mention the spec also requires we
return 1 in this case.

C comment updated attached and applied.

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

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


--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-10-2008, 02:05 PM
Tom Lane
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, rather than an error, and have 0 ^

Bruce Momjian <bruce@momjian.us> writes:
>> b) If VB is 0 (zero) and VE is negative, then an exception condition is
>> raised: data exception Ñ invalid argument for power function.


> Well, this indicates we shouldn't return "zero raised to a negative
> power is undefined", but rather the power error we are giving now, or
> are you saying we should return the "power" error code but an error
> message mentioning zero?


The spec says what the SQLSTATE code should be. We have always felt
free to word the message text more specifically than that, though.

regards, tom lane

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-10-2008, 02:05 PM
Bruce Momjian
 
Posts: n/a
Default Re: pgsql: Have numeric 0 ^ 4.3 return 1, ratherthan an error, and have 0 ^

Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> writes:
> >> b) If VB is 0 (zero) and VE is negative, then an exception condition is
> >> raised: data exception Ñ invalid argument for power function.

>
> > Well, this indicates we shouldn't return "zero raised to a negative
> > power is undefined", but rather the power error we are giving now, or
> > are you saying we should return the "power" error code but an error
> > message mentioning zero?

>
> The spec says what the SQLSTATE code should be. We have always felt
> free to word the message text more specifically than that, though.


OK, I will work on it then.

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

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

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:35 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com