1 /* mpfr_sub -- subtract two floating-point numbers 2 3 Copyright 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4 Contributed by the Arenaire and Caramel projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 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. 12 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. 17 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. */ 22 23 #include "mpfr-impl.h" 24 25 int 26 mpfr_sub (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode) 27 { 28 MPFR_LOG_FUNC 29 (("b[%Pu]=%.*Rg c[%Pu]=%.*Rg rnd=%d", 30 mpfr_get_prec (b), mpfr_log_prec, b, 31 mpfr_get_prec (c), mpfr_log_prec, c, rnd_mode), 32 ("a[%Pu]=%.*Rg", mpfr_get_prec (a), mpfr_log_prec, a)); 33 34 if (MPFR_ARE_SINGULAR (b,c)) 35 { 36 if (MPFR_IS_NAN (b) || MPFR_IS_NAN (c)) 37 { 38 MPFR_SET_NAN (a); 39 MPFR_RET_NAN; 40 } 41 else if (MPFR_IS_INF (b)) 42 { 43 if (!MPFR_IS_INF (c) || MPFR_SIGN (b) != MPFR_SIGN(c)) 44 { 45 MPFR_SET_INF (a); 46 MPFR_SET_SAME_SIGN (a, b); 47 MPFR_RET (0); /* exact */ 48 } 49 else 50 { 51 MPFR_SET_NAN (a); /* Inf - Inf */ 52 MPFR_RET_NAN; 53 } 54 } 55 else if (MPFR_IS_INF (c)) 56 { 57 MPFR_SET_INF (a); 58 MPFR_SET_OPPOSITE_SIGN (a, c); 59 MPFR_RET (0); /* exact */ 60 } 61 else if (MPFR_IS_ZERO (b)) 62 { 63 if (MPFR_IS_ZERO (c)) 64 { 65 int sign = rnd_mode != MPFR_RNDD 66 ? ((MPFR_IS_NEG(b) && MPFR_IS_POS(c)) ? -1 : 1) 67 : ((MPFR_IS_POS(b) && MPFR_IS_NEG(c)) ? 1 : -1); 68 MPFR_SET_SIGN (a, sign); 69 MPFR_SET_ZERO (a); 70 MPFR_RET(0); /* 0 - 0 is exact */ 71 } 72 else 73 return mpfr_neg (a, c, rnd_mode); 74 } 75 else 76 { 77 MPFR_ASSERTD (MPFR_IS_ZERO (c)); 78 return mpfr_set (a, b, rnd_mode); 79 } 80 } 81 82 MPFR_ASSERTD (MPFR_IS_PURE_FP (b)); 83 MPFR_ASSERTD (MPFR_IS_PURE_FP (c)); 84 85 if (MPFR_LIKELY (MPFR_SIGN (b) == MPFR_SIGN (c))) 86 { /* signs are equal, it's a real subtraction */ 87 if (MPFR_LIKELY (MPFR_PREC (a) == MPFR_PREC (b) 88 && MPFR_PREC (b) == MPFR_PREC (c))) 89 return mpfr_sub1sp (a, b, c, rnd_mode); 90 else 91 return mpfr_sub1 (a, b, c, rnd_mode); 92 } 93 else 94 { /* signs differ, it's an addition */ 95 if (MPFR_GET_EXP (b) < MPFR_GET_EXP (c)) 96 { /* exchange rounding modes toward +/- infinity */ 97 int inexact; 98 rnd_mode = MPFR_INVERT_RND (rnd_mode); 99 if (MPFR_LIKELY (MPFR_PREC (a) == MPFR_PREC (b) 100 && MPFR_PREC (b) == MPFR_PREC (c))) 101 inexact = mpfr_add1sp (a, c, b, rnd_mode); 102 else 103 inexact = mpfr_add1 (a, c, b, rnd_mode); 104 MPFR_CHANGE_SIGN (a); 105 return -inexact; 106 } 107 else 108 { 109 if (MPFR_LIKELY (MPFR_PREC (a) == MPFR_PREC (b) 110 && MPFR_PREC (b) == MPFR_PREC (c))) 111 return mpfr_add1sp (a, b, c, rnd_mode); 112 else 113 return mpfr_add1 (a, b, c, rnd_mode); 114 } 115 } 116 } 117