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 2.1 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; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
30
check_print (mpf_srcptr src, mpf_srcptr got, mpf_srcptr want)
33
mpf_trace ("src ", src);
34
mpf_trace ("got ", got);
35
mpf_trace ("want", want);
37
printf ("got size=%d exp=%ld\n", SIZ(got), EXP(got));
38
mpn_trace (" limbs=", PTR(got), ABSIZ(got));
40
printf ("want size=%d exp=%ld\n", SIZ(want), EXP(want));
41
mpn_trace (" limbs=", PTR(want), ABSIZ(want));
45
check_one (mpf_srcptr src, mpf_srcptr trunc, mpf_srcptr ceil, mpf_srcptr floor)
49
mpf_init2 (got, mpf_get_prec (trunc));
50
ASSERT_ALWAYS (PREC(got) == PREC(trunc));
51
ASSERT_ALWAYS (PREC(got) == PREC(ceil));
52
ASSERT_ALWAYS (PREC(got) == PREC(floor));
54
#define CHECK_SEP(name, fun, want) \
55
mpf_set_ui (got, 54321L); /* initial junk */ \
57
MPF_CHECK_FORMAT (got); \
58
if (mpf_cmp (got, want) != 0) \
60
printf ("%s wrong\n", name); \
61
check_print (src, got, want); \
65
CHECK_SEP ("mpf_trunc", mpf_trunc, trunc);
66
CHECK_SEP ("mpf_ceil", mpf_ceil, ceil);
67
CHECK_SEP ("mpf_floor", mpf_floor, floor);
69
#define CHECK_INPLACE(name, fun, want) \
72
MPF_CHECK_FORMAT (got); \
73
if (mpf_cmp (got, want) != 0) \
75
printf ("%s wrong\n", name); \
76
check_print (src, got, want); \
80
CHECK_INPLACE ("mpf_trunc", mpf_trunc, trunc);
82
/* Can't do these unconditionally in case truncation by mpf_set strips
83
some low non-zero limbs which would have rounded the result. */
84
if (ABSIZ(src) <= PREC(trunc)+1)
86
CHECK_INPLACE ("mpf_ceil", mpf_ceil, ceil);
87
CHECK_INPLACE ("mpf_floor", mpf_floor, floor);
94
check_all (mpf_ptr src, mpf_ptr trunc, mpf_ptr ceil, mpf_ptr floor)
96
/* some of these values are generated with direct field assignments */
97
MPF_CHECK_FORMAT (src);
98
MPF_CHECK_FORMAT (trunc);
99
MPF_CHECK_FORMAT (ceil);
100
MPF_CHECK_FORMAT (floor);
102
check_one (src, trunc, ceil, floor);
105
mpf_neg (trunc, trunc);
106
mpf_neg (ceil, ceil);
107
mpf_neg (floor, floor);
108
check_one (src, trunc, floor, ceil);
114
mpf_t src, trunc, ceil, floor;
117
mpf_init2 (src, 512L);
118
mpf_init2 (trunc, 256L);
119
mpf_init2 (ceil, 256L);
120
mpf_init2 (floor, 256L);
123
mpf_set_ui (src, 0L);
124
mpf_set_ui (trunc, 0L);
125
mpf_set_ui (ceil, 0L);
126
mpf_set_ui (floor, 0L);
127
check_all (src, trunc, ceil, floor);
130
mpf_set_ui (src, 1L);
131
mpf_set_ui (trunc, 1L);
132
mpf_set_ui (ceil, 1L);
133
mpf_set_ui (floor, 1L);
134
check_all (src, trunc, ceil, floor);
137
mpf_set_ui (src, 1L);
138
mpf_mul_2exp (src, src, 1024L);
139
mpf_set (trunc, src);
141
mpf_set (floor, src);
142
check_all (src, trunc, ceil, floor);
144
/* 1/2^1024, fraction only */
145
mpf_set_ui (src, 1L);
146
mpf_div_2exp (src, src, 1024L);
147
mpf_set_si (trunc, 0L);
148
mpf_set_si (ceil, 1L);
149
mpf_set_si (floor, 0L);
150
check_all (src, trunc, ceil, floor);
153
mpf_set_ui (src, 1L);
154
mpf_div_2exp (src, src, 1L);
155
mpf_set_si (trunc, 0L);
156
mpf_set_si (ceil, 1L);
157
mpf_set_si (floor, 0L);
158
check_all (src, trunc, ceil, floor);
161
mpf_set_ui (src, 1L);
162
mpf_div_2exp (src, src, 64L);
163
mpf_add_ui (src, src, 123L);
164
mpf_set_si (trunc, 123L);
165
mpf_set_si (ceil, 124L);
166
mpf_set_si (floor, 123L);
167
check_all (src, trunc, ceil, floor);
169
/* integer of full prec+1 limbs, unchanged */
171
ASSERT_ALWAYS (n <= PREC(src)+1);
174
for (i = 0; i < SIZ(src); i++)
176
mpf_set (trunc, src);
178
mpf_set (floor, src);
179
check_all (src, trunc, ceil, floor);
181
/* full prec+1 limbs, 1 trimmed for integer */
183
ASSERT_ALWAYS (n <= PREC(src)+1);
186
for (i = 0; i < SIZ(src); i++)
190
for (i = 0; i < SIZ(trunc); i++)
191
PTR(trunc)[i] = i+201;
192
mpf_set (floor, trunc);
193
mpf_add_ui (ceil, trunc, 1L);
194
check_all (src, trunc, ceil, floor);
196
/* prec+3 limbs, 2 trimmed for size */
198
ASSERT_ALWAYS (n <= PREC(src)+1);
201
for (i = 0; i < SIZ(src); i++)
205
for (i = 0; i < SIZ(trunc); i++)
206
PTR(trunc)[i] = i+302;
207
mpf_set (floor, trunc);
208
mpf_set (ceil, trunc);
210
check_all (src, trunc, ceil, floor);
212
/* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */
214
ASSERT_ALWAYS (n <= PREC(src)+1);
217
for (i = 0; i < SIZ(src); i++)
221
for (i = 0; i < SIZ(trunc); i++)
222
PTR(trunc)[i] = i+403;
223
mpf_set (floor, trunc);
224
mpf_set (ceil, trunc);
226
check_all (src, trunc, ceil, floor);
228
/* F.F, carry out of ceil */
231
PTR(src)[0] = GMP_NUMB_MAX;
232
PTR(src)[1] = GMP_NUMB_MAX;
235
PTR(trunc)[0] = GMP_NUMB_MAX;
236
mpf_set (floor, trunc);
240
check_all (src, trunc, ceil, floor);
242
/* FF.F, carry out of ceil */
245
PTR(src)[0] = GMP_NUMB_MAX;
246
PTR(src)[1] = GMP_NUMB_MAX;
247
PTR(src)[2] = GMP_NUMB_MAX;
250
PTR(trunc)[0] = GMP_NUMB_MAX;
251
PTR(trunc)[1] = GMP_NUMB_MAX;
252
mpf_set (floor, trunc);
256
check_all (src, trunc, ceil, floor);