[BACK]Return to sub.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr

Annotation of OpenXM_contrib/gmp/mpfr/sub.c, Revision 1.1.1.2

1.1       maekawa     1: /* mpfr_sub -- subtract two floating-point numbers
                      2:
1.1.1.2 ! ohara       3: Copyright 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_sub (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:         if (MPFR_SIGN(c) == MPFR_SIGN(a))
        !            58:           MPFR_CHANGE_SIGN(a);
        !            59:         MPFR_RET(0); /* exact */
        !            60:       }
1.1       maekawa    61:
1.1.1.2 ! ohara      62:   MPFR_ASSERTN(MPFR_IS_FP(b) && MPFR_IS_FP(c));
1.1       maekawa    63:
1.1.1.2 ! ohara      64:   if (MPFR_IS_ZERO(b))
        !            65:     {
        !            66:       if (MPFR_IS_ZERO(c))
        !            67:         {
        !            68:           if (MPFR_SIGN(a) !=
        !            69:               (rnd_mode != GMP_RNDD ?
        !            70:                ((MPFR_SIGN(b) < 0 && MPFR_SIGN(c) > 0) ? -1 : 1) :
        !            71:                ((MPFR_SIGN(b) > 0 && MPFR_SIGN(c) < 0) ? 1 : -1)))
        !            72:             MPFR_CHANGE_SIGN(a);
        !            73:           MPFR_CLEAR_INF(a);
        !            74:           MPFR_SET_ZERO(a);
        !            75:           MPFR_RET(0); /* 0 - 0 is exact */
        !            76:         }
        !            77:       return mpfr_neg (a, c, rnd_mode);
1.1       maekawa    78:     }
1.1.1.2 ! ohara      79:
        !            80:   if (MPFR_IS_ZERO(c))
        !            81:     {
        !            82:       return mpfr_set (a, b, rnd_mode);
1.1       maekawa    83:     }
1.1.1.2 ! ohara      84:
        !            85:   MPFR_CLEAR_INF(a);
        !            86:
        !            87:   if (MPFR_SIGN(b) == MPFR_SIGN(c))
        !            88:     { /* signs are equal, it's a real subtraction */
        !            89:       return mpfr_sub1(a, b, c, rnd_mode, 1);
        !            90:     }
        !            91:   else
        !            92:     { /* signs differ, it's an addition */
        !            93:       if (MPFR_EXP(b) < MPFR_EXP(c))
        !            94:         { /* exchange rounding modes towards +/- infinity */
        !            95:           int inexact;
        !            96:           if (rnd_mode == GMP_RNDU)
        !            97:             rnd_mode = GMP_RNDD;
        !            98:           else if (rnd_mode == GMP_RNDD)
        !            99:             rnd_mode = GMP_RNDU;
        !           100:           inexact = mpfr_add1(a, c, b, rnd_mode,
        !           101:                               (mp_exp_unsigned_t) MPFR_EXP(c) - MPFR_EXP(b));
        !           102:           MPFR_CHANGE_SIGN(a);
        !           103:           return -inexact;
        !           104:         }
        !           105:       else
        !           106:         {
        !           107:           return mpfr_add1(a, b, c, rnd_mode,
        !           108:                            (mp_exp_unsigned_t) MPFR_EXP(b) - MPFR_EXP(c));
        !           109:         }
1.1       maekawa   110:     }
                    111: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>