1/* Test file for mpfr_prec_round. 2 3Copyright 1999, 2000, 2001, 2002, 2003, 2004, 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#include <stdio.h> 24#include <stdlib.h> 25 26#include "mpfr-test.h" 27 28int 29main (void) 30{ 31 mpfr_t x; 32 mpfr_exp_t emax; 33 34 tests_start_mpfr (); 35 36 mpfr_init (x); 37 38 mpfr_set_nan (x); 39 mpfr_prec_round (x, 2, MPFR_RNDN); 40 MPFR_ASSERTN(mpfr_nan_p (x)); 41 42 mpfr_set_inf (x, 1); 43 mpfr_prec_round (x, 2, MPFR_RNDN); 44 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); 45 46 mpfr_set_inf (x, -1); 47 mpfr_prec_round (x, 2, MPFR_RNDN); 48 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) < 0); 49 50 mpfr_set_ui (x, 0, MPFR_RNDN); 51 mpfr_prec_round (x, 2, MPFR_RNDN); 52 MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x)); 53 54 mpfr_set_ui (x, 0, MPFR_RNDN); 55 mpfr_neg (x, x, MPFR_RNDN); 56 mpfr_prec_round (x, 2, MPFR_RNDN); 57 MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG(x)); 58 59 emax = mpfr_get_emax (); 60 set_emax (0); 61 mpfr_set_prec (x, 3); 62 mpfr_set_str_binary (x, "0.111"); 63 mpfr_prec_round (x, 2, MPFR_RNDN); 64 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); 65 set_emax (emax); 66 67 mpfr_set_prec (x, mp_bits_per_limb + 2); 68 mpfr_set_ui (x, 1, MPFR_RNDN); 69 mpfr_nextbelow (x); 70 mpfr_prec_round (x, mp_bits_per_limb + 1, MPFR_RNDN); 71 MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0); 72 73 mpfr_set_prec (x, 3); 74 mpfr_set_ui (x, 5, MPFR_RNDN); 75 mpfr_prec_round (x, 2, MPFR_RNDN); 76 if (mpfr_cmp_ui(x, 4)) 77 { 78 printf ("Error in tround: got "); 79 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); 80 printf (" instead of 4\n"); 81 exit (1); 82 } 83 84 /* check case when reallocation is needed */ 85 mpfr_set_prec (x, 3); 86 mpfr_set_ui (x, 5, MPFR_RNDN); /* exact */ 87 mpfr_prec_round (x, mp_bits_per_limb + 1, MPFR_RNDN); 88 if (mpfr_cmp_ui(x, 5)) 89 { 90 printf ("Error in tround: got "); 91 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); 92 printf (" instead of 5\n"); 93 exit (1); 94 } 95 96 mpfr_clear(x); 97 mpfr_init2 (x, 3); 98 mpfr_set_si (x, -5, MPFR_RNDN); /* exact */ 99 mpfr_prec_round (x, mp_bits_per_limb + 1, MPFR_RNDN); 100 if (mpfr_cmp_si(x, -5)) 101 { 102 printf ("Error in tround: got "); 103 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); 104 printf (" instead of -5\n"); 105 exit (1); 106 } 107 108 /* check case when new precision needs less limbs */ 109 mpfr_set_prec (x, mp_bits_per_limb + 1); 110 mpfr_set_ui (x, 5, MPFR_RNDN); /* exact */ 111 mpfr_prec_round (x, 3, MPFR_RNDN); /* exact */ 112 if (mpfr_cmp_ui(x, 5)) 113 { 114 printf ("Error in tround: got "); 115 mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); 116 printf (" instead of 5\n"); 117 exit (1); 118 } 119 120 mpfr_clear(x); 121 122 tests_end_mpfr (); 123 return 0; 124} 125