Thread: factorial doc bug?

factorial doc bug?

From
Tatsuo Ishii
Date:
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
explicitcast
 

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


Re: factorial doc bug?

From
Gavin Sherry
Date:
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



Re: factorial doc bug?

From
Tatsuo Ishii
Date:
> 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


Re: factorial doc bug?

From
Thomas Lockhart
Date:
> Shall we correct the doc or is that a bug?

Fix the docs...
                     - Thomas


Re: factorial doc bug?

From
Tatsuo Ishii
Date:
> > 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.
ThePostgres operator catalog has only
 
>one entry for factorial, taking an integer operand. If given a non-integer numeric argument, Postgres will try to
convertthat 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-integeris 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
takethe factorial of a
 
>      floating point number, Postgres will try to oblige.


Re: factorial doc bug?

From
Tatsuo Ishii
Date:
> > 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


Re: factorial doc bug?

From
Thomas Lockhart
Date:
> 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


Re: factorial doc bug?

From
Thomas Lockhart
Date:
> 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


Re: factorial doc bug?

From
Peter Eisentraut
Date:
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



Re: factorial doc bug?

From
Patrick Welche
Date:
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


Re: factorial doc bug?

From
Bruce Momjian
Date:
> 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,
Pennsylvania19026
 


Re: factorial doc bug?

From
Tom Lane
Date:
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
anexplicit 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


Re: factorial doc bug?

From
Tom Lane
Date:
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