File: fact.c

package info (click to toggle)
mathomatic 16.0.5-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,192 kB
  • sloc: ansic: 22,029; makefile: 340; sh: 319; python: 96; awk: 39
file content (39 lines) | stat: -rw-r--r-- 836 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#if	0
#define _REENTRANT      1       /* Should be defined before including math.h for Mac OS X. */
				/* Do not use with iOS.  Disabled by default and unnecessary. */
#endif

#include <math.h>

/*
 * General factorial function in C for double precision floating point.
 * Uses the threadsafe lgamma_r(3) function, if _REENTRANT is defined.
 * Works for any floating point value.
 * Recommend using tgamma(3) (true gamma) function instead, if available.
 *
 * Link with -lm
 *
 * Returns (arg!) (same as gamma(arg+1)).
 * Sets "errno" external variable on overflow or error.
 */
double
factorial(double arg)
{
	double	d;

#if	USE_TGAMMA
	d = tgamma(arg + 1.0);
	return d;
#else
#if	_REENTRANT
	int	sign;

	d = exp(lgamma_r(arg + 1.0, &sign));
	return(d * sign);
#else
	d = exp(lgamma(arg + 1.0)) * signgam;
	return d;
#endif
#endif
}