In typeconv.sgml we have an example:

tgl=> select (4.3 !);
 ?column?
----------
       24
(1 row)

However, actually it does not work:

test=# select (4.3 !);
ERROR:  Unable to identify a postfix operator '!' for type 'double precision'
	You may need to add parentheses or an explicit cast

Shall we correct the doc or is that a bug?
--
Tatsuo Ishii

Mon, 10 Sep 2001 13:35:58 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
factorial doc bug?
On Mon, 10 Sep 2001, Tatsuo Ishii wrote:

> In typeconv.sgml we have an example:
> 
> tgl=> select (4.3 !);
>  ?column?
> ----------
>        24
> (1 row)

Mathematically speaking, one cannot find the factorial of such a
number. Users could easily cast/round a float to an integer - making it
suitable for such an operation.

I'd say it was a documentation issue.

Gavin


Mon, 10 Sep 2001 15:03:55 +1000 (EST)
Gavin Sherry <swm@linuxworld.com.au>
Re: factorial doc bug?
> On Mon, 10 Sep 2001, Tatsuo Ishii wrote:
> 
> > In typeconv.sgml we have an example:
> > 
> > tgl=> select (4.3 !);
> >  ?column?
> > ----------
> >        24
> > (1 row)
> 
> Mathematically speaking, one cannot find the factorial of such a
> number. Users could easily cast/round a float to an integer - making it
> suitable for such an operation.
> 
> I'd say it was a documentation issue.

My point is the docs claims that PostgreSQL automaticaly converts 4.3
to 4 in this case.

>This example illustrates an interesting result. Traditionally, the
>factorial operator is defined for integers only. The <productname>Postgres</productname>
>operator catalog has only one entry for factorial, taking an integer operand.
>If given a non-integer numeric argument, <productname>Postgres</productname>
>will try to convert that argument to an integer for evaluation of the
>factorial.
--
Tatsuo Ishii

Mon, 10 Sep 2001 14:12:06 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
Re: factorial doc bug?
> Shall we correct the doc or is that a bug?

Fix the docs...

                      - Thomas

Tue, 11 Sep 2001 16:14:03 +0000
Thomas Lockhart <lockhart@fourpalms.org>
Re: factorial doc bug?
> > Shall we correct the doc or is that a bug?
> 
> Fix the docs...

Are you saying we should remove the whole chapter below from the docs?
--
Tatsuo Ishii

>5.2.1.3. Factorial
>
>This example illustrates an interesting result. Traditionally, the factorial operator is defined for integers only. The Postgres operator catalog has only
>one entry for factorial, taking an integer operand. If given a non-integer numeric argument, Postgres will try to convert that argument to an integer
>for evaluation of the factorial. 
>
>tgl=> select (4.3 !);
> ?column?
>----------
>       24
>(1 row)
>
>      Note: Of course, this leads to a mathematically suspect result, since in principle the factorial of a non-integer is not defined. However,
>      the role of a database is not to teach mathematics, but to be a tool for data manipulation. If a user chooses to take the factorial of a
>      floating point number, Postgres will try to oblige.

Wed, 12 Sep 2001 09:59:35 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
Re: factorial doc bug?
> Are you saying we should remove the whole chapter below from the docs?

Hmm. I wrote that :/

I vaguely recall some discussion of this topic (a few months ago?). I'm
not certain that the current behavior was an intended result of changes
in the "automatic coersion" algorithms, but I think it was. Tom Lane is
probably the person who made those changes, and we should have him in
the discussion on whether the current behavior is appropriate. 

Keep in mind that he is a mathematician, and I'll guess that he won't
have much patience with folks who expect a result for a factorial of a
fractional number ;) But there may have been another case which made it
clearer that the old behavior was a bad road to take. We can look at the
archives, right?

                   - Thomas

Wed, 12 Sep 2001 01:35:33 +0000
Thomas Lockhart <lockhart@fourpalms.org>
Re: factorial doc bug?
> > Are you saying we should remove the whole chapter below from the docs?
> 
> Hmm. I wrote that :/
> 
> I vaguely recall some discussion of this topic (a few months ago?). I'm
> not certain that the current behavior was an intended result of changes
> in the "automatic coersion" algorithms, but I think it was. Tom Lane is
> probably the person who made those changes, and we should have him in
> the discussion on whether the current behavior is appropriate. 
> 
> Keep in mind that he is a mathematician, and I'll guess that he won't
> have much patience with folks who expect a result for a factorial of a
> fractional number ;) But there may have been another case which made it
> clearer that the old behavior was a bad road to take. We can look at the
> archives, right?

Ok, let's wait for him coming back...
--
Tatsuo Ishii

Wed, 12 Sep 2001 10:38:58 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
Re: factorial doc bug?
> Are you saying we should remove the whole chapter below from the docs?

Actually, it may be simply that we (now) implement factorial operators
for int8, int4, and int2. Not sure what previous releases implemented,
but perhaps it is just an issue of knowing which one should be used for
the operation. If before we only had, say, int4, then the coersion code
could easily assume that it was the correct coersion.

                      - Thomas

Wed, 12 Sep 2001 01:39:23 +0000
Thomas Lockhart <lockhart@fourpalms.org>
Re: factorial doc bug?
Thomas Lockhart writes:

> Keep in mind that he is a mathematician, and I'll guess that he won't
> have much patience with folks who expect a result for a factorial of a
> fractional number ;)

Real mathematicians will be perfectly happy with a factorial for a
fractional number, as long as it's properly and consistently defined. ;-)

Seriously, there is a well-established definition of factorials of
non-integral real numbers, but the current behaviour is probably the most
intuitive for the vast majority of users.

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter


Wed, 12 Sep 2001 14:45:10 +0200 (CEST)
Peter Eisentraut <peter_e@gmx.net>
Re: factorial doc bug?
On Wed, Sep 12, 2001 at 02:45:10PM +0200, Peter Eisentraut wrote:
> Thomas Lockhart writes:
> 
> > Keep in mind that he is a mathematician, and I'll guess that he won't
> > have much patience with folks who expect a result for a factorial of a
> > fractional number ;)
> 
> Real mathematicians will be perfectly happy with a factorial for a
> fractional number, as long as it's properly and consistently defined. ;-)
> 
> Seriously, there is a well-established definition of factorials of
> non-integral real numbers, but the current behaviour is probably the most
> intuitive for the vast majority of users.

I would be happy with with exp(lgamma(x+1)) as a synonym for x!
(So 4.3!=38.078 as far as I'm concerned :) )

Cheers,

Patrick

Wed, 12 Sep 2001 16:10:22 +0100
Patrick Welche <prlw1@newn.cam.ac.uk>
Re: factorial doc bug?
> On Wed, Sep 12, 2001 at 02:45:10PM +0200, Peter Eisentraut wrote:
> > Thomas Lockhart writes:
> > 
> > > Keep in mind that he is a mathematician, and I'll guess that he won't
> > > have much patience with folks who expect a result for a factorial of a
> > > fractional number ;)
> > 
> > Real mathematicians will be perfectly happy with a factorial for a
> > fractional number, as long as it's properly and consistently defined. ;-)
> > 
> > Seriously, there is a well-established definition of factorials of
> > non-integral real numbers, but the current behaviour is probably the most
> > intuitive for the vast majority of users.
> 
> I would be happy with with exp(lgamma(x+1)) as a synonym for x!
> (So 4.3!=38.078 as far as I'm concerned :) )

Yes, gamms is the standard for non-integer factorial but we don't
implement it that way.  :-)

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Wed, 12 Sep 2001 13:28:39 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: factorial doc bug?
Thomas Lockhart <lockhart@fourpalms.org> writes:
> Actually, it may be simply that we (now) implement factorial operators
> for int8, int4, and int2. Not sure what previous releases implemented,
> but perhaps it is just an issue of knowing which one should be used for
> the operation. If before we only had, say, int4, then the coersion code
> could easily assume that it was the correct coersion.

I think this must be the correct explanation.  Observe this experiment:

regression=# create operator !! (procedure = int4fac, leftarg = int4);
CREATE
regression=# select 4.3 !!;
 ?column?
----------
       24
(1 row)

regression=# create operator !! (procedure = int2fac, leftarg = int2);
CREATE
regression=# select 4.3 !!;
ERROR:  Unable to identify a postfix operator '!!' for type 'double precision'
        You may need to add parentheses or an explicit cast
regression=#

The int2 and int8 factorial operators were new in 7.0.  The example in
the docs is older --- its claim that there's only one factorial op in
the catalogs is clearly out of date.  So I'd say we should change the
example.  Have we got any other operators that only come in an int4
flavor, and are likely to stay that way?

			regards, tom lane

Sun, 16 Sep 2001 16:18:45 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: factorial doc bug? 
Thomas Lockhart <lockhart@fourpalms.org> writes:
> ... Tom Lane is
> probably the person who made those changes, and we should have him in
> the discussion on whether the current behavior is appropriate. 

> Keep in mind that he is a mathematician, and I'll guess that he won't
> have much patience with folks who expect a result for a factorial of a
> fractional number ;)

Actually, I'm an engineer by training, not a mathematician --- either
camp will tell you there's a big difference ;-)

I have no objection to adding a "float8 !" operator using the
gamma-based definition, if someone felt like doing it.  But even if we
did, that would not fix the example in typeconv.sgml; indeed it would
render the example completely wrong with respect to the point it was
originally written to make.  We need an operator that exists only for
int4 to demonstrate implicit coercion.  Unfortunately, I see no
candidate for one in the current catalogs.  Has anyone got another idea
about how to replace this example with a correct one?

			regards, tom lane

Sun, 16 Sep 2001 17:36:19 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: factorial doc bug? 