PIPE - Fix a panic introduced by the last commit.
[dragonfly.git] / contrib / mpfr / modf.c
blobe7a649870de3153610b0785a7601433ad1a2ab6b
1 /* mpfr_modf -- Integral and fractional part.
3 Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 /* Set iop to the integral part of op and fop to its fractional part */
26 int
27 mpfr_modf (mpfr_ptr iop, mpfr_ptr fop, mpfr_srcptr op, mpfr_rnd_t rnd_mode)
29 mp_exp_t ope;
30 mp_prec_t opq;
31 int inexact;
32 MPFR_SAVE_EXPO_DECL (expo);
34 MPFR_LOG_FUNC (("op[%#R]=%R rnd=%d", op, op, rnd_mode),
35 ("iop[%#R]=%R fop[%#R]=%R", iop, iop, fop, fop));
37 MPFR_ASSERTN (iop != fop);
39 if ( MPFR_UNLIKELY (MPFR_IS_SINGULAR (op)) )
41 if (MPFR_IS_NAN (op))
43 MPFR_SET_NAN (iop);
44 MPFR_SET_NAN (fop);
45 MPFR_RET_NAN;
47 MPFR_SET_SAME_SIGN (iop, op);
48 MPFR_SET_SAME_SIGN (fop, op);
49 if (MPFR_IS_INF (op))
51 MPFR_SET_INF (iop);
52 MPFR_SET_ZERO (fop);
53 MPFR_RET (0);
55 else /* op is zero */
57 MPFR_ASSERTD (MPFR_IS_ZERO (op));
58 MPFR_SET_ZERO (iop);
59 MPFR_SET_ZERO (fop);
60 MPFR_RET (0);
64 MPFR_SAVE_EXPO_MARK (expo);
66 ope = MPFR_GET_EXP (op);
67 opq = MPFR_PREC (op);
69 if (ope <=0) /* 0 < |op| < 1 */
71 inexact = (fop != op) ? mpfr_set (fop, op, rnd_mode) : 0;
72 MPFR_SET_SAME_SIGN (iop, op);
73 MPFR_SET_ZERO (iop);
74 MPFR_SAVE_EXPO_FREE (expo);
75 mpfr_check_range (fop, inexact, rnd_mode); /* set the underflow flag if needed */
76 MPFR_RET (MPFR_INT_SIGN (op) > 0 ? -2 : +2);
78 else if (ope >= opq) /* op has no fractional part */
80 inexact = (iop != op)? mpfr_set (iop, op, rnd_mode) : 0;
81 MPFR_SET_SAME_SIGN (fop, op);
82 MPFR_SET_ZERO (fop);
83 MPFR_SAVE_EXPO_FREE (expo);
84 return mpfr_check_range (iop, inexact, rnd_mode); /* set the overflow flag if needed */
86 else /* op has both integral and fractional parts */
88 int inexi, inexf;
89 mpfr_t opf, opi;
91 /* opi and opf are set with minimal but sufficient precision */
92 mpfr_init2 (opi, ope <= MPFR_PREC_MIN ? MPFR_PREC_MIN : ope);
93 inexi = mpfr_trunc (opi, op);
94 mpfr_init2 (opf, opq - ope <= MPFR_PREC_MIN ? MPFR_PREC_MIN : opq - ope);
95 inexf = mpfr_frac (opf, op, GMP_RNDZ);
96 MPFR_ASSERTD (inexf == 0);
98 inexf = mpfr_set (fop, opf, rnd_mode);
99 inexi = mpfr_set (iop, opi, rnd_mode);
100 mpfr_clear (opi);
101 mpfr_clear (opf);
103 MPFR_SAVE_EXPO_FREE (expo);
104 inexf = mpfr_check_range (fop, inexf, rnd_mode);
105 inexi = mpfr_check_range (iop, inexi, rnd_mode);
107 /* return value like mpfr_trunc(): */
108 /* 0 iff iop and fop are exact */
109 /* -1 if op is an integer, op > iop */
110 /* +1 if op is an integer, op < iop */
111 /* -2 if op is not an integer, op > 0 */
112 /* +2 if op is not an integer, op < 0 */
113 inexact = inexf ? (inexi ? 2 * inexi : -2 * MPFR_INT_SIGN (op)) :
114 (mpfr_zero_p (fop) ? inexi : 2 * inexi);
115 MPFR_RET (inexact);