tset_z.c revision 1.1 1 1.1 mrg /* Test file for mpfr_set_z.
2 1.1 mrg
3 1.1 mrg Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 1.1 mrg Contributed by the Arenaire and Cacao projects, INRIA.
5 1.1 mrg
6 1.1 mrg This file is part of the GNU MPFR Library.
7 1.1 mrg
8 1.1 mrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9 1.1 mrg it under the terms of the GNU Lesser General Public License as published by
10 1.1 mrg the Free Software Foundation; either version 3 of the License, or (at your
11 1.1 mrg option) any later version.
12 1.1 mrg
13 1.1 mrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 1.1 mrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 1.1 mrg License for more details.
17 1.1 mrg
18 1.1 mrg You should have received a copy of the GNU Lesser General Public License
19 1.1 mrg along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 1.1 mrg http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 1.1 mrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 1.1 mrg
23 1.1 mrg #include <stdio.h>
24 1.1 mrg #include <stdlib.h>
25 1.1 mrg #include <limits.h>
26 1.1 mrg
27 1.1 mrg #include "mpfr-test.h"
28 1.1 mrg
29 1.1 mrg static void check0(void)
30 1.1 mrg {
31 1.1 mrg mpz_t y;
32 1.1 mrg mpfr_t x;
33 1.1 mrg int inexact, r;
34 1.1 mrg
35 1.1 mrg /* Check for +0 */
36 1.1 mrg mpfr_init (x);
37 1.1 mrg mpz_init (y);
38 1.1 mrg mpz_set_si (y, 0);
39 1.1 mrg for(r = 0; r < MPFR_RND_MAX; r++)
40 1.1 mrg {
41 1.1 mrg inexact = mpfr_set_z (x, y, (mpfr_rnd_t) r);
42 1.1 mrg if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact)
43 1.1 mrg {
44 1.1 mrg printf("mpfr_set_z(x,0) failed for %s\n",
45 1.1 mrg mpfr_print_rnd_mode ((mpfr_rnd_t) r));
46 1.1 mrg exit(1);
47 1.1 mrg }
48 1.1 mrg }
49 1.1 mrg mpfr_clear(x);
50 1.1 mrg mpz_clear(y);
51 1.1 mrg }
52 1.1 mrg
53 1.1 mrg /* FIXME: It'd be better to examine the actual data in an mpfr_t to see that
54 1.1 mrg it's as expected. Comparing mpfr_set_z with mpfr_cmp or against
55 1.1 mrg mpfr_get_si is a rather indirect test of a low level routine. */
56 1.1 mrg
57 1.1 mrg static void
58 1.1 mrg check (long i, mpfr_rnd_t rnd)
59 1.1 mrg {
60 1.1 mrg mpfr_t f;
61 1.1 mrg mpz_t z;
62 1.1 mrg
63 1.1 mrg mpfr_init2 (f, 8 * sizeof(long));
64 1.1 mrg mpz_init (z);
65 1.1 mrg mpz_set_ui (z, i);
66 1.1 mrg mpfr_set_z (f, z, rnd);
67 1.1 mrg if (mpfr_get_si (f, MPFR_RNDZ) != i)
68 1.1 mrg {
69 1.1 mrg printf ("Error in mpfr_set_z for i=%ld rnd_mode=%d\n", i, rnd);
70 1.1 mrg exit (1);
71 1.1 mrg }
72 1.1 mrg mpfr_clear (f);
73 1.1 mrg mpz_clear (z);
74 1.1 mrg }
75 1.1 mrg
76 1.1 mrg static void
77 1.1 mrg check_large (void)
78 1.1 mrg {
79 1.1 mrg mpz_t z;
80 1.1 mrg mpfr_t x, y;
81 1.1 mrg mpfr_exp_t emax, emin;
82 1.1 mrg
83 1.1 mrg mpz_init (z);
84 1.1 mrg mpfr_init2 (x, 160);
85 1.1 mrg mpfr_init2 (y, 160);
86 1.1 mrg
87 1.1 mrg mpz_set_str (z, "77031627725494291259359895954016675357279104942148788042", 10);
88 1.1 mrg mpfr_set_z (x, z, MPFR_RNDN);
89 1.1 mrg mpfr_set_str_binary (y, "0.1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111000001001E186");
90 1.1 mrg if (mpfr_cmp (x, y))
91 1.1 mrg {
92 1.1 mrg printf ("Error in mpfr_set_z on large input\n");
93 1.1 mrg exit (1);
94 1.1 mrg }
95 1.1 mrg
96 1.1 mrg /* check overflow */
97 1.1 mrg emax = mpfr_get_emax ();
98 1.1 mrg set_emax (2);
99 1.1 mrg mpz_set_str (z, "7", 10);
100 1.1 mrg mpfr_set_z (x, z, MPFR_RNDU);
101 1.1 mrg MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
102 1.1 mrg set_emax (3);
103 1.1 mrg mpfr_set_prec (x, 2);
104 1.1 mrg mpz_set_str (z, "7", 10);
105 1.1 mrg mpfr_set_z (x, z, MPFR_RNDU);
106 1.1 mrg MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
107 1.1 mrg set_emax (emax);
108 1.1 mrg
109 1.1 mrg /* check underflow */
110 1.1 mrg emin = mpfr_get_emin ();
111 1.1 mrg set_emin (3);
112 1.1 mrg mpz_set_str (z, "1", 10);
113 1.1 mrg mpfr_set_z (x, z, MPFR_RNDZ);
114 1.1 mrg MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
115 1.1 mrg set_emin (2);
116 1.1 mrg mpfr_set_z (x, z, MPFR_RNDN);
117 1.1 mrg MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));
118 1.1 mrg set_emin (emin);
119 1.1 mrg
120 1.1 mrg mpz_clear (z);
121 1.1 mrg
122 1.1 mrg mpfr_clear (x);
123 1.1 mrg mpfr_clear (y);
124 1.1 mrg }
125 1.1 mrg
126 1.1 mrg int
127 1.1 mrg main (int argc, char *argv[])
128 1.1 mrg {
129 1.1 mrg long j;
130 1.1 mrg
131 1.1 mrg tests_start_mpfr ();
132 1.1 mrg
133 1.1 mrg check_large ();
134 1.1 mrg check (0, MPFR_RNDN);
135 1.1 mrg for (j = 0; j < 200000; j++)
136 1.1 mrg check (randlimb () & LONG_MAX, RND_RAND ());
137 1.1 mrg check0 ();
138 1.1 mrg
139 1.1 mrg tests_end_mpfr ();
140 1.1 mrg
141 1.1 mrg return 0;
142 1.1 mrg }
143