1 /* Test file for mpfr_const_log2.
3 Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include "mpfr-test.h"
28 /* tlog2 [prec] [rnd] [0 = no print] */
31 check (mpfr_prec_t p0, mpfr_prec_t p1)
39 mpfr_init2 (z, p1 + 10);
40 mpfr_const_log2 (z, MPFR_RNDN);
41 mpfr_clear_cache (__gmpfr_cache_const_log2);
45 mpfr_set_prec (x, p0);
46 mpfr_set_prec (y, p0);
49 mpfr_const_log2 (x, rnd);
51 if ((dif = mpfr_cmp (x, y))
52 && mpfr_can_round (z, mpfr_get_prec(z), MPFR_RNDN,
55 printf ("mpfr_const_log2 fails for prec=%u, rnd=%s Diff=%d\n",
56 (unsigned int) p0, mpfr_print_rnd_mode (rnd), dif);
57 printf ("expected "), mpfr_dump (y);
58 printf ("got "), mpfr_dump (x);
73 mpfr_init2 (x, 25000);
74 mpfr_init2 (y, 26000);
75 (mpfr_const_log2) (x, MPFR_RNDN); /* First one ! */
76 (mpfr_const_log2) (y, MPFR_RNDN); /* Then the other - cache - */
77 mpfr_prec_round (y, 25000, MPFR_RNDN);
80 printf ("const_log2: error for large prec\n");
84 /* worst-case with 15 successive ones after last bit,
85 to exercise can_round loop */
86 mpfr_set_prec (x, 26249);
87 mpfr_const_log2 (x, MPFR_RNDZ);
89 mpfr_clears (x, y, (mpfr_ptr) 0);
100 i = mpfr_const_log2 (x, MPFR_RNDN);
103 printf("Error for log2. Invalid ternary value (1).\n");
106 mpfr_set_prec (x, 194);
107 i = mpfr_const_log2 (x, MPFR_RNDN);
110 printf("Error for log2. Invalid ternary value (2).\n");
115 mpfr_set_prec (x, 9);
116 mpfr_const_log2 (x, MPFR_RNDN);
117 mpfr_set_prec (x, 8);
118 mpfr_const_log2 (x, MPFR_RNDN);
119 if (mpfr_cmp_str (x, "0.10110001E0", 2, MPFR_RNDN))
121 printf("Error for log2. Wrong rounding.\n");
128 /* Wrapper for tgeneric */
130 my_const_log2 (mpfr_ptr x, mpfr_srcptr y, mpfr_rnd_t r)
132 return mpfr_const_log2 (x, r);
135 #define RAND_FUNCTION(x) mpfr_set_ui ((x), 0, MPFR_RNDN)
136 #define TEST_FUNCTION my_const_log2
137 #include "tgeneric.c"
140 main (int argc, char *argv[])
148 p = (argc>1) ? atoi(argv[1]) : 53;
149 rnd = (argc>2) ? (mpfr_rnd_t) atoi(argv[2]) : MPFR_RNDZ;
155 /* check precision of 2 bits */
156 mpfr_set_prec (x, 2);
157 mpfr_const_log2 (x, MPFR_RNDN);
158 if (mpfr_cmp_ui_2exp(x, 3, -2)) /* 3*2^-2 */
160 printf ("mpfr_const_log2 failed for prec=2, rnd=MPFR_RNDN\n"
161 "expected 0.75, got ");
162 mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN);
169 mpfr_set_prec (x, p);
170 mpfr_const_log2 (x, rnd);
172 mpfr_out_str (stdout, 10, 0, x, rnd);
176 mpfr_set_prec (x, 53);
177 mpfr_const_log2 (x, MPFR_RNDZ);
178 if (mpfr_cmp_str1 (x, "6.9314718055994530941e-1") )
180 printf ("mpfr_const_log2 failed for prec=53\n");
184 mpfr_set_prec (x, 32);
185 mpfr_const_log2 (x, MPFR_RNDN);
186 if (mpfr_cmp_str1 (x, "0.69314718060195446"))
188 printf ("mpfr_const_log2 failed for prec=32\n");
197 test_generic (2, 200, 1);