1 /* Test file for mpfr_set_z. 2 3 Copyright 1999, 2001-2023 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramba projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 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. 12 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. 17 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 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 #include "mpfr-test.h" 24 25 static void check0(void) 26 { 27 mpz_t y; 28 mpfr_t x; 29 int inexact, r; 30 31 /* Check for +0 */ 32 mpfr_init (x); 33 mpz_init (y); 34 mpz_set_si (y, 0); 35 RND_LOOP (r) 36 { 37 inexact = mpfr_set_z (x, y, (mpfr_rnd_t) r); 38 if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact) 39 { 40 printf("mpfr_set_z(x,0) failed for %s\n", 41 mpfr_print_rnd_mode ((mpfr_rnd_t) r)); 42 exit(1); 43 } 44 } 45 mpfr_clear(x); 46 mpz_clear(y); 47 } 48 49 /* FIXME: It'd be better to examine the actual data in an mpfr_t to see that 50 it's as expected. Comparing mpfr_set_z with mpfr_cmp or against 51 mpfr_get_si is a rather indirect test of a low level routine. */ 52 53 static void 54 check (long i, mpfr_rnd_t rnd) 55 { 56 mpfr_t f; 57 mpz_t z; 58 59 mpfr_init2 (f, 8 * sizeof(long)); 60 mpz_init (z); 61 mpz_set_ui (z, i); 62 mpfr_set_z (f, z, rnd); 63 if (mpfr_get_si (f, MPFR_RNDZ) != i) 64 { 65 printf ("Error in mpfr_set_z for i=%ld rnd_mode=%d\n", i, rnd); 66 exit (1); 67 } 68 mpfr_clear (f); 69 mpz_clear (z); 70 } 71 72 static void 73 check_large (void) 74 { 75 mpz_t z; 76 mpfr_t x, y; 77 mpfr_exp_t emax, emin; 78 79 mpz_init (z); 80 mpfr_init2 (x, 160); 81 mpfr_init2 (y, 160); 82 83 mpz_set_str (z, "77031627725494291259359895954016675357279104942148788042", 10); 84 mpfr_set_z (x, z, MPFR_RNDN); 85 mpfr_set_str_binary (y, "0.1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111000001001E186"); 86 if (mpfr_cmp (x, y)) 87 { 88 printf ("Error in mpfr_set_z on large input\n"); 89 exit (1); 90 } 91 92 /* check overflow */ 93 emax = mpfr_get_emax (); 94 set_emax (2); 95 mpz_set_str (z, "7", 10); 96 mpfr_set_z (x, z, MPFR_RNDU); 97 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); 98 set_emax (3); 99 mpfr_set_prec (x, 2); 100 mpz_set_str (z, "7", 10); 101 mpfr_set_z (x, z, MPFR_RNDU); 102 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); 103 set_emax (emax); 104 105 /* check underflow */ 106 emin = mpfr_get_emin (); 107 set_emin (3); 108 mpz_set_str (z, "1", 10); 109 mpfr_set_z (x, z, MPFR_RNDZ); 110 MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x)); 111 set_emin (2); 112 mpfr_set_z (x, z, MPFR_RNDN); 113 MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x)); 114 set_emin (emin); 115 116 mpz_clear (z); 117 118 mpfr_clear (x); 119 mpfr_clear (y); 120 } 121 122 int 123 main (int argc, char *argv[]) 124 { 125 long j; 126 127 tests_start_mpfr (); 128 129 check_large (); 130 check (0, MPFR_RNDN); 131 for (j = 0; j < 200000; j++) 132 check (randlimb () & LONG_MAX, RND_RAND ()); 133 check0 (); 134 135 tests_end_mpfr (); 136 137 return 0; 138 } 139