summaryrefslogtreecommitdiff
path: root/regress/lib/libm/tgamma/tgamma.c
blob: 491fe6efcbb488bd90d8bd00796a4c5d02c30e23 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*	$OpenBSD: tgamma.c,v 1.2 2008/09/07 20:36:10 martynas Exp $	*/

/*	Written by Martynas Venckus, 2008,  Public domain.	*/

#include <err.h>
#include <errno.h>
#include <math.h>

extern int errno;

#if defined(__vax__)
#define _IEEE		0
#else
#define _IEEE		1
#endif

double
infnan(int iarg)
{
	switch (iarg) {
	case  ERANGE:
		errno = ERANGE;
		return (HUGE);
	case -ERANGE:
		errno = EDOM;
		return (-HUGE);
	default:
		errno = EDOM;
		return (0);
	}
}

int
_isinf(double x)
{
	if (_IEEE) {
		return isinf(x);
	}
	else {
		return errno == ERANGE;
	}
}

int
_isnan(double x)
{
	if (_IEEE) {
		return isnan(x);
	}
	else {
		return errno == ERANGE;
	}
}

int
main(void)
{
	double x;

	/* Random values, approx. -177.79..171.63 */
	x = tgamma(11.0);			/* (11 - 1)! */
	if (floor(x) != 3628800.0)
		errx(1, "tgamma(11.0) = %f", x);

	x = tgamma(3.5);			/* 15/8 * sqrt(pi) */
	if (floor(x * 100) != 332.0)
		errx(1, "tgamma(3.5) = %f", x);

	x = tgamma(-0.5);			/* -2 * sqrt(pi) */
	if (floor(x * 100) != -355.0)
		errx(1, "tgamma(-0.5) = %f", x);

	/* Special cases */
	x = tgamma(-1);				/* Negative integers */
	if (!_isnan(x))
		errx(1, "tgamma(-1) = %f", x);

	x = tgamma(-177.8);			/* x ~< -177.79 */
	if (x != 0)
		errx(1, "tgamma(-177.8) = %f", x);

	x = tgamma(171.64);			/* x ~> 171.63 */
	if (!_isinf(x))
		errx(1, "tgamma(171.64) = %f", x);

	x = tgamma(0.0);
	if (!_isinf(x) || x < 0)
		errx(1, "tgamma(0) = %f", x);

	x = tgamma(-0.0);
	if (!_isinf(x) || x > 0)
		errx(1, "tgamma(0) = %f", x);

	x = tgamma(-HUGE_VAL);
	if (!_isnan(x))
		errx(1, "tgamma(-HUGE_VAL) = %f", x);

	x = tgamma(HUGE_VAL);
	if (!_isinf(x))
		errx(1, "tgamma(HUGE_VAL) = %f", x);

#ifdef NAN
	x = tgamma(NAN);
	if (!_isnan(x))
		errx(1, "tgamma(NaN) = %f", x);
#endif

	return 0;
}