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