> -----Original Message-----
> From: Cui Shijun [mailto:rancpine@gmail.com]
> Sent: Friday, June 08, 2007 11:11 PM
> To: Dann Corbit
> Cc: Jim C. Nasby; pgsql-hackers@postgresql.org
> Subject: Re: [HACKERS] Issues with factorial operator
>
> Hi,
>
> 2007/6/9, Dann Corbit <DCorbit@connx.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