summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthevenyp <thevenyp@280ebfd0-de03-0410-8827-d642c229c3f4>2009-07-20 09:22:08 +0000
committerthevenyp <thevenyp@280ebfd0-de03-0410-8827-d642c229c3f4>2009-07-20 09:22:08 +0000
commit400d708c80da335d631bcb8b74ffb6758ae78a2b (patch)
treeffe19deb7a1837ea371983fb1d1829d6df6b7f4b
parent7315d9ffc4024673374415bcac4ebf64d12cccc3 (diff)
downloadmpfr-400d708c80da335d631bcb8b74ffb6758ae78a2b.tar.gz
Makefile.am mpfr.h mpfr.texi set_zero.c: New function mpfr_set_zero.
tests/tset.c: Replace all MPFR_ASSERTN with verbose message. Add tests for mpfr_set_zero. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@6339 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--Makefile.am2
-rw-r--r--mpfr.h1
-rw-r--r--mpfr.texi11
-rw-r--r--set_zero.c31
-rw-r--r--tests/tset.c149
5 files changed, 137 insertions, 57 deletions
diff --git a/Makefile.am b/Makefile.am
index deef8a14a..21db84e31 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -8,7 +8,7 @@ include_HEADERS = mpfr.h mpf2mpfr.h
lib_LTLIBRARIES = libmpfr.la
-libmpfr_la_SOURCES = mpfr.h mpf2mpfr.h mpfr-gmp.h mpfr-impl.h mpfr-longlong.h mpfr-thread.h exceptions.c extract.c uceil_exp2.c uceil_log2.c ufloor_log2.c add.c add1.c add_ui.c agm.c clear.c cmp.c cmp_abs.c cmp_si.c cmp_ui.c comparisons.c div_2exp.c div_2si.c div_2ui.c div.c div_ui.c dump.c eq.c exp10.c exp2.c exp3.c exp.c frac.c get_d.c get_exp.c get_str.c init.c inp_str.c isinteger.c isinf.c isnan.c isnum.c const_log2.c log.c modf.c mul_2exp.c mul_2si.c mul_2ui.c mul.c mul_ui.c neg.c next.c out_str.c printf.c vasprintf.c const_pi.c pow.c pow_si.c pow_ui.c print_raw.c print_rnd_mode.c reldiff.c round_prec.c set.c setmax.c setmin.c set_d.c set_dfl_prec.c set_exp.c set_rnd.c set_f.c set_prc_raw.c set_prec.c set_q.c set_si.c set_str.c set_str_raw.c set_ui.c set_z.c sqrt.c sqrt_ui.c sub.c sub1.c sub_ui.c rint.c ui_div.c ui_sub.c urandomb.c get_z_exp.c swap.c factorial.c cosh.c sinh.c tanh.c sinh_cosh.c acosh.c asinh.c atanh.c atan.c cmp2.c exp_2.c asin.c const_euler.c cos.c sin.c tan.c fma.c fms.c hypot.c log1p.c expm1.c log2.c log10.c ui_pow.c ui_pow_ui.c minmax.c dim.c signbit.c copysign.c setsign.c gmp_op.c init2.c acos.c sin_cos.c set_nan.c set_inf.c powerof2.c gamma.c set_ld.c get_ld.c cbrt.c volatile.c fits_s.h fits_sshort.c fits_sint.c fits_slong.c fits_u.h fits_ushort.c fits_uint.c fits_ulong.c fits_uintmax.c fits_intmax.c get_si.c get_ui.c zeta.c cmp_d.c erf.c inits.c inits2.c clears.c sgn.c check.c sub1sp.c version.c mpn_exp.c mpfr-gmp.c mp_clz_tab.c sum.c add1sp.c free_cache.c si_op.c cmp_ld.c set_ui_2exp.c set_si_2exp.c set_uj.c set_sj.c get_sj.c get_uj.c get_z.c iszero.c cache.c sqr.c int_ceil_log2.c isqrt.c strtofr.c pow_z.c logging.c mulders.c get_f.c round_p.c erfc.c atan2.c subnormal.c const_catalan.c root.c gen_inverse.h sec.c csc.c cot.c eint.c sech.c csch.c coth.c round_near_x.c constant.c abort_prec_max.c stack_interface.c lngamma.c zeta_ui.c set_d64.c get_d64.c jn.c yn.c rem1.c get_patches.c add_d.c sub_d.c d_sub.c mul_d.c div_d.c d_div.c li2.c rec_sqrt.c min_prec.c buildopt.c
+libmpfr_la_SOURCES = mpfr.h mpf2mpfr.h mpfr-gmp.h mpfr-impl.h mpfr-longlong.h mpfr-thread.h exceptions.c extract.c uceil_exp2.c uceil_log2.c ufloor_log2.c add.c add1.c add_ui.c agm.c clear.c cmp.c cmp_abs.c cmp_si.c cmp_ui.c comparisons.c div_2exp.c div_2si.c div_2ui.c div.c div_ui.c dump.c eq.c exp10.c exp2.c exp3.c exp.c frac.c get_d.c get_exp.c get_str.c init.c inp_str.c isinteger.c isinf.c isnan.c isnum.c const_log2.c log.c modf.c mul_2exp.c mul_2si.c mul_2ui.c mul.c mul_ui.c neg.c next.c out_str.c printf.c vasprintf.c const_pi.c pow.c pow_si.c pow_ui.c print_raw.c print_rnd_mode.c reldiff.c round_prec.c set.c setmax.c setmin.c set_d.c set_dfl_prec.c set_exp.c set_rnd.c set_f.c set_prc_raw.c set_prec.c set_q.c set_si.c set_str.c set_str_raw.c set_ui.c set_z.c sqrt.c sqrt_ui.c sub.c sub1.c sub_ui.c rint.c ui_div.c ui_sub.c urandomb.c get_z_exp.c swap.c factorial.c cosh.c sinh.c tanh.c sinh_cosh.c acosh.c asinh.c atanh.c atan.c cmp2.c exp_2.c asin.c const_euler.c cos.c sin.c tan.c fma.c fms.c hypot.c log1p.c expm1.c log2.c log10.c ui_pow.c ui_pow_ui.c minmax.c dim.c signbit.c copysign.c setsign.c gmp_op.c init2.c acos.c sin_cos.c set_nan.c set_inf.c set_zero.c powerof2.c gamma.c set_ld.c get_ld.c cbrt.c volatile.c fits_s.h fits_sshort.c fits_sint.c fits_slong.c fits_u.h fits_ushort.c fits_uint.c fits_ulong.c fits_uintmax.c fits_intmax.c get_si.c get_ui.c zeta.c cmp_d.c erf.c inits.c inits2.c clears.c sgn.c check.c sub1sp.c version.c mpn_exp.c mpfr-gmp.c mp_clz_tab.c sum.c add1sp.c free_cache.c si_op.c cmp_ld.c set_ui_2exp.c set_si_2exp.c set_uj.c set_sj.c get_sj.c get_uj.c get_z.c iszero.c cache.c sqr.c int_ceil_log2.c isqrt.c strtofr.c pow_z.c logging.c mulders.c get_f.c round_p.c erfc.c atan2.c subnormal.c const_catalan.c root.c gen_inverse.h sec.c csc.c cot.c eint.c sech.c csch.c coth.c round_near_x.c constant.c abort_prec_max.c stack_interface.c lngamma.c zeta_ui.c set_d64.c get_d64.c jn.c yn.c rem1.c get_patches.c add_d.c sub_d.c d_sub.c mul_d.c div_d.c d_div.c li2.c rec_sqrt.c min_prec.c buildopt.c
libmpfr_la_LIBADD = @LIBOBJS@
diff --git a/mpfr.h b/mpfr.h
index 53fa67551..7322003f8 100644
--- a/mpfr.h
+++ b/mpfr.h
@@ -291,6 +291,7 @@ __MPFR_DECLSPEC int
mpfr_set_z _MPFR_PROTO ((mpfr_ptr, mpz_srcptr, mpfr_rnd_t));
__MPFR_DECLSPEC void mpfr_set_nan _MPFR_PROTO ((mpfr_ptr));
__MPFR_DECLSPEC void mpfr_set_inf _MPFR_PROTO ((mpfr_ptr, int));
+__MPFR_DECLSPEC void mpfr_set_zero _MPFR_PROTO ((mpfr_ptr, int));
__MPFR_DECLSPEC int
mpfr_set_f _MPFR_PROTO ((mpfr_ptr, mpf_srcptr, mpfr_rnd_t));
__MPFR_DECLSPEC int
diff --git a/mpfr.texi b/mpfr.texi
index 4c06c7868..f49cc1833 100644
--- a/mpfr.texi
+++ b/mpfr.texi
@@ -1181,11 +1181,12 @@ NaN.
@end deftypefun
-@deftypefun void mpfr_set_inf (mpfr_t @var{x}, int @var{sign})
-@deftypefunx void mpfr_set_nan (mpfr_t @var{x})
-Set the variable @var{x} to infinity or NaN (Not-a-Number) respectively.
-In @code{mpfr_set_inf}, @var{x} is set to plus infinity iff @var{sign} is
-nonnegative.
+@deftypefun void mpfr_set_nan (mpfr_t @var{x})
+@deftypefunx void mpfr_set_inf (mpfr_t @var{x}, int @var{sign})
+@deftypefunx void mpfr_set_zero (mpfr_t @var{x}, int @var{sign})
+Set the variable @var{x} to NaN (Not-a-Number), infinity or zero respectively.
+In @code{mpfr_set_inf} or @code{mpfr_set_zero}, @var{x} is set to plus
+infinity or plus zero iff @var{sign} is nonnegative.
@end deftypefun
@deftypefun void mpfr_swap (mpfr_t @var{x}, mpfr_t @var{y})
diff --git a/set_zero.c b/set_zero.c
new file mode 100644
index 000000000..8308c7022
--- /dev/null
+++ b/set_zero.c
@@ -0,0 +1,31 @@
+/* mpfr_set_zero -- set a number to plus or minus zero.
+
+Copyright 2009 Free Software Foundation, Inc.
+Contributed by the Arenaire and Cacao projects, INRIA.
+
+This file is part of the GNU MPFR Library.
+
+The GNU MPFR Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+The GNU MPFR Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MPFR Library; see the file COPYING.LIB. If not, see
+http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
+51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#include "mpfr-impl.h"
+
+void
+mpfr_set_zero (mpfr_ptr x, int sign)
+{
+ mpfr_set_ui (x, 0, MPFR_RNDN);
+ if (sign < 0)
+ MPFR_SET_NEG(x);
+}
diff --git a/tests/tset.c b/tests/tset.c
index 0ab66078b..e956d7636 100644
--- a/tests/tset.c
+++ b/tests/tset.c
@@ -25,6 +25,13 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
#include "mpfr-test.h"
+#define PRINT_ERROR_IF(condition, text) \
+ do { \
+ if (condition) \
+ printf (text); \
+ } while (0)
+
+
/* Maybe better create its own test file ? */
static void
check_neg_special (void)
@@ -34,12 +41,98 @@ check_neg_special (void)
MPFR_SET_NAN (x);
mpfr_clear_nanflag ();
mpfr_neg (x, x, MPFR_RNDN);
- if (!mpfr_nanflag_p () )
+ PRINT_ERROR_IF (!mpfr_nanflag_p (),
+ "ERROR: neg (NaN) doesn't set Nan flag.\n");
+ mpfr_clear (x);
+}
+
+static void
+check_special (void)
+{
+ mpfr_t x, y;
+ int inexact;
+
+ mpfr_init (x);
+ mpfr_init (y);
+
+ mpfr_set_inf (x, 1);
+ PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) < 0,
+ "ERROR: mpfr_set_inf failed to set variable to +infinity.\n");
+ inexact = mpfr_set (y, x, MPFR_RNDN);
+ PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
+ "ERROR: mpfr_set failed to set variable to +infinity.\n");
+
+ inexact = mpfr_set_ui (y, 0, MPFR_RNDN);
+ PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
+ "ERROR: mpfr_set_ui failed to set variable to +0.\n");
+
+ mpfr_set_inf (x, -1);
+ PRINT_ERROR_IF (!mpfr_inf_p (x) || mpfr_sgn (x) > 0,
+ "ERROR: mpfr_set_inf failed to set variable to -infinity.\n");
+ inexact = mpfr_set (y, x, MPFR_RNDN);
+ PRINT_ERROR_IF (!mpfr_inf_p (y) || mpfr_sgn (y) > 0 || inexact != 0,
+ "ERROR: mpfr_set failed to set variable to -infinity.\n");
+
+ mpfr_set_zero (x, 1);
+ PRINT_ERROR_IF (!mpfr_zero_p (x) || mpfr_sgn (x) < 0,
+ "ERROR: mpfr_set_zero failed to set variable to +0.\n");
+ inexact = mpfr_set (y, x, MPFR_RNDN);
+ PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) < 0 || inexact != 0,
+ "ERROR: mpfr_set failed to set variable to +0.\n");
+
+ mpfr_set_zero (x, -1);
+ PRINT_ERROR_IF (!mpfr_zero_p (x) || mpfr_sgn (x) > 0,
+ "ERROR: mpfr_set_zero failed to set variable to -0.\n");
+ inexact = mpfr_set (y, x, MPFR_RNDN);
+ PRINT_ERROR_IF (!mpfr_zero_p (y) || mpfr_sgn (y) > 0 || inexact != 0,
+ "ERROR: mpfr_set failed to set variable to -0.\n");
+
+ mpfr_set_nan (x);
+ PRINT_ERROR_IF (!mpfr_nan_p (x),
+ "ERROR: mpfr_set_nan failed to set variable to NaN.\n");
+ inexact = mpfr_set (y, x, MPFR_RNDN);
+ PRINT_ERROR_IF (!mpfr_nan_p (y) || inexact != 0,
+ "ERROR: mpfr_set failed to set variable to NaN.\n");
+
+ mpfr_clear (x);
+ mpfr_clear (y);
+}
+
+static void
+check_ternary_value (void)
+{
+ int p, q, rnd;
+ int inexact, cmp;
+ mpfr_t x, y;
+
+ mpfr_init (x);
+ mpfr_init (y);
+ for (p=2; p<500; p++)
{
- printf("ERROR: neg (NaN) doesn't set Nan flag.\n");
- exit (1);
+ mpfr_set_prec (x, p);
+ mpfr_urandomb (x, RANDS);
+ if (randlimb () % 2)
+ mpfr_neg (x, x, MPFR_RNDN);
+ for (q=2; q<2*p; q++)
+ {
+ mpfr_set_prec (y, q);
+ for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
+ {
+ inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd);
+ cmp = mpfr_cmp (y, x);
+ if (((inexact == 0) && (cmp != 0)) ||
+ ((inexact > 0) && (cmp <= 0)) ||
+ ((inexact < 0) && (cmp >= 0)))
+ {
+ printf ("Wrong ternary value in mpfr_set: expected %d,"
+ " got %d\n", cmp, inexact);
+ exit (1);
+ }
+ }
+ }
}
mpfr_clear (x);
+ mpfr_clear (y);
}
#define TEST_FUNCTION mpfr_set
@@ -51,7 +144,7 @@ main (void)
mp_prec_t p, q;
mpfr_t x, y, z, u;
int rnd;
- int inexact, cmp;
+ int inexact;
mp_exp_t emax;
tests_start_mpfr ();
@@ -62,27 +155,6 @@ main (void)
inexact = mpfr_init_set_ui (z, 1, MPFR_RNDN);
inexact = mpfr_init_set_d (u, 1.0, MPFR_RNDN);
- mpfr_set_nan (x);
- (mpfr_set) (y, x, MPFR_RNDN);
- MPFR_ASSERTN(mpfr_nan_p (y));
-
- mpfr_set_inf (x, 1);
- mpfr_set (y, x, MPFR_RNDN);
- MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) > 0);
-
- mpfr_set_inf (x, -1);
- mpfr_set (y, x, MPFR_RNDN);
- MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) < 0);
-
- mpfr_set_ui (x, 0, MPFR_RNDN);
- mpfr_set (y, x, MPFR_RNDN);
- MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS(y));
-
- mpfr_set_ui (x, 0, MPFR_RNDN);
- mpfr_neg (x, x, MPFR_RNDN);
- mpfr_set (y, x, MPFR_RNDN);
- MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG(y));
-
emax = mpfr_get_emax ();
set_emax (0);
mpfr_set_prec (x, 3);
@@ -98,7 +170,6 @@ main (void)
exit (1);
}
- MPFR_ASSERTN (MPFR_IS_INF (y) && MPFR_SIGN (y) > 0);
set_emax (emax);
mpfr_set_prec (y, 11);
@@ -112,36 +183,12 @@ main (void)
exit (1);
}
- for (p=2; p<500; p++)
- {
- mpfr_set_prec (x, p);
- mpfr_urandomb (x, RANDS);
- if (randlimb () % 2)
- mpfr_neg (x, x, MPFR_RNDN);
- for (q=2; q<2*p; q++)
- {
- mpfr_set_prec (y, q);
- for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
- {
- inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd);
- cmp = mpfr_cmp (y, x);
- if (((inexact == 0) && (cmp != 0)) ||
- ((inexact > 0) && (cmp <= 0)) ||
- ((inexact < 0) && (cmp >= 0)))
- {
- printf ("Wrong inexact flag in mpfr_set: expected %d,"
- " got %d\n", cmp, inexact);
- exit (1);
- }
- }
- }
- }
-
mpfr_clear (x);
mpfr_clear (y);
mpfr_clear (z);
mpfr_clear (u);
+ check_ternary_value ();
check_neg_special ();
test_generic (2, 1000, 10);