1 /* mpf_ceil, mpf_floor -- round an mpf to an integer.
3 Copyright 2001, 2004 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/. */
24 /* dir==1 for ceil, dir==-1 for floor
26 Notice the use of prec+1 ensures mpf_ceil and mpf_floor are equivalent to
27 mpf_set if u is already an integer. */
29 static void __gmpf_ceil_or_floor
__GMP_PROTO ((REGPARM_2_1 (mpf_ptr
, mpf_srcptr
, int))) REGPARM_ATTR (1);
30 #define mpf_ceil_or_floor(r,u,dir) __gmpf_ceil_or_floor (REGPARM_2_1 (r, u, dir))
32 REGPARM_ATTR (1) static void
33 mpf_ceil_or_floor (mpf_ptr r
, mpf_srcptr u
, int dir
)
36 mp_size_t size
, asize
, prec
;
52 /* u is only a fraction */
66 /* skip fraction part of u */
67 asize
= MIN (asize
, exp
);
69 /* don't lose precision in the copy */
72 /* skip excess over target precision */
73 asize
= MIN (asize
, prec
);
77 if ((size
^ dir
) >= 0)
79 /* rounding direction matches sign, must increment if ignored part is
81 for (p
= PTR(u
); p
!= up
; p
++)
85 if (mpn_add_1 (rp
, up
, asize
, CNST_LIMB(1)))
87 /* was all 0xFF..FFs, which have become zeros, giving just
93 SIZ(r
) = (size
>= 0 ? asize
: -asize
);
99 SIZ(r
) = (size
>= 0 ? asize
: -asize
);
101 MPN_COPY_INCR (rp
, up
, asize
);
106 mpf_ceil (mpf_ptr r
, mpf_srcptr u
)
108 mpf_ceil_or_floor (r
, u
, 1);
112 mpf_floor (mpf_ptr r
, mpf_srcptr u
)
114 mpf_ceil_or_floor (r
, u
, -1);