new beta-0.90.0
[luatex.git] / source / libs / mpfr / mpfr-src / src / get_z.c
blobe323255137c8f5329803dde786d11e170426b0eb
1 /* mpfr_get_z -- get a multiple-precision integer from
2 a floating-point number
4 Copyright 2004, 2006-2016 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramba 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"
26 int
27 mpfr_get_z (mpz_ptr z, mpfr_srcptr f, mpfr_rnd_t rnd)
29 int inex;
30 mpfr_t r;
31 mpfr_exp_t exp;
33 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
35 if (MPFR_UNLIKELY (MPFR_NOTZERO (f)))
36 MPFR_SET_ERANGE ();
37 mpz_set_ui (z, 0);
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. */
41 return 0;
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
51 representable in r */
52 MPFR_ASSERTN (MPFR_IS_FP (r));
53 exp = mpfr_get_z_2exp (z, r);
54 if (exp >= 0)
55 mpz_mul_2exp (z, z, exp);
56 else
57 mpz_fdiv_q_2exp (z, z, -exp);
58 mpfr_clear (r);
60 return inex;