Annotation of OpenXM_contrib/gmp/mpfr/add.c, Revision 1.1.1.2
1.1 maekawa 1: /* mpfr_add -- add two floating-point numbers
2:
1.1.1.2 ! ohara 3: Copyright 1999, 2000, 2001 Free Software Foundation.
! 4: Contributed by the Spaces project, INRIA Lorraine.
1.1 maekawa 5:
6: This file is part of the MPFR Library.
7:
8: The MPFR Library is free software; you can redistribute it and/or modify
1.1.1.2 ! ohara 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
1.1 maekawa 11: option) any later version.
12:
13: The MPFR Library is distributed in the hope that it will be useful, but
14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! ohara 15: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 16: License for more details.
17:
1.1.1.2 ! ohara 18: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 19: along with the MPFR Library; see the file COPYING.LIB. If not, write to
20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21: MA 02111-1307, USA. */
22:
23: #include "gmp.h"
24: #include "gmp-impl.h"
25: #include "mpfr.h"
1.1.1.2 ! ohara 26: #include "mpfr-impl.h"
1.1 maekawa 27:
1.1.1.2 ! ohara 28: int
! 29: mpfr_add (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mp_rnd_t rnd_mode)
1.1 maekawa 30: {
1.1.1.2 ! ohara 31: if (MPFR_IS_NAN(b) || MPFR_IS_NAN(c))
1.1 maekawa 32: {
1.1.1.2 ! ohara 33: MPFR_SET_NAN(a);
! 34: MPFR_RET_NAN;
1.1 maekawa 35: }
36:
1.1.1.2 ! ohara 37: MPFR_CLEAR_NAN(a);
1.1 maekawa 38:
1.1.1.2 ! ohara 39: if (MPFR_IS_INF(b))
! 40: {
! 41: if (!MPFR_IS_INF(c) || MPFR_SIGN(b) == MPFR_SIGN(c))
! 42: {
! 43: MPFR_SET_INF(a);
! 44: MPFR_SET_SAME_SIGN(a, b);
! 45: MPFR_RET(0); /* exact */
! 46: }
! 47: else
! 48: {
! 49: MPFR_SET_NAN(a);
! 50: MPFR_RET_NAN;
! 51: }
! 52: }
! 53: else
! 54: if (MPFR_IS_INF(c))
! 55: {
! 56: MPFR_SET_INF(a);
! 57: MPFR_SET_SAME_SIGN(a, c);
! 58: MPFR_RET(0); /* exact */
1.1 maekawa 59: }
60:
1.1.1.2 ! ohara 61: MPFR_ASSERTN(MPFR_IS_FP(b) && MPFR_IS_FP(c));
1.1 maekawa 62:
1.1.1.2 ! ohara 63: if (MPFR_IS_ZERO(b))
! 64: {
! 65: if (MPFR_IS_ZERO(c))
! 66: {
! 67: if (MPFR_SIGN(a) !=
! 68: (rnd_mode != GMP_RNDD ?
! 69: ((MPFR_SIGN(b) < 0 && MPFR_SIGN(c) < 0) ? -1 : 1) :
! 70: ((MPFR_SIGN(b) > 0 && MPFR_SIGN(c) > 0) ? 1 : -1)))
! 71: {
! 72: MPFR_CHANGE_SIGN(a);
! 73: }
! 74: MPFR_CLEAR_INF(a);
! 75: MPFR_SET_ZERO(a);
! 76: MPFR_RET(0); /* 0 + 0 is exact */
! 77: }
! 78: return mpfr_set(a, c, rnd_mode);
1.1 maekawa 79: }
80:
1.1.1.2 ! ohara 81: if (MPFR_IS_ZERO(c))
! 82: {
! 83: return mpfr_set(a, b, rnd_mode);
1.1 maekawa 84: }
85:
1.1.1.2 ! ohara 86: MPFR_CLEAR_INF(a); /* clear Inf flag */
1.1 maekawa 87:
1.1.1.2 ! ohara 88: if (MPFR_SIGN(b) != MPFR_SIGN(c))
! 89: { /* signs differ, it's a subtraction */
! 90: return mpfr_sub1(a, b, c, rnd_mode, 0);
! 91: }
! 92: else
! 93: { /* signs are equal, it's an addition */
! 94: if (MPFR_EXP(b) < MPFR_EXP(c))
! 95: {
! 96: return mpfr_add1(a, c, b, rnd_mode,
! 97: (mp_exp_unsigned_t) MPFR_EXP(c) - MPFR_EXP(b));
! 98: }
! 99: else
! 100: {
! 101: return mpfr_add1(a, b, c, rnd_mode,
! 102: (mp_exp_unsigned_t) MPFR_EXP(b) - MPFR_EXP(c));
! 103: }
1.1 maekawa 104: }
105: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>