1/* Test compatibility mpf-mpfr. 2 3Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4Contributed by the Arenaire and Cacao projects, INRIA. 5 6This file is part of the GNU MPFR Library. 7 8The GNU MPFR Library is free software; you can redistribute it and/or modify 9it under the terms of the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MPFR Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23#if defined (__cplusplus) 24#include <cstdio> 25#else 26#include <stdio.h> 27#endif 28#include <stdlib.h> 29#include <string.h> 30 31#include "gmp.h" 32#include "mpfr.h" 33#ifdef MPFR 34#include "mpf2mpfr.h" 35#endif 36 37int 38main (void) 39{ 40 unsigned long int prec; 41 unsigned long int prec2; 42 mpf_t x, y; 43 mpz_t z; 44 mpq_t q; 45 double d; 46 signed long int exp; 47 long l; 48 unsigned long u; 49 char *s; 50 int i; 51 FILE *f; 52 gmp_randstate_t state; 53 54 /* Initialization Functions */ 55 prec = 53; 56 mpf_set_default_prec (prec); 57 prec2 = mpf_get_default_prec (); 58 if (prec2 < prec) 59 { 60 printf ("Error in get_default_prec: %lu < %lu\n", prec2, prec); 61 exit (1); 62 } 63 64 mpf_init (y); 65 66 mpf_init2 (x, prec); 67 prec2 = mpf_get_prec (x); 68 if (prec2 < prec) 69 { 70 printf ("Error in get_prec: %lu < %lu\n", prec2, prec); 71 mpf_clear (y); 72 mpf_clear (x); 73 exit (1); 74 } 75 76 mpf_set_prec (x, 2 * prec); 77 prec2 = mpf_get_prec (x); 78 if (prec2 < 2 * prec) 79 { 80 printf ("Error in set_prec: %lu < %lu\n", prec2, 2 * prec); 81 mpf_clear (y); 82 mpf_clear (x); 83 exit (1); 84 } 85 86 mpf_set_prec_raw (x, prec); 87 prec2 = mpf_get_prec (x); 88 if (prec2 < prec) 89 { 90 printf ("Error in set_prec_raw: %lu < %lu\n", prec2, prec); 91 mpf_clear (y); 92 mpf_clear (x); 93 exit (1); 94 } 95 96 /* Assignment Functions */ 97 98 mpf_set (y, x); 99 mpf_set_ui (x, 1); 100 mpf_set_si (x, -1); 101 mpf_set_d (x, 1.0); 102 103 mpz_init_set_ui (z, 17); 104 mpf_set_z (x, z); 105 mpz_clear (z); 106 107 mpq_init (q); 108 mpq_set_ui (q, 2, 3); 109 mpf_set_q (x, q); 110 mpq_clear (q); 111 112 mpf_set_str (x, "3.1415e1", 10); 113 mpf_swap (x, y); 114 115 /* Combined Initialization and Assignment Functions */ 116 117 mpf_clear (x); 118 mpf_init_set (x, y); 119 mpf_clear (x); 120 mpf_init_set_ui (x, 17); 121 mpf_clear (x); 122 mpf_init_set_si (x, -17); 123 mpf_clear (x); 124 mpf_init_set_d (x, 17.0); 125 mpf_clear (x); 126 mpf_init_set_str (x, "3.1415e1", 10); 127 128 /* Conversion Functions */ 129 130 d = mpf_get_d (x); 131 d = mpf_get_d_2exp (&exp, x); 132 l = mpf_get_si (x); 133 u = mpf_get_ui (x); 134 s = mpf_get_str (NULL, &exp, 10, 10, x); 135 /* MPF doen't have mpf_free_str */ 136 mpfr_free_str (s); 137 138 139 /* Arithmetic Functions */ 140 141 mpf_add (y, x, x); 142 mpf_add_ui (y, x, 1); 143 mpf_sub (y, x, x); 144 mpf_ui_sub (y, 1, x); 145 mpf_sub_ui (y, x, 1); 146 mpf_mul (y, x, x); 147 mpf_mul_ui (y, x, 17); 148 mpf_div (y, x, x); 149 mpf_ui_div (y, 17, x); 150 mpf_div_ui (y, x, 17); 151 mpf_sqrt (y, x); 152 mpf_sqrt_ui (y, 17); 153 mpf_pow_ui (y, x, 2); 154 mpf_neg (y, x); 155 mpf_abs (y, x); 156 mpf_mul_2exp (y, x, 17); 157 mpf_div_2exp (y, x, 17); 158 159 /* Comparison Functions */ 160 161 i = mpf_cmp (y, x); 162 i = mpf_cmp_d (y, 1.7); 163 i = mpf_cmp_ui (y, 17); 164 i = mpf_cmp_si (y, -17); 165 i = mpf_eq (y, x, 17); 166 mpf_reldiff (y, y, x); 167 i = mpf_sgn (x); 168 169 /* Input and Output Functions */ 170 171 f = fopen ("/dev/null", "w"); 172 if (f != NULL) 173 { 174 mpf_out_str (f, 10, 10, x); 175 fclose (f); 176 } 177 178 mpf_set_prec (x, 15); 179 mpf_set_prec (y, 15); 180 /* We may use src_fopen instead of fopen, but it is defined 181 in mpfr-test, and not in mpfr.h and gmp.h, and we want 182 to test theses includes files. */ 183 f = fopen ("inp_str.data", "r"); 184 if (f != NULL) 185 { 186 i = mpf_inp_str (x, f, 10); 187 if ((i == 0) || mpf_cmp_ui (x, 31415)) 188 { 189 printf ("Error in reading 1st line from file inp_str.data\n"); 190 exit (1); 191 } 192 fclose (f); 193 } 194 195 /* Miscellaneous Functions */ 196 197 mpf_ceil (y, x); 198 mpf_floor (y, x); 199 mpf_trunc (y, x); 200 201 i = mpf_integer_p (x); 202 203 i = mpf_fits_ulong_p (x); 204 i = mpf_fits_slong_p (x); 205 i = mpf_fits_uint_p (x); 206 i = mpf_fits_sint_p (x); 207 i = mpf_fits_ushort_p (x); 208 i = mpf_fits_sshort_p (x); 209 210 gmp_randinit_lc_2exp_size (state, 128); 211 mpf_urandomb (x, state, 10); 212 gmp_randclear (state); 213 214 /* Conversion to mpz */ 215 mpz_init (z); 216 mpf_set_ui (x, 17); 217 mpz_set_f (z, x); 218 mpf_set_z (x, z); 219 mpz_clear (z); 220 if (mpf_cmp_ui (x, 17) != 0) 221 { 222 fprintf (stderr, "Error in conversion to/from mpz\n"); 223 fprintf (stderr, "expected 17, got %1.16e\n", mpf_get_d (x)); 224 exit (1); 225 } 226 227 /* clear all variables */ 228 mpf_clear (y); 229 mpf_clear (x); 230 231 return 0; 232} 233