missing/tgamma.c

Go to the documentation of this file.
00001 /* tgamma.c  - public domain implementation of function tgamma(3m)
00002 
00003 reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
00004             (New Algorithm handbook in C language) (Gijyutsu hyouron
00005             sha, Tokyo, 1991) [in Japanese]
00006             http://oku.edu.mie-u.ac.jp/~okumura/algo/
00007 */
00008 
00009 /***********************************************************
00010     gamma.c -- Gamma function
00011 ***********************************************************/
00012 #include "ruby/config.h"
00013 #include <math.h>
00014 #include <errno.h>
00015 
00016 #ifdef HAVE_LGAMMA_R
00017 
00018 double tgamma(double x)
00019 {
00020     int sign;
00021     double d;
00022     if (x == 0.0) { /* Pole Error */
00023         errno = ERANGE;
00024         return 1/x < 0 ? -HUGE_VAL : HUGE_VAL;
00025     }
00026     if (x < 0) {
00027         static double zero = 0.0;
00028         double i, f;
00029         f = modf(-x, &i);
00030         if (f == 0.0) { /* Domain Error */
00031             errno = EDOM;
00032             return zero/zero;
00033         }
00034     }
00035     d = lgamma_r(x, &sign);
00036     return sign * exp(d);
00037 }
00038 
00039 #else
00040 
00041 #include <errno.h>
00042 #define PI      3.14159265358979324  /* $\pi$ */
00043 #define LOG_2PI 1.83787706640934548  /* $\log 2\pi$ */
00044 #define N       8
00045 
00046 #define B0  1                 /* Bernoulli numbers */
00047 #define B1  (-1.0 / 2.0)
00048 #define B2  ( 1.0 / 6.0)
00049 #define B4  (-1.0 / 30.0)
00050 #define B6  ( 1.0 / 42.0)
00051 #define B8  (-1.0 / 30.0)
00052 #define B10 ( 5.0 / 66.0)
00053 #define B12 (-691.0 / 2730.0)
00054 #define B14 ( 7.0 / 6.0)
00055 #define B16 (-3617.0 / 510.0)
00056 
00057 static double
00058 loggamma(double x)  /* the natural logarithm of the Gamma function. */
00059 {
00060     double v, w;
00061 
00062     v = 1;
00063     while (x < N) {  v *= x;  x++;  }
00064     w = 1 / (x * x);
00065     return ((((((((B16 / (16 * 15))  * w + (B14 / (14 * 13))) * w
00066                 + (B12 / (12 * 11))) * w + (B10 / (10 *  9))) * w
00067                 + (B8  / ( 8 *  7))) * w + (B6  / ( 6 *  5))) * w
00068                 + (B4  / ( 4 *  3))) * w + (B2  / ( 2 *  1))) / x
00069                 + 0.5 * LOG_2PI - log(v) - x + (x - 0.5) * log(x);
00070 }
00071 
00072 double tgamma(double x)  /* Gamma function */
00073 {
00074     if (x == 0.0) { /* Pole Error */
00075         errno = ERANGE;
00076         return 1/x < 0 ? -HUGE_VAL : HUGE_VAL;
00077     }
00078     if (x < 0) {
00079         int sign;
00080         static double zero = 0.0;
00081         double i, f;
00082         f = modf(-x, &i);
00083         if (f == 0.0) { /* Domain Error */
00084             errno = EDOM;
00085             return zero/zero;
00086         }
00087         sign = (fmod(i, 2.0) != 0.0) ? 1 : -1;
00088         return sign * PI / (sin(PI * f) * exp(loggamma(1 - x)));
00089     }
00090     return exp(loggamma(x));
00091 }
00092 #endif
00093 

Generated on Wed Aug 10 09:17:10 2011 for Ruby by  doxygen 1.4.7