1 /* mpfr_modf -- Integral and fractional part.
3 Copyright 2007-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 3 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.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 #define INEXPOS(y) ((y) == 0 ? 0 : (((y) > 0) ? 1 : 2))
26 #define INEX(y,z) (INEXPOS(y) | (INEXPOS(z) << 2))
28 /* Set iop to the integral part of op and fop to its fractional part */
30 mpfr_modf (mpfr_ptr iop
, mpfr_ptr fop
, mpfr_srcptr op
, mpfr_rnd_t rnd_mode
)
37 (("op[%Pu]=%.*Rg rnd=%d",
38 mpfr_get_prec (op
), mpfr_log_prec
, op
, rnd_mode
),
39 ("iop[%Pu]=%.*Rg fop[%Pu]=%.*Rg",
40 mpfr_get_prec (iop
), mpfr_log_prec
, iop
,
41 mpfr_get_prec (fop
), mpfr_log_prec
, fop
));
43 MPFR_ASSERTN (iop
!= fop
);
45 if ( MPFR_UNLIKELY (MPFR_IS_SINGULAR (op
)) )
53 MPFR_SET_SAME_SIGN (iop
, op
);
54 MPFR_SET_SAME_SIGN (fop
, op
);
63 MPFR_ASSERTD (MPFR_IS_ZERO (op
));
70 ope
= MPFR_GET_EXP (op
);
73 if (ope
<= 0) /* 0 < |op| < 1 */
75 inexf
= (fop
!= op
) ? mpfr_set (fop
, op
, rnd_mode
) : 0;
76 MPFR_SET_SAME_SIGN (iop
, op
);
78 MPFR_RET (INEX(0, inexf
));
80 else if (ope
>= opq
) /* op has no fractional part */
82 inexi
= (iop
!= op
) ? mpfr_set (iop
, op
, rnd_mode
) : 0;
83 MPFR_SET_SAME_SIGN (fop
, op
);
85 MPFR_RET (INEX(inexi
, 0));
87 else /* op has both integral and fractional parts */
91 inexi
= mpfr_rint_trunc (iop
, op
, rnd_mode
);
92 inexf
= mpfr_frac (fop
, op
, rnd_mode
);
96 MPFR_ASSERTN (fop
!= op
);
97 inexf
= mpfr_frac (fop
, op
, rnd_mode
);
98 inexi
= mpfr_rint_trunc (iop
, op
, rnd_mode
);
100 MPFR_RET (INEX(inexi
, inexf
));