1 /* mpfr_hypot -- Euclidean distance
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 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 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* The computation of hypot of x and y is done by *
27 * hypot(x,y)= sqrt(x^2+y^2) = z */
30 mpfr_hypot (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y, mp_rnd_t rnd_mode)
33 mpfr_t t, te, ti; /* auxiliary variables */
34 mp_prec_t N, Nz; /* size variables */
35 mp_prec_t Nt; /* precision of the intermediary variable */
38 mp_exp_unsigned_t diff_exp;
40 MPFR_SAVE_EXPO_DECL (expo);
42 MPFR_BLOCK_DECL (flags);
44 /* particular cases */
45 if (MPFR_ARE_SINGULAR (x, y))
47 if (MPFR_IS_INF (x) || MPFR_IS_INF (y))
49 /* Return +inf, even when the other number is NaN. */
54 else if (MPFR_IS_NAN (x) || MPFR_IS_NAN (y))
59 else if (MPFR_IS_ZERO (x))
60 return mpfr_abs (z, y, rnd_mode);
61 else /* y is necessarily 0 */
62 return mpfr_abs (z, x, rnd_mode);
66 if (mpfr_cmpabs (x, y) < 0)
76 Ex = MPFR_GET_EXP (x);
77 diff_exp = (mp_exp_unsigned_t) Ex - MPFR_GET_EXP (y);
79 N = MPFR_PREC (x); /* Precision of input variable */
80 Nz = MPFR_PREC (z); /* Precision of output variable */
81 threshold = (MAX (N, Nz) + (rnd_mode == GMP_RNDN ? 1 : 0)) << 1;
83 /* Is |x| a suitable approximation to the precision Nz ?
84 (see algorithms.tex for explanations) */
85 if (diff_exp > threshold)
86 /* result is |x| or |x|+ulp(|x|,Nz) */
88 if (MPFR_UNLIKELY (rnd_mode == GMP_RNDU))
90 /* If z > abs(x), then it was already rounded up; otherwise
91 z = abs(x), and we need to add one ulp due to y. */
92 if (mpfr_abs (z, x, rnd_mode) == 0)
96 else /* GMP_RNDZ, GMP_RNDD, GMP_RNDN */
98 if (MPFR_LIKELY (Nz >= N))
100 mpfr_abs (z, x, rnd_mode); /* exact */
105 MPFR_SET_EXP (z, Ex);
106 MPFR_SET_SIGN (z, 1);
107 MPFR_RNDRAW_GEN (inexact, z, MPFR_MANT (x), N, rnd_mode, 1,
109 if (MPFR_UNLIKELY (++ MPFR_EXP (z) >
111 return mpfr_overflow (z, rnd_mode, 1);
114 if (MPFR_UNLIKELY (inexact == 0))
123 N = MAX (MPFR_PREC (x), MPFR_PREC (y));
125 /* working precision */
126 Nt = Nz + MPFR_INT_CEIL_LOG2 (Nz) + 4;
132 MPFR_SAVE_EXPO_MARK (expo);
134 /* Scale x and y to avoid overflow/underflow in x^2 and overflow in y^2
135 (as |x| >= |y|). The scaling of y can underflow only when the target
136 precision is huge, otherwise the case would already have been handled
137 by the diff_exp > threshold code. */
138 sh = mpfr_get_emax () / 2 - Ex - 1;
140 MPFR_ZIV_INIT (loop, Nt);
145 exact = mpfr_mul_2si (te, x, sh, GMP_RNDZ);
146 exact |= mpfr_mul_2si (ti, y, sh, GMP_RNDZ);
147 exact |= mpfr_sqr (te, te, GMP_RNDZ);
148 /* Use fma in order to avoid underflow when diff_exp<=MPFR_EMAX_MAX-2 */
149 exact |= mpfr_fma (t, ti, ti, te, GMP_RNDZ);
150 exact |= mpfr_sqrt (t, t, GMP_RNDZ);
152 err = Nt < N ? 4 : 2;
153 if (MPFR_LIKELY (exact == 0
154 || MPFR_CAN_ROUND (t, Nt-err, Nz, rnd_mode)))
157 MPFR_ZIV_NEXT (loop, Nt);
158 mpfr_set_prec (t, Nt);
159 mpfr_set_prec (te, Nt);
160 mpfr_set_prec (ti, Nt);
162 MPFR_ZIV_FREE (loop);
164 MPFR_BLOCK (flags, inexact = mpfr_div_2si (z, t, sh, rnd_mode));
165 MPFR_ASSERTD (exact == 0 || inexact != 0);
173 0 0 result is exact, ternary flag is 0
174 0 non zero t is exact, ternary flag given by inexact
175 1 0 impossible (see above)
176 1 non zero ternary flag given by inexact
179 MPFR_SAVE_EXPO_FREE (expo);
181 if (MPFR_OVERFLOW (flags))
182 mpfr_set_overflow ();
183 /* hypot(x,y) >= |x|, thus underflow is not possible. */
185 return mpfr_check_range (z, inexact, rnd_mode);