gamma.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <math.h>
  2. #include <float.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #define check_d1(func, param, expected) \
  7. do { \
  8. int err; hex_union ur; hex_union up; \
  9. double result = func(param); up.f = param; ur.f = result; \
  10. errors += (err = (result != (expected))); \
  11. err \
  12. ? printf("FAIL: %s(%g/"HEXFMT")=%g/"HEXFMT" (expected %g)\n", \
  13. #func, (double)(param), (long long)up.hex, result, (long long)ur.hex, (double)(expected)) \
  14. : printf("PASS: %s(%g)=%g\n", #func, (double)(param), result); \
  15. } while (0)
  16. #define HEXFMT "%08llx"
  17. typedef union {
  18. double f;
  19. uint64_t hex;
  20. } hex_union;
  21. double result;
  22. #define M_2_SQRT_PIl 3.5449077018110320545963349666822903L /* 2 sqrt (M_PIl) */
  23. #define M_SQRT_PIl 1.7724538509055160272981674833411451L /* sqrt (M_PIl) */
  24. double zero = 0.0;
  25. double minus_zero = 0.0;
  26. double nan_value = 0.0;
  27. int errors = 0;
  28. int main(void)
  29. {
  30. nan_value /= nan_value;
  31. minus_zero = copysign(zero, -1.0);
  32. //check_d1(tgamma, HUGE_VAL, NAN);
  33. //check_d1(tgamma, negative_integer, NAN);
  34. check_d1(tgamma, 0.0, HUGE_VAL); /* pole */
  35. check_d1(tgamma, minus_zero, -HUGE_VAL); /* pole */
  36. check_d1(tgamma, DBL_MAX/2, HUGE_VAL); /* overflow to inf */
  37. check_d1(tgamma, DBL_MAX, HUGE_VAL); /* overflow to inf */
  38. check_d1(tgamma, HUGE_VAL, HUGE_VAL); /* overflow to inf */
  39. check_d1(tgamma, 7, 2*3*4*5*6); /* normal value */
  40. check_d1(tgamma, -0.5, -M_2_SQRT_PIl); /* normal value (testing negative points) */
  41. check_d1(lgamma, -HUGE_VAL, HUGE_VAL);
  42. //check_d1(lgamma, HUGE_VAL, NAN);
  43. check_d1(lgamma, 0.0, HUGE_VAL); /* pole */
  44. check_d1(lgamma, minus_zero, HUGE_VAL); /* pole */
  45. check_d1(lgamma, 1.0, 0.0);
  46. check_d1(lgamma, 2.0, 0.0);
  47. check_d1(lgamma, DBL_MAX/2, HUGE_VAL); /* overflow to inf */
  48. check_d1(lgamma, DBL_MAX, HUGE_VAL); /* overflow to inf */
  49. check_d1(lgamma, HUGE_VAL, HUGE_VAL); /* overflow to inf */
  50. check_d1(lgamma, 7, log(2*3*4*5*6)); /* normal value */
  51. /* In glibc, gamma == lgamma. (In BSD, it's == tgamma */
  52. check_d1(gamma, -HUGE_VAL, HUGE_VAL);
  53. //check_d1(gamma, HUGE_VAL, NAN);
  54. check_d1(gamma, 0.0, HUGE_VAL); /* pole */
  55. check_d1(gamma, minus_zero, HUGE_VAL); /* pole */
  56. check_d1(gamma, 1.0, 0.0);
  57. check_d1(gamma, 2.0, 0.0);
  58. check_d1(gamma, DBL_MAX/2, HUGE_VAL); /* overflow to inf */
  59. check_d1(gamma, DBL_MAX, HUGE_VAL); /* overflow to inf */
  60. check_d1(gamma, HUGE_VAL, HUGE_VAL); /* overflow to inf */
  61. check_d1(gamma, 7, log(2*3*4*5*6)); /* normal value */
  62. printf("Errors: %d\n", errors);
  63. return errors;
  64. }