1 /* mpfr_get_z -- get a multiple-precision integer from
2 a floating-point number
4 Copyright 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by the Arenaire and Caramel projects, INRIA.
7 This file is part of the GNU MPFR Library.
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "mpfr-impl.h"
27 mpfr_get_z (mpz_ptr z, mpfr_srcptr f, mpfr_rnd_t rnd)
33 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
35 if (MPFR_UNLIKELY (MPFR_NOTZERO (f)))
38 /* The ternary value is 0 even for infinity. Giving the rounding
39 direction in this case would not make much sense anyway, and
40 the direction would not necessarily match rnd. */
44 exp = MPFR_GET_EXP (f);
45 /* if exp <= 0, then |f|<1, thus |o(f)|<=1 */
46 MPFR_ASSERTN (exp < 0 || exp <= MPFR_PREC_MAX);
47 mpfr_init2 (r, (exp < (mpfr_exp_t) MPFR_PREC_MIN ?
48 MPFR_PREC_MIN : (mpfr_prec_t) exp));
49 inex = mpfr_rint (r, f, rnd);
50 MPFR_ASSERTN (inex != 1 && inex != -1); /* integral part of f is
52 MPFR_ASSERTN (MPFR_IS_FP (r));
53 exp = mpfr_get_z_2exp (z, r);
55 mpz_mul_2exp (z, z, exp);
57 mpz_fdiv_q_2exp (z, z, -exp);