From: cowen <cowen@attbi.com>
To: gcc-help@gcc.gnu.org
Subject: simple question
Date: Wed, 27 Nov 2002 10:11:00 -0000 [thread overview]
Message-ID: <5.1.1.6.0.20021127101045.00a80550@mail.attbi.com> (raw)
Hello all.
This is perhaps a stupid newbie question. I'm implementing gammln, one of
the numerical recipes
(available from
http://www.ulib.org/webRoot/Books/Numerical_Recipes/bookcpdf.html, chapter 6).
gammln (or lnGamma as I've named it) computes the natural log of the gamma
function, which
should allow one to compute interesting things, like factorials.
Here's the code:
***************** utils.h **************************************
#ifndef UTILS_H
#define UTILS_H
double lnGamma ( double xx );
#endif // UTILS_H
**************** utils.c **************************************
#include <math.h>
double lnGamma ( double xx )
{
int j;
double x,y,tmp,ser;
static double cof[6] = {
76.18009172947146,
-86.50532032941677,
24.01409824083091,
-1.231739572450155,
0.1208650973866179e-2,
-0.5395239384953e-5
};
y = x = xx;
tmp = x + 5.5;
tmp -= ( x + 0.5 ) * log( tmp );
ser = 1.000000000190015;
for ( j = 0; j <= 5; j++ )
ser += cof[j]/++y;
return -tmp + log( 2.5066282746310005 * ser/x );
}
**************************** test.c
*******************************************
#include <stdio.h>
#include <math.h>
#include "utils.h"
int main ()
{
int i;
for ( i = 1; i <= 20; i++ ) {
printf( "%3d!: %.0f\n", i, exp( lnGamma( i + 1.0 ) ) );
}
return 0;
}
*************************************************************************************
Here's the output:
1!: 1
2!: 2
3!: 6
4!: 24
5!: 120
6!: 720
7!: 5040
8!: 40320
9!: 362880
10!: 3628800
11!: 39916800
12!: 479001600
13!: 6227020800
14!: 87178291200
15!: 1307674368006
16!: 20922789888126
17!: 355687428098601
18!: 6402373705783494
19!: 121645100410059440
20!: 2432902008204834800
**********************************************************
This is obviously wrong. (15! is evidence enough.)
The algorithm, however, is correct -- I lifted it
directly from the numerical recipe, and I'm fairly confident that
the authors have not made an error. I've built and run the example
on different platforms. The result is always the same.
Here is a primitive Makefile:
all: utils.o test.o test.exe
%.o: %.c
gcc -c -g -Wall -o $@ $^
test.exe:
gcc -o $@ -g -Wall utils.o test.o
Any suggestions would be most helpful.
Thanks in advance,
Charles.
next reply other threads:[~2002-11-27 18:11 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-11-27 10:11 cowen [this message]
2002-11-27 10:19 ` simple question [OT] Eljay Love-Jensen
-- strict thread matches above, loose matches on Subject: below --
2012-08-22 13:58 Simple question Byron Blue
2012-08-22 14:03 ` Ángel González
2012-08-22 14:35 ` Ian Lance Taylor
2012-08-22 15:21 ` David Brown
2012-08-22 16:39 ` Bob Plantz
2012-08-25 15:57 ` Florian Weimer
2012-08-25 16:05 ` Georg-Johann Lay
2012-08-25 17:30 ` Ian Lance Taylor
2012-08-25 17:55 ` Bob Plantz
2012-08-25 18:20 ` Georg-Johann Lay
2012-08-25 20:37 ` Ian Lance Taylor
2012-08-25 21:01 ` Georg-Johann Lay
2012-08-25 21:23 ` Ian Lance Taylor
2012-08-26 9:13 ` Ángel González
2012-08-26 9:55 ` Jonathan Wakely
2004-04-16 19:26 simple question lrtaylor
2004-04-16 19:19 Ramin NIkaeen
2004-04-16 16:40 lrtaylor
2004-04-15 21:43 Ramin NIkaeen
2004-04-15 21:47 ` Gabriel Dos Reis
2004-04-15 21:47 ` Andreas Tobler
2004-04-15 21:48 ` Paolo Carlini
2004-04-16 9:06 ` Houda Benabderrazik
2000-08-08 10:46 RPathiyal
2000-08-08 18:18 ` Alexandre Oliva
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5.1.1.6.0.20021127101045.00a80550@mail.attbi.com \
--to=cowen@attbi.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).