This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: PATCH: BZ 552: Wrong return values for tgamma (0) and tgamma (-0)
On Fri, Nov 19, 2004 at 10:31:37AM +0100, Jakub Jelinek wrote:
> On Tue, Nov 16, 2004 at 05:54:50PM -0800, H. J. Lu wrote:
> > + case 50:
> > + case 150:
> > + case 250:
> > + /* tgamma(|x|==0) */
> > +#ifdef FE_INVALID
> > + feclearexcept (FE_INVALID);
> > +#endif
> > + exc.type = SING;
> > + exc.name = type < 100 ? "tgamma" : (type < 200
> > + ? "tgammaf" : "tgammal");
> > + exc.retval = 1.0/x;
>
> This doesn't sound right to me.
> If tgamma (+-0) is supposed to raise divide by zero exception, then
> raising invalid exception, then clearing it and raising divide by
> zero sounds wrong.
> I'd say the right fix is to change sysdeps/ieee754/*/e_gamma*_r.c
> to:
Here is the updated patch.
Thanks.
H.J.
---
2004-11-19 H.J. Lu <hongjiu.lu@intel.com>
[BZ #552]
* math/libm-test.inc (tgamma_test): Update tgamma (0) and
tgamma (-0).
* sysdeps/generic/w_tgamma.c (__tgamma): Properly handle
|x| == 0.
* sysdeps/generic/w_tgammaf.c (__tgammaf): Likewise.
* sysdeps/generic/w_tgammal.c (__tgammal): Likewise.
* sysdeps/ieee754/dbl-64/e_gamma_r.c (__ieee754_gamma_r): Likewise.
* sysdeps/ieee754/flt-32/e_gammaf_r.c: Likewise.
* sysdeps/ieee754/ldbl-128/e_gammal_r.c: Likewise.
* sysdeps/ieee754/ldbl-96/e_gammal_r.c: Likewise.
* sysdeps/ieee754/k_standard.c (__kernel_standard): Handle
tgamma (0) and tgamma (-0).
--- libc/math/libm-test.inc.tgamma 2004-09-27 11:54:13.000000000 -0700
+++ libc/math/libm-test.inc 2004-11-17 08:45:44.000000000 -0800
@@ -4132,8 +4132,8 @@ tgamma_test (void)
START (tgamma);
TEST_f_f (tgamma, plus_infty, plus_infty);
- TEST_f_f (tgamma, 0, nan_value, INVALID_EXCEPTION);
- TEST_f_f (tgamma, minus_zero, nan_value, INVALID_EXCEPTION);
+ TEST_f_f (tgamma, 0, plus_infty, DIVIDE_BY_ZERO_EXCEPTION);
+ TEST_f_f (tgamma, minus_zero, minus_infty, DIVIDE_BY_ZERO_EXCEPTION);
/* tgamma (x) == NaN plus invalid exception for integer x <= 0. */
TEST_f_f (tgamma, -2, nan_value, INVALID_EXCEPTION);
TEST_f_f (tgamma, minus_infty, nan_value, INVALID_EXCEPTION);
--- libc/sysdeps/generic/w_tgamma.c.tgamma 2001-02-16 17:45:34.000000000 -0800
+++ libc/sysdeps/generic/w_tgamma.c 2004-11-17 08:45:44.000000000 -0800
@@ -39,8 +39,10 @@ static char rcsid[] = "$NetBSD: w_gamma.
if(_LIB_VERSION == _IEEE_) return y;
if(!__finite(y)&&__finite(x)) {
- if(__floor(x)==x&&x<=0.0)
- return __kernel_standard(x,x,41); /* tgamma pole */
+ if (x == 0.0)
+ return __kernel_standard(x,x,50); /* tgamma pole */
+ else if(__floor(x)==x&&x<0.0)
+ return __kernel_standard(x,x,41); /* tgamma domain */
else
return __kernel_standard(x,x,40); /* tgamma overflow */
}
--- libc/sysdeps/generic/w_tgammaf.c.tgamma 2001-02-16 17:45:39.000000000 -0800
+++ libc/sysdeps/generic/w_tgammaf.c 2004-11-17 08:45:44.000000000 -0800
@@ -37,8 +37,11 @@ static char rcsid[] = "$NetBSD: w_gammaf
if(_LIB_VERSION == _IEEE_) return y;
if(!__finitef(y)&&__finitef(x)) {
- if(__floorf(x)==x&&x<=(float)0.0)
+ if (x == (float)0.0)
/* tgammaf pole */
+ return (float)__kernel_standard((double)x,(double)x,150);
+ else if(__floorf(x)==x&&x<(float)0.0)
+ /* tgammaf domain */
return (float)__kernel_standard((double)x,(double)x,141);
else
/* tgammaf overflow */
--- libc/sysdeps/generic/w_tgammal.c.tgamma 2001-02-16 17:45:44.000000000 -0800
+++ libc/sysdeps/generic/w_tgammal.c 2004-11-17 08:45:44.000000000 -0800
@@ -42,8 +42,10 @@ static char rcsid[] = "$NetBSD: $";
if(_LIB_VERSION == _IEEE_) return y;
if(!__finitel(y)&&__finitel(x)) {
- if(__floorl(x)==x&&x<=0.0)
- return __kernel_standard(x,x,241); /* tgamma pole */
+ if(x==0.0)
+ return __kernel_standard(x,x,250); /* tgamma pole */
+ else if(__floorl(x)==x&&x<0.0)
+ return __kernel_standard(x,x,241); /* tgamma domain */
else
return __kernel_standard(x,x,240); /* tgamma overflow */
}
--- libc/sysdeps/ieee754/dbl-64/e_gamma_r.c.tgamma 2001-07-05 21:55:55.000000000 -0700
+++ libc/sysdeps/ieee754/dbl-64/e_gamma_r.c 2004-11-19 07:52:47.296379870 -0800
@@ -1,5 +1,5 @@
/* Implementation of gamma function according to ISO C.
- Copyright (C) 1997, 1999, 2001 Free Software Foundation, Inc.
+ Copyright (C) 1997, 1999, 2001, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@@ -35,9 +35,9 @@ __ieee754_gamma_r (double x, int *signga
if (((hx & 0x7fffffff) | lx) == 0)
{
- /* Return value for x == 0 is NaN with invalid exception. */
+ /* Return value for x == 0 is Inf with divide by zero exception. */
*signgamp = 0;
- return x / x;
+ return 1.0 / x;
}
if (hx < 0 && (u_int32_t) hx < 0xfff00000 && __rint (x) == x)
{
--- libc/sysdeps/ieee754/flt-32/e_gammaf_r.c.tgamma 2001-07-05 21:55:55.000000000 -0700
+++ libc/sysdeps/ieee754/flt-32/e_gammaf_r.c 2004-11-19 07:53:09.898452892 -0800
@@ -1,5 +1,5 @@
/* Implementation of gamma function according to ISO C.
- Copyright (C) 1997, 1999, 2001 Free Software Foundation, Inc.
+ Copyright (C) 1997, 1999, 2001, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@@ -34,9 +34,9 @@ __ieee754_gammaf_r (float x, int *signga
if ((hx & 0x7fffffff) == 0)
{
- /* Return value for x == 0 is NaN with invalid exception. */
+ /* Return value for x == 0 is Inf with divide by zero exception. */
*signgamp = 0;
- return x / x;
+ return 1.0 / x;
}
if (hx < 0 && (u_int32_t) hx < 0xff800000 && __rintf (x) == x)
{
--- libc/sysdeps/ieee754/k_standard.c.tgamma 2001-06-06 05:46:35.000000000 -0700
+++ libc/sysdeps/ieee754/k_standard.c 2004-11-19 07:51:02.289978242 -0800
@@ -832,7 +832,7 @@ static double zero = 0.0; /* used as con
case 40:
case 140:
case 240:
- /* gamma(finite) overflow */
+ /* tgamma(finite) overflow */
exc.type = OVERFLOW;
exc.name = type < 100 ? "tgamma" : (type < 200
? "tgammaf" : "tgammal");
@@ -846,7 +846,7 @@ static double zero = 0.0; /* used as con
case 41:
case 141:
case 241:
- /* gamma(-integer) or gamma(0) */
+ /* tgamma(-integer) */
exc.type = SING;
exc.name = type < 100 ? "tgamma" : (type < 200
? "tgammaf" : "tgammal");
@@ -973,8 +973,26 @@ static double zero = 0.0; /* used as con
__set_errno (EDOM);
}
break;
+ case 50:
+ case 150:
+ case 250:
+ /* tgamma(|x|==0) */
+ exc.type = SING;
+ exc.name = type < 100 ? "tgamma" : (type < 200
+ ? "tgammaf" : "tgammal");
+ exc.retval = 1.0/x;
+ if (_LIB_VERSION == _POSIX_)
+ __set_errno (ERANGE);
+ else if (!matherr(&exc)) {
+ if (_LIB_VERSION == _SVID_) {
+ (void) WRITE2("tgamma: SING error\n", 18);
+ exc.retval = 1.0/x;
+ }
+ __set_errno (ERANGE);
+ }
+ break;
- /* #### Last used is 49/149/249 ### */
+ /* #### Last used is 50/150/250 ### */
}
return exc.retval;
}
--- libc/sysdeps/ieee754/ldbl-128/e_gammal_r.c.tgamma 2002-07-10 20:12:07.000000000 -0700
+++ libc/sysdeps/ieee754/ldbl-128/e_gammal_r.c 2004-11-19 07:53:38.121797957 -0800
@@ -1,5 +1,5 @@
/* Implementation of gamma function according to ISO C.
- Copyright (C) 1997, 1999, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1997, 1999, 2002, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
Jakub Jelinek <jj@ultra.linux.cz, 1999.
@@ -36,9 +36,9 @@ __ieee754_gammal_r (long double x, int *
if (((hx & 0x7fffffffffffffffLL) | lx) == 0)
{
- /* Return value for x == 0 is NaN with invalid exception. */
+ /* Return value for x == 0 is Inf with divide by zero exception. */
*signgamp = 0;
- return x / x;
+ return 1.0 / x;
}
if (hx < 0 && (u_int64_t) hx < 0xffff000000000000ULL && __rintl (x) == x)
{
--- libc/sysdeps/ieee754/ldbl-96/e_gammal_r.c.tgamma 2003-04-07 08:09:36.000000000 -0700
+++ libc/sysdeps/ieee754/ldbl-96/e_gammal_r.c 2004-11-19 07:54:15.695932092 -0800
@@ -1,5 +1,5 @@
/* Implementation of gamma function according to ISO C.
- Copyright (C) 1997, 1999, 2001, 2003 Free Software Foundation, Inc.
+ Copyright (C) 1997, 1999, 2001, 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@@ -34,9 +34,9 @@ __ieee754_gammal_r (long double x, int *
if (((es & 0x7fff) | hx | lx) == 0)
{
- /* Return value for x == 0 is NaN with invalid exception. */
+ /* Return value for x == 0 is Inf with divide by zero exception. */
*signgamp = 0;
- return x / x;
+ return 1.0 / x;
}
if (es == 0xffffffff && ((hx & 0x7fffffff) | lx) == 0)
{