TEST(math, lgamma_r_17471883) { int sign; sign = 0; ASSERT_DOUBLE_EQ(HUGE_VAL, lgamma_r(0.0, &sign)); ASSERT_EQ(1, sign); sign = 0; ASSERT_DOUBLE_EQ(HUGE_VAL, lgamma_r(-0.0, &sign)); ASSERT_EQ(-1, sign); }
double tgamma(double x) /* Gamma function */ { int sign; if (x == 0.0) { /* Pole Error */ errno = ERANGE; return 1/x < 0 ? -HUGE_VAL : HUGE_VAL; } if (isinf(x)) { if (x < 0) goto domain_error; return x; } if (x < 0) { static double zero = 0.0; double i, f; f = modf(-x, &i); if (f == 0.0) { /* Domain Error */ domain_error: errno = EDOM; return zero/zero; } #ifndef HAVE_LGAMMA_R sign = (fmod(i, 2.0) != 0.0) ? 1 : -1; return sign * PI / (sin(PI * f) * exp(loggamma(1 - x))); #endif } #ifndef HAVE_LGAMMA_R return exp(loggamma(x)); #else x = lgamma_r(x, &sign); return sign * exp(x); #endif }
/* double tgamma(double x) * Return the Gamma function of x. */ double __ieee754_tgamma(double x) { int sign_of_gamma; int32_t hx; u_int32_t lx; /* We don't have a real gamma implementation now. We'll use lgamma and the exp function. But due to the required boundary conditions we must check some values separately. */ EXTRACT_WORDS(hx, lx, x); if (((hx & 0x7fffffff) | lx) == 0) { /* Return value for x == 0 is Inf with divide by zero exception. */ return 1.0 / x; } if (hx < 0 && (u_int32_t)hx < 0xfff00000 && rint(x) == x) { /* Return value for integer x < 0 is NaN with invalid exception. */ return (x - x) / (x - x); } if ((u_int32_t)hx == 0xfff00000 && lx == 0) { /* x == -Inf. According to ISO this is NaN. */ return x - x; } x = exp(lgamma_r(x, &sign_of_gamma)); return sign_of_gamma >= 0 ? x : -x; }
int main() { double x = 1.0; double y = 1.0; int i = 1; acosh(x); asinh(x); atanh(x); cbrt(x); expm1(x); erf(x); erfc(x); isnan(x); j0(x); j1(x); jn(i,x); ilogb(x); logb(x); log1p(x); rint(x); y0(x); y1(x); yn(i,x); # ifdef _THREAD_SAFE gamma_r(x,&i); lgamma_r(x,&i); # else gamma(x); lgamma(x); # endif hypot(x,y); nextafter(x,y); remainder(x,y); scalb(x,y); return 0; }
int main(void) { #pragma STDC FENV_ACCESS ON int yi; double y; float d; int e, i, err = 0; struct d_di *p; for (i = 0; i < sizeof t/sizeof *t; i++) { p = t + i; if (p->r < 0) continue; fesetround(p->r); feclearexcept(FE_ALL_EXCEPT); y = lgamma_r(p->x, &yi); e = fetestexcept(INEXACT|INVALID|DIVBYZERO|UNDERFLOW|OVERFLOW); if (!checkexcept(e, p->e, p->r)) { printf("%s:%d: bad fp exception: %s lgamma_r(%a)=%a,%lld, want %s", p->file, p->line, rstr(p->r), p->x, p->y, p->i, estr(p->e)); printf(" got %s\n", estr(e)); err++; } d = ulperr(y, p->y, p->dy); if (!checkulp(d, p->r) || (!isnan(p->x) && p->x!=-inf && !(p->e&DIVBYZERO) && yi != p->i)) { printf("%s:%d: %s lgamma_r(%a) want %a,%lld got %a,%d ulperr %.3f = %a + %a\n", p->file, p->line, rstr(p->r), p->x, p->y, p->i, y, yi, d, d-p->dy, p->dy); err++; } } return !!err; }
static inline double ruby_lgamma_r(const double d, int *sign) { const double g = lgamma_r(d, sign); if (isinf(g)) { if (d == 0.0 && signbit(d)) { *sign = -1; return INFINITY; } } return g; }
static VALUE math_lgamma(VALUE obj, VALUE x) { double d0, d; int sign; VALUE v; Need_Float(x); errno = 0; d0 = RFLOAT_VALUE(x); d = lgamma_r(d0, &sign); domain_check(d0, d, "lgamma"); v = DBL2NUM(d); return rb_assoc_new(v, INT2FIX(sign)); }
static VALUE math_lgamma(VALUE obj, VALUE x) { double d; int sign=1; VALUE v; d = Get_Double(x); /* check for domain error */ if (isinf(d)) { if (signbit(d)) domain_error("lgamma"); return rb_assoc_new(DBL2NUM(INFINITY), INT2FIX(1)); } v = DBL2NUM(lgamma_r(d, &sign)); return rb_assoc_new(v, INT2FIX(sign)); }
void test_fp_gamma( void ) { int s; #if __STDC_VERSION__ >= 199901L printf( "Testing C99 Gamma functions...\n" ); /* tgamma first */ VERIFY( CompDbl( tgamma( 1.0 ), 1.0 ) ); VERIFY( CompDbl( tgamma( 2.0 ), 1.0 ) ); VERIFY( CompDbl( tgamma( 4.0 ), 6.0 ) ); VERIFY( CompDbl( tgamma( 0.5 ), SQRTPI ) ); VERIFY( CompDbl( tgamma( -0.5 ), -2.0*SQRTPI ) ); VERIFY( isnan(tgamma( NAN )) ); VERIFY( tgamma( INFINITY ) == INFINITY ); VERIFY( isnan(tgamma( -INFINITY )) ); /* lgamma testing */ VERIFY( CompDbl( lgamma( 1.0 ), 0.0 ) ); VERIFY( signgam > 0 ); VERIFY( CompDbl( lgamma( 2.0 ), 0.0 ) ); VERIFY( signgam > 0 ); VERIFY( CompDbl( lgamma( -0.5 ), log( 2.0*SQRTPI ) ) ); VERIFY( signgam < 0 ); VERIFY( isnan(lgamma( NAN )) ); VERIFY( lgamma( INFINITY ) == INFINITY ); VERIFY( lgamma( -INFINITY ) == INFINITY ); /* lgamma_r testing */ VERIFY( CompDbl( lgamma_r( 1.0, &s ), 0.0 ) ); VERIFY( s > 0 ); VERIFY( CompDbl( lgamma_r( 2.0, &s ), 0.0 ) ); VERIFY( s > 0 ); VERIFY( CompDbl( lgamma_r( -0.5, &s ), log( 2.0*SQRTPI ) ) ); VERIFY( s < 0 ); #endif }
static VALUE math_lgamma(VALUE obj, VALUE x) { double d0, d; int sign=1; VALUE v; Need_Float(x); d0 = RFLOAT_VALUE(x); /* check for domain error */ if (isinf(d0)) { if (signbit(d0)) domain_error("lgamma"); return rb_assoc_new(DBL2NUM(INFINITY), INT2FIX(1)); } d = lgamma_r(d0, &sign); v = DBL2NUM(d); return rb_assoc_new(v, INT2FIX(sign)); }
static VALUE math_lgamma(VALUE unused_obj, VALUE x) { double d; int sign=1; VALUE v; d = Get_Double(x); /* check for domain error */ if (isinf(d)) { if (signbit(d)) domain_error("lgamma"); return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1)); } if (d == 0.0) { VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1); return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign); } v = DBL2NUM(lgamma_r(d, &sign)); return rb_assoc_new(v, INT2FIX(sign)); }
void builtin_lgamma_r(void) { const int n = 1024; float src[n]; // Setup kernel and buffers OCL_CREATE_KERNEL("builtin_lgamma_r"); OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(float), NULL); OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(float), NULL); OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(int), NULL); OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]); OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]); OCL_SET_ARG(2, sizeof(cl_mem), &buf[2]); globals[0] = n; locals[0] = 16; for (int j = 0; j < 1024; j++) { OCL_MAP_BUFFER(0); for (int i = 0; i < n; ++i) { src[i] = ((float*) buf_data[0])[i] = (j * n + i + 1) * 0.001f; } OCL_UNMAP_BUFFER(0); OCL_NDRANGE(1); OCL_MAP_BUFFER(1); OCL_MAP_BUFFER(2); float *dst = (float*) buf_data[1]; for (int i = 0; i < n; ++i) { int cpu_signp; float cpu = lgamma_r(src[i], &cpu_signp); int gpu_signp = ((int*)buf_data[2])[i]; float gpu = dst[i]; if (cpu_signp != gpu_signp || fabsf(cpu - gpu) >= 1e-3) { printf("%f %f %f\n", src[i], cpu, gpu); OCL_ASSERT(0); } } OCL_UNMAP_BUFFER(1); OCL_UNMAP_BUFFER(2); } }
static TACommandVerdict lgamma_r_cmd(TAThread thread,TAInputStream stream) { double x, res; int signp; x = readDouble(&stream); START_TARGET_OPERATION(thread); errno = 0; res = lgamma_r(x, &signp); END_TARGET_OPERATION(thread); writeInt(thread, errno); writeDouble(thread, res); writeInt(thread, signp); sendResponse(thread); return taDefaultVerdict; }
double gamma(double x) { return lgamma_r(x,&signgam); }
double attribute_hidden lgammal_r (double x, int *signgamp) { return lgamma_r (x, signgamp); }
TEST(math, lgammal_r) { int sign; ASSERT_DOUBLE_EQ(log(24.0L), lgamma_r(5.0L, &sign)); ASSERT_EQ(1, sign); }
double lgamma2 ( double x) { int s; return lgamma_r ( x, &s); }
_WMRTLINK double lgamma(double x) { return lgamma_r(x, &signgam); }
TEST(math, lgamma_r) { int sign; ASSERT_FLOAT_EQ(log(24.0), lgamma_r(5.0, &sign)); ASSERT_EQ(1, sign); }
extern double _swift_Darwin_lgamma_r(double x, int *psigngam) { return lgamma_r(x, psigngam); }