1 /* $OpenBSD: tgamma.c,v 1.2 2008/09/07 20:36:10 martynas Exp $ */ 2 3 /* Written by Martynas Venckus, 2008, Public domain. */ 4 5 #include <err.h> 6 #include <errno.h> 7 #include <math.h> 8 9 extern int errno; 10 11 #if defined(__vax__) 12 #define _IEEE 0 13 #else 14 #define _IEEE 1 15 #endif 16 17 double 18 infnan(int iarg) 19 { 20 switch (iarg) { 21 case ERANGE: 22 errno = ERANGE; 23 return (HUGE); 24 case -ERANGE: 25 errno = EDOM; 26 return (-HUGE); 27 default: 28 errno = EDOM; 29 return (0); 30 } 31 } 32 33 int 34 _isinf(double x) 35 { 36 if (_IEEE) { 37 return isinf(x); 38 } 39 else { 40 return errno == ERANGE; 41 } 42 } 43 44 int 45 _isnan(double x) 46 { 47 if (_IEEE) { 48 return isnan(x); 49 } 50 else { 51 return errno == ERANGE; 52 } 53 } 54 55 int 56 main(void) 57 { 58 double x; 59 60 /* Random values, approx. -177.79..171.63 */ 61 x = tgamma(11.0); /* (11 - 1)! */ 62 if (floor(x) != 3628800.0) 63 errx(1, "tgamma(11.0) = %f", x); 64 65 x = tgamma(3.5); /* 15/8 * sqrt(pi) */ 66 if (floor(x * 100) != 332.0) 67 errx(1, "tgamma(3.5) = %f", x); 68 69 x = tgamma(-0.5); /* -2 * sqrt(pi) */ 70 if (floor(x * 100) != -355.0) 71 errx(1, "tgamma(-0.5) = %f", x); 72 73 /* Special cases */ 74 x = tgamma(-1); /* Negative integers */ 75 if (!_isnan(x)) 76 errx(1, "tgamma(-1) = %f", x); 77 78 x = tgamma(-177.8); /* x ~< -177.79 */ 79 if (x != 0) 80 errx(1, "tgamma(-177.8) = %f", x); 81 82 x = tgamma(171.64); /* x ~> 171.63 */ 83 if (!_isinf(x)) 84 errx(1, "tgamma(171.64) = %f", x); 85 86 x = tgamma(0.0); 87 if (!_isinf(x) || x < 0) 88 errx(1, "tgamma(0) = %f", x); 89 90 x = tgamma(-0.0); 91 if (!_isinf(x) || x > 0) 92 errx(1, "tgamma(0) = %f", x); 93 94 x = tgamma(-HUGE_VAL); 95 if (!_isnan(x)) 96 errx(1, "tgamma(-HUGE_VAL) = %f", x); 97 98 x = tgamma(HUGE_VAL); 99 if (!_isinf(x)) 100 errx(1, "tgamma(HUGE_VAL) = %f", x); 101 102 #ifdef NAN 103 x = tgamma(NAN); 104 if (!_isnan(x)) 105 errx(1, "tgamma(NaN) = %f", x); 106 #endif 107 108 return 0; 109 } 110