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 */
27 mpfr_modf (mpfr_ptr iop
, mpfr_ptr fop
, mpfr_srcptr op
, mpfr_rnd_t rnd_mode
)
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
)) )
47 MPFR_SET_SAME_SIGN (iop
, op
);
48 MPFR_SET_SAME_SIGN (fop
, op
);
57 MPFR_ASSERTD (MPFR_IS_ZERO (op
));
64 MPFR_SAVE_EXPO_MARK (expo
);
66 ope
= MPFR_GET_EXP (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
);
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
);
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 */
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
);
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
);