summaryrefslogtreecommitdiff
path: root/tests/trint.c
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2002-01-30 04:57:52 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2002-01-30 04:57:52 +0000
commit8daf86c3ea4a53b2d0ec7143c010652a09fc6749 (patch)
treeb14fb532520dfe5bd01af94e170486c06f2c612c /tests/trint.c
parentdd9723f715d6c1a18616404cfcf302b082b42672 (diff)
downloadmpfr-8daf86c3ea4a53b2d0ec7143c010652a09fc6749.tar.gz
Tests added (mpfr_trunc, mpfr_floor, mpfr_ceil, mpfr_round).
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@1673 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'tests/trint.c')
-rw-r--r--tests/trint.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/trint.c b/tests/trint.c
new file mode 100644
index 000000000..28be5b78c
--- /dev/null
+++ b/tests/trint.c
@@ -0,0 +1,94 @@
+/* Test file for mpfr_trunc, mpfr_floor, mpfr_ceil, mpfr_round.
+
+Copyright (C) 2002 Free Software Foundation.
+
+This file is part of the MPFR Library.
+
+The 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 2.1 of the License, or (at your
+option) any later version.
+
+The 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 MPFR Library; see the file COPYING.LIB. If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+MA 02111-1307, USA. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "gmp.h"
+#include "mpfr.h"
+
+int
+main (void)
+{
+ mp_size_t s;
+ mpz_t z;
+ mp_prec_t p;
+ mpfr_t x, y, t;
+ mp_rnd_t r;
+ int inexact, sign_t;
+
+ mpfr_init (x);
+ mpfr_init (y);
+ mpz_init (z);
+ mpfr_init (t);
+ mpz_set_ui (z, 1);
+ for (s=1; s<100; s++)
+ {
+ /* z has exactly s bits */
+
+ mpz_mul_2exp (z, z, 1);
+ if (rand () % 2)
+ mpz_add_ui (z, z, 1);
+ if (s == 1)
+ continue;
+ mpfr_set_prec (x, s);
+ mpfr_set_prec (t, s);
+ if (mpfr_set_z (x, z, GMP_RNDN))
+ {
+ fprintf (stderr, "Error: mpfr_set_z should be exact (s = %u)\n",
+ (unsigned int) s);
+ exit (1);
+ }
+ for (p=2; p<100; p++)
+ {
+ mpfr_set_prec (y, p);
+ for (r=0; r<4; r++)
+ {
+ if (r == GMP_RNDN)
+ inexact = mpfr_round (y, x);
+ else if (r == GMP_RNDZ)
+ inexact = mpfr_trunc (y, x);
+ else if (r == GMP_RNDU)
+ inexact = mpfr_ceil (y, x);
+ else /* r = GMP_RNDD */
+ inexact = mpfr_floor (y, x);
+ if (mpfr_sub (t, y, x, GMP_RNDN))
+ {
+ fprintf (stderr, "Error: subtraction should be exact\n");
+ exit (1);
+ }
+ sign_t = mpfr_cmp_ui (t, 0);
+ if (((inexact == 0) && (sign_t != 0)) ||
+ ((inexact < 0) && (sign_t >= 0)) ||
+ ((inexact > 0) && (sign_t <= 0)))
+ {
+ fprintf (stderr, "Wrong inexact flag\n");
+ exit (1);
+ }
+ }
+ }
+ }
+ mpfr_clear (x);
+ mpfr_clear (y);
+ mpz_clear (z);
+ mpfr_clear (t);
+
+ return 0;
+}