6#include "ruby/missing.h"
10static double q_gamma(
double,
double,
double);
14static double p_gamma(
double a,
double x,
double loggamma_a)
17 double result, term, previous;
19 if (x >= 1 + a)
return 1 - q_gamma(a, x, loggamma_a);
21 result = term = exp(a * log(x) - x - loggamma_a) / a;
22 for (k = 1; k < 1000; k++) {
24 previous = result; result += term;
25 if (result == previous)
return result;
27 fprintf(stderr,
"erf.c:%d:p_gamma() could not converge.", __LINE__);
33static double q_gamma(
double a,
double x,
double loggamma_a)
36 double result, w, temp, previous;
37 double la = 1, lb = 1 + x - a;
39 if (x < 1 + a)
return 1 - p_gamma(a, x, loggamma_a);
40 w = exp(a * log(x) - x - loggamma_a);
42 for (k = 2; k < 1000; k++) {
43 temp = ((k - 1 - a) * (lb - la) + (k + x) * lb) / k;
47 previous = result; result += temp;
48 if (result == previous)
return result;
50 fprintf(stderr,
"erf.c:%d:q_gamma() could not converge.", __LINE__);
54#define LOG_PI_OVER_2 0.572364942924700087071713675675
59 if (isnan(x))
return x;
60 return (x>0 ? 1.0 : -1.0);
62 if (x >= 0)
return p_gamma(0.5, x * x, LOG_PI_OVER_2);
63 else return - p_gamma(0.5, x * x, LOG_PI_OVER_2);
69 if (isnan(x))
return x;
70 return (x>0 ? 0.0 : 2.0);
72 if (x >= 0)
return q_gamma(0.5, x * x, LOG_PI_OVER_2);
73 else return 1 + p_gamma(0.5, x * x, LOG_PI_OVER_2);