1 /* Test file for mpfr_can_round. 2 3 Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4 Contributed by the Arenaire and Cacao 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 http://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 <stdio.h> 24 #include <stdlib.h> 25 26 #include "mpfr-test.h" 27 28 #define MAX_LIMB_SIZE 100 29 30 static void 31 check_round_p (void) 32 { 33 mp_limb_t buf[MAX_LIMB_SIZE]; 34 mp_size_t n, i; 35 mpfr_prec_t p; 36 mpfr_exp_t err; 37 int r1, r2; 38 39 for (n = 2 ; n <= MAX_LIMB_SIZE ; n++) 40 { 41 /* avoid mpn_random which leaks memory */ 42 for (i = 0; i < n; i++) 43 buf[i] = randlimb (); 44 p = randlimb() % ((n-1) * GMP_NUMB_BITS) + MPFR_PREC_MIN; 45 err = p + randlimb () % GMP_NUMB_BITS; 46 r1 = mpfr_round_p (buf, n, err, p); 47 r2 = mpfr_can_round_raw (buf, n, MPFR_SIGN_POS, err, 48 MPFR_RNDN, MPFR_RNDZ, p); 49 if (r1 != r2) 50 { 51 printf ("mpfr_round_p(%d) != mpfr_can_round(%d)!\n" 52 "bn = %ld, err0 = %ld, prec = %lu\nbp = ", 53 r1, r2, n, err, (unsigned long) p); 54 gmp_printf ("%NX\n", buf, n); 55 exit (1); 56 } 57 } 58 } 59 60 int 61 main (void) 62 { 63 mpfr_t x; 64 mpfr_prec_t i, j; 65 66 tests_start_mpfr (); 67 68 /* checks that rounds to nearest sets the last 69 bit to zero in case of equal distance */ 70 mpfr_init2 (x, 59); 71 mpfr_set_str_binary (x, "-0.10010001010111000011110010111010111110000000111101100111111E663"); 72 if (mpfr_can_round (x, 54, MPFR_RNDZ, MPFR_RNDZ, 53) != 0) 73 { 74 printf ("Error (1) in mpfr_can_round\n"); 75 exit (1); 76 } 77 78 mpfr_set_str_binary (x, "-Inf"); 79 if (mpfr_can_round (x, 2000, MPFR_RNDZ, MPFR_RNDZ, 2000) != 0) 80 { 81 printf ("Error (2) in mpfr_can_round\n"); 82 exit (1); 83 } 84 85 mpfr_set_prec (x, 64); 86 mpfr_set_str_binary (x, "0.1011001000011110000110000110001111101011000010001110011000000000"); 87 if (mpfr_can_round (x, 65, MPFR_RNDN, MPFR_RNDN, 54)) 88 { 89 printf ("Error (3) in mpfr_can_round\n"); 90 exit (1); 91 } 92 93 mpfr_set_prec (x, 137); 94 mpfr_set_str_binary (x, "-0.10111001101001010110011000110100111010011101101010010100101100001110000100111111011101010110001010111100100101110111100001000010000000000E-97"); 95 if (mpfr_can_round (x, 132, MPFR_RNDU, MPFR_RNDZ, 128) == 0) 96 { 97 printf ("Error (4) in mpfr_can_round\n"); 98 exit (1); 99 } 100 101 /* in the following, we can round but cannot determine the inexact flag */ 102 mpfr_set_prec (x, 86); 103 mpfr_set_str_binary (x, "-0.11100100010011001111011010100111101010011000000000000000000000000000000000000000000000E-80"); 104 if (mpfr_can_round (x, 81, MPFR_RNDU, MPFR_RNDZ, 44) == 0) 105 { 106 printf ("Error (5) in mpfr_can_round\n"); 107 exit (1); 108 } 109 110 mpfr_set_prec (x, 62); 111 mpfr_set_str (x, "0.ff4ca619c76ba69", 16, MPFR_RNDZ); 112 for (i = 30; i < 99; i++) 113 for (j = 30; j < 99; j++) 114 { 115 int r1, r2; 116 for (r1 = 0; r1 < MPFR_RND_MAX ; r1++) 117 for (r2 = 0; r2 < MPFR_RND_MAX ; r2++) 118 mpfr_can_round (x, i, (mpfr_rnd_t) r1, (mpfr_rnd_t) r2, j); /* test for assertions */ 119 } 120 121 mpfr_clear (x); 122 123 check_round_p (); 124 125 tests_end_mpfr (); 126 return 0; 127 } 128