Re: Issues with factorial operator

From: "Dann Corbit" <DCorbit(at)connx(dot)com>
To: "Cui Shijun" <rancpine(at)gmail(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Issues with factorial operator
Date: 2007-06-09 06:54:04
Message-ID: D425483C2C5C9F49B5B7A41F89441547010006FD@postal.corporate.connx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> -----Original Message-----
> From: Cui Shijun [mailto:rancpine(at)gmail(dot)com]
> Sent: Friday, June 08, 2007 11:11 PM
> To: Dann Corbit
> Cc: Jim C. Nasby; pgsql-hackers(at)postgresql(dot)org
> Subject: Re: [HACKERS] Issues with factorial operator
>
> Hi,
>
> 2007/6/9, Dann Corbit <DCorbit(at)connx(dot)com>:
> > It makes sense with factorial function to do an error check on the
> > domain. Calculate beforehand, and figure out what the largest
sensible
> > domain value is.
>
> well, in fact what we need is to calculate log10(n!) first to see if
> the result will get exceeded.

#include <math.h>

double log10nfactorialestimate(unsigned n)
{
unsigned i;
double estimate = 0;
for (i = 1; i < n; i++)
estimate += log10(n);
return estimate;
}

#ifdef UNIT_TEST
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start,
end;
double answer;
start = clock();
end = clock();
answer = log10nfactorialestimate(92838278);
printf("log 10 of 92838278! is pretty close to %g and took %g
seconds\n",
answer, (end - start) / (1.0 * CLOCKS_PER_SEC));
return 0;
}
#endif
/*
C:\tmp>cl /W4 /Ox /DUNIT_TEST log10EST.C
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

log10EST.C
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

/out:log10EST.exe
log10EST.obj

C:\tmp>log10est
log 10 of 92838278! is pretty close to 7.3971e+008 and took 0 seconds
*/

> >
> > For instance, in Maple, I get this:
> > > y:=92838278!;
> > Error, object too large
> > >
> >
> > The error message returns instantly.
> >
> > For reasonably large values, it might make sense to pre-compute
> > factorials and store them in an array.
> >It should also be possible to
> > store 1/2 of Pascal's triangle in memory and demand load that memory
> > segment the first time someone asks for factorials or combinations
or
> > permutations.
>
> there may be too much memories to waste in that case... :-(

64 bit address space is coming. Are you ready for it?


> Regards
> CUI Shijun

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jonathan Gray 2007-06-09 07:08:54 GiST intarray rd-tree indexes using intbig
Previous Message Cui Shijun 2007-06-09 06:11:23 Re: Issues with factorial operator