1 /* Test mpf_trunc, mpf_ceil, mpf_floor.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 check_print (mpf_srcptr src, mpf_srcptr got, mpf_srcptr want)
31 mpf_trace ("src ", src);
32 mpf_trace ("got ", got);
33 mpf_trace ("want", want);
35 printf ("got size=%d exp=%ld\n", SIZ(got), EXP(got));
36 mpn_trace (" limbs=", PTR(got), (mp_size_t) ABSIZ(got));
38 printf ("want size=%d exp=%ld\n", SIZ(want), EXP(want));
39 mpn_trace (" limbs=", PTR(want), (mp_size_t) ABSIZ(want));
43 check_one (mpf_srcptr src, mpf_srcptr trunc, mpf_srcptr ceil, mpf_srcptr floor)
47 mpf_init2 (got, mpf_get_prec (trunc));
48 ASSERT_ALWAYS (PREC(got) == PREC(trunc));
49 ASSERT_ALWAYS (PREC(got) == PREC(ceil));
50 ASSERT_ALWAYS (PREC(got) == PREC(floor));
52 #define CHECK_SEP(name, fun, want) \
53 mpf_set_ui (got, 54321L); /* initial junk */ \
55 MPF_CHECK_FORMAT (got); \
56 if (mpf_cmp (got, want) != 0) \
58 printf ("%s wrong\n", name); \
59 check_print (src, got, want); \
63 CHECK_SEP ("mpf_trunc", mpf_trunc, trunc);
64 CHECK_SEP ("mpf_ceil", mpf_ceil, ceil);
65 CHECK_SEP ("mpf_floor", mpf_floor, floor);
67 #define CHECK_INPLACE(name, fun, want) \
70 MPF_CHECK_FORMAT (got); \
71 if (mpf_cmp (got, want) != 0) \
73 printf ("%s wrong\n", name); \
74 check_print (src, got, want); \
78 CHECK_INPLACE ("mpf_trunc", mpf_trunc, trunc);
80 /* Can't do these unconditionally in case truncation by mpf_set strips
81 some low non-zero limbs which would have rounded the result. */
82 if (ABSIZ(src) <= PREC(trunc)+1)
84 CHECK_INPLACE ("mpf_ceil", mpf_ceil, ceil);
85 CHECK_INPLACE ("mpf_floor", mpf_floor, floor);
92 check_all (mpf_ptr src, mpf_ptr trunc, mpf_ptr ceil, mpf_ptr floor)
94 /* some of these values are generated with direct field assignments */
95 MPF_CHECK_FORMAT (src);
96 MPF_CHECK_FORMAT (trunc);
97 MPF_CHECK_FORMAT (ceil);
98 MPF_CHECK_FORMAT (floor);
100 check_one (src, trunc, ceil, floor);
103 mpf_neg (trunc, trunc);
104 mpf_neg (ceil, ceil);
105 mpf_neg (floor, floor);
106 check_one (src, trunc, floor, ceil);
112 mpf_t src, trunc, ceil, floor;
115 mpf_init2 (src, 512L);
116 mpf_init2 (trunc, 256L);
117 mpf_init2 (ceil, 256L);
118 mpf_init2 (floor, 256L);
121 mpf_set_ui (src, 0L);
122 mpf_set_ui (trunc, 0L);
123 mpf_set_ui (ceil, 0L);
124 mpf_set_ui (floor, 0L);
125 check_all (src, trunc, ceil, floor);
128 mpf_set_ui (src, 1L);
129 mpf_set_ui (trunc, 1L);
130 mpf_set_ui (ceil, 1L);
131 mpf_set_ui (floor, 1L);
132 check_all (src, trunc, ceil, floor);
135 mpf_set_ui (src, 1L);
136 mpf_mul_2exp (src, src, 1024L);
137 mpf_set (trunc, src);
139 mpf_set (floor, src);
140 check_all (src, trunc, ceil, floor);
142 /* 1/2^1024, fraction only */
143 mpf_set_ui (src, 1L);
144 mpf_div_2exp (src, src, 1024L);
145 mpf_set_si (trunc, 0L);
146 mpf_set_si (ceil, 1L);
147 mpf_set_si (floor, 0L);
148 check_all (src, trunc, ceil, floor);
151 mpf_set_ui (src, 1L);
152 mpf_div_2exp (src, src, 1L);
153 mpf_set_si (trunc, 0L);
154 mpf_set_si (ceil, 1L);
155 mpf_set_si (floor, 0L);
156 check_all (src, trunc, ceil, floor);
159 mpf_set_ui (src, 1L);
160 mpf_div_2exp (src, src, 64L);
161 mpf_add_ui (src, src, 123L);
162 mpf_set_si (trunc, 123L);
163 mpf_set_si (ceil, 124L);
164 mpf_set_si (floor, 123L);
165 check_all (src, trunc, ceil, floor);
167 /* integer of full prec+1 limbs, unchanged */
169 ASSERT_ALWAYS (n <= PREC(src)+1);
172 for (i = 0; i < SIZ(src); i++)
174 mpf_set (trunc, src);
176 mpf_set (floor, src);
177 check_all (src, trunc, ceil, floor);
179 /* full prec+1 limbs, 1 trimmed for integer */
181 ASSERT_ALWAYS (n <= PREC(src)+1);
184 for (i = 0; i < SIZ(src); i++)
188 for (i = 0; i < SIZ(trunc); i++)
189 PTR(trunc)[i] = i+201;
190 mpf_set (floor, trunc);
191 mpf_add_ui (ceil, trunc, 1L);
192 check_all (src, trunc, ceil, floor);
194 /* prec+3 limbs, 2 trimmed for size */
196 ASSERT_ALWAYS (n <= PREC(src)+1);
199 for (i = 0; i < SIZ(src); i++)
203 for (i = 0; i < SIZ(trunc); i++)
204 PTR(trunc)[i] = i+302;
205 mpf_set (floor, trunc);
206 mpf_set (ceil, trunc);
208 check_all (src, trunc, ceil, floor);
210 /* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */
212 ASSERT_ALWAYS (n <= PREC(src)+1);
215 for (i = 0; i < SIZ(src); i++)
219 for (i = 0; i < SIZ(trunc); i++)
220 PTR(trunc)[i] = i+403;
221 mpf_set (floor, trunc);
222 mpf_set (ceil, trunc);
224 check_all (src, trunc, ceil, floor);
226 /* F.F, carry out of ceil */
229 PTR(src)[0] = GMP_NUMB_MAX;
230 PTR(src)[1] = GMP_NUMB_MAX;
233 PTR(trunc)[0] = GMP_NUMB_MAX;
234 mpf_set (floor, trunc);
238 check_all (src, trunc, ceil, floor);
240 /* FF.F, carry out of ceil */
243 PTR(src)[0] = GMP_NUMB_MAX;
244 PTR(src)[1] = GMP_NUMB_MAX;
245 PTR(src)[2] = GMP_NUMB_MAX;
248 PTR(trunc)[0] = GMP_NUMB_MAX;
249 PTR(trunc)[1] = GMP_NUMB_MAX;
250 mpf_set (floor, trunc);
254 check_all (src, trunc, ceil, floor);