summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2009-12-07 13:23:25 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2009-12-07 13:23:25 +0000
commite69c2fb020cd3d27b097904fd5cbe6ce949e7623 (patch)
treee686423d7378de392758183ae3b6b0bf67559765
parent88520ac4482da223d3493f8c5de61bc0750afb72 (diff)
downloadmpfr-e69c2fb020cd3d27b097904fd5cbe6ce949e7623.tar.gz
sin_cos.c, tsin_cos.c: ported patch and tests from the trunk.
More precisely: * tsin_cos.c: more testing, in particular in the underflow case. * sin_cos.c: in case of tiny inputs, keep the flags. In practice, this fixes the following bug in mpfr_sin_cos (shown by the improved tsin_cos.c): if emin is the minimum exponent (MPFR_EMIN_MIN), the absolute value of the input is the minimum positive number and the rounding mode is toward 0 (or equivalent), then the underflow flag is dropped. The other exception cases (e.g., in case of reduced exponent range) are handled by mpfr_check_range(). git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/2.4@6629 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--sin_cos.c34
-rw-r--r--tests/tsin_cos.c56
2 files changed, 66 insertions, 24 deletions
diff --git a/sin_cos.c b/sin_cos.c
index ab3ea41b7..d4ec34989 100644
--- a/sin_cos.c
+++ b/sin_cos.c
@@ -82,17 +82,19 @@ mpfr_sin_cos (mpfr_ptr y, mpfr_ptr z, mpfr_srcptr x, mp_rnd_t rnd_mode)
if (y != x)
/* y and x differ, thus we can safely try to compute y first */
{
- MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -2 * expx, 2, 0, rnd_mode,
- { inexy = _inexact;
- goto small_input; });
+ MPFR_FAST_COMPUTE_IF_SMALL_INPUT (
+ y, x, -2 * expx, 2, 0, rnd_mode,
+ { inexy = _inexact;
+ goto small_input; });
if (0)
{
small_input:
/* we can go here only if we can round sin(x) */
- MPFR_FAST_COMPUTE_IF_SMALL_INPUT (z, __gmpfr_one, -2 * expx,
- 1, 0, rnd_mode,
- { inexz = _inexact;
- goto end; });
+ MPFR_FAST_COMPUTE_IF_SMALL_INPUT (
+ z, __gmpfr_one, -2 * expx, 1, 0, rnd_mode,
+ { inexz = _inexact;
+ MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
+ goto end; });
}
/* if we go here, one of the two MPFR_FAST_COMPUTE_IF_SMALL_INPUT
@@ -101,18 +103,19 @@ mpfr_sin_cos (mpfr_ptr y, mpfr_ptr z, mpfr_srcptr x, mp_rnd_t rnd_mode)
else /* y and x are the same variable: try to compute z first, which
necessarily differs */
{
- MPFR_FAST_COMPUTE_IF_SMALL_INPUT (z, __gmpfr_one, -2 * expx,
- 1, 0, rnd_mode,
- { inexz = _inexact;
- goto small_input2; });
+ MPFR_FAST_COMPUTE_IF_SMALL_INPUT (
+ z, __gmpfr_one, -2 * expx, 1, 0, rnd_mode,
+ { inexz = _inexact;
+ goto small_input2; });
if (0)
{
small_input2:
/* we can go here only if we can round cos(x) */
- MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -2 * expx, 2, 0,
- rnd_mode,
- { inexy = _inexact;
- goto end; });
+ MPFR_FAST_COMPUTE_IF_SMALL_INPUT (
+ y, x, -2 * expx, 2, 0, rnd_mode,
+ { inexy = _inexact;
+ MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
+ goto end; });
}
}
m += 2 * (-expx);
@@ -207,7 +210,6 @@ mpfr_sin_cos (mpfr_ptr y, mpfr_ptr z, mpfr_srcptr x, mp_rnd_t rnd_mode)
mpfr_clear (xr);
end:
- /* FIXME: update the underflow flag if need be. */
MPFR_SAVE_EXPO_FREE (expo);
mpfr_check_range (y, inexy, rnd_mode);
mpfr_check_range (z, inexz, rnd_mode);
diff --git a/tests/tsin_cos.c b/tests/tsin_cos.c
index 6391d9711..6c21777ea 100644
--- a/tests/tsin_cos.c
+++ b/tests/tsin_cos.c
@@ -382,23 +382,56 @@ static void
consistency (void)
{
mpfr_t x, s1, s2, c1, c2;
+ mp_exp_t emin, emax;
mp_rnd_t rnd;
+ unsigned int flags_sin, flags_cos, flags, flags_before, flags_ref;
+ int inex_sin, inex_cos, inex, inex_ref;
int i;
+ emin = mpfr_get_emin ();
+ emax = mpfr_get_emax ();
+
for (i = 0; i <= 10000; i++)
{
mpfr_init2 (x, MPFR_PREC_MIN + (randlimb () % 8));
mpfr_inits2 (MPFR_PREC_MIN + (randlimb () % 8), s1, s2, c1, c2,
(mpfr_ptr) 0);
- tests_default_random (x, 256, -5, 50);
- rnd = RND_RAND ();
- mpfr_sin (s1, x, rnd);
- mpfr_cos (c1, x, rnd);
- mpfr_sin_cos (s2, c2, x, rnd);
- if (!(mpfr_equal_p (s1, s2) && mpfr_equal_p (c1, c2)))
+ if (i < 8 * GMP_RND_MAX)
+ {
+ int j = i / GMP_RND_MAX;
+ if (j & 1)
+ mpfr_set_emin (MPFR_EMIN_MIN);
+ mpfr_set_si (x, (j & 2) ? 1 : -1, GMP_RNDN);
+ mpfr_set_exp (x, mpfr_get_emin ());
+ rnd = (mpfr_rnd_t) (i % GMP_RND_MAX);
+ flags_before = 0;
+ if (j & 4)
+ mpfr_set_emax (-17);
+ }
+ else
+ {
+ tests_default_random (x, 256, -5, 50);
+ rnd = RND_RAND ();
+ flags_before = (randlimb () & 1) ?
+ (unsigned int) (MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE) :
+ (unsigned int) 0;
+ }
+ __gmpfr_flags = flags_before;
+ inex_sin = mpfr_sin (s1, x, rnd);
+ flags_sin = __gmpfr_flags;
+ __gmpfr_flags = flags_before;
+ inex_cos = mpfr_cos (c1, x, rnd);
+ flags_cos = __gmpfr_flags;
+ __gmpfr_flags = flags_before;
+ inex = !!mpfr_sin_cos (s2, c2, x, rnd);
+ flags = __gmpfr_flags;
+ inex_ref = inex_sin || inex_cos;
+ flags_ref = flags_sin | flags_cos;
+ if (!(mpfr_equal_p (s1, s2) && mpfr_equal_p (c1, c2)) ||
+ inex != inex_ref || flags != flags_ref)
{
- printf ("mpfr_sin_cos and mpfr_sin/mpfr_cos disagree on %s,\nx = ",
- mpfr_print_rnd_mode (rnd));
+ printf ("mpfr_sin_cos and mpfr_sin/mpfr_cos disagree on %s,"
+ " i = %d\nx = ", mpfr_print_rnd_mode (rnd), i);
mpfr_dump (x);
printf ("s1 = ");
mpfr_dump (s1);
@@ -408,9 +441,16 @@ consistency (void)
mpfr_dump (c1);
printf ("c2 = ");
mpfr_dump (c2);
+ printf ("inex_sin = %d, inex_cos = %d, inex = %d (expected %d)\n",
+ inex_sin, inex_cos, inex, inex_ref);
+ printf ("flags_sin = 0x%x, flags_cos = 0x%x, "
+ "flags = 0x%x (expected 0x%x)\n",
+ flags_sin, flags_cos, flags, flags_ref);
exit (1);
}
mpfr_clears (x, s1, s2, c1, c2, (mpfr_ptr) 0);
+ mpfr_set_emin (emin);
+ mpfr_set_emax (emax);
}
}