diff options
author | daney <daney@280ebfd0-de03-0410-8827-d642c229c3f4> | 2001-10-16 15:09:29 +0000 |
---|---|---|
committer | daney <daney@280ebfd0-de03-0410-8827-d642c229c3f4> | 2001-10-16 15:09:29 +0000 |
commit | 13ec00da14ed9a9978ab5e9a62a0f9692db1c62a (patch) | |
tree | 31e19d529b3019768875505d96c050177b61f305 /hypot.c | |
parent | 40e57c595f9ad089bb61ba7e9c8d9050d17e9885 (diff) | |
download | mpfr-13ec00da14ed9a9978ab5e9a62a0f9692db1c62a.tar.gz |
ajout hypot
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@1266 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'hypot.c')
-rw-r--r-- | hypot.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/hypot.c b/hypot.c new file mode 100644 index 000000000..e68a657ae --- /dev/null +++ b/hypot.c @@ -0,0 +1,131 @@ +/* mpfr_hypot -- Euclidean distance + +Copyright (C) 1999 Free Software Foundation. + +This file is part of the MPFR Library. + +The MPFR Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Library General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at your +option) any later version. + +The MPFR Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public +License for more details. + +You should have received a copy of the GNU Library General Public License +along with the MPFR Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "mpfr.h" +#include "mpfr-impl.h" + + /* The computation of hypot of x and y is done by + + hypot(x,y)= sqrt(x^2+y^2) = z + */ +int mpfr_hypot _PROTO((mpfr_ptr, mpfr_srcptr,mpfr_srcptr, mp_rnd_t)); + +int +#if __STDC__ +mpfr_hypot (mpfr_ptr z, mpfr_srcptr x ,mpfr_srcptr y , mp_rnd_t rnd_mode) +#else +mpfr_hypot (z, x,y, rnd_mode) + mpfr_ptr z; + mpfr_srcptr x; + mpfr_srcptr y; + mp_rnd_t rnd_mode; +#endif +{ + + /* particular cases */ + + if (MPFR_IS_NAN(x) || MPFR_IS_NAN(y)) { MPFR_SET_NAN(z); return 1; } + MPFR_CLEAR_NAN(z); + + if (MPFR_IS_INF(x) || MPFR_IS_INF(y)){ + MPFR_SET_INF(z); + if (MPFR_SIGN(z) < 0) MPFR_CHANGE_SIGN(z); + return 1; + } + + MPFR_CLEAR_INF(z); + + if(!MPFR_NOTZERO(x)){ + mpfr_set(z,y,rnd_mode); + if (MPFR_SIGN(z) < 0) MPFR_CHANGE_SIGN(z); + return 1; + } + if(!MPFR_NOTZERO(y)){ + mpfr_set(z,x,rnd_mode); + if (MPFR_SIGN(z) < 0) MPFR_CHANGE_SIGN(z); + return 1; + } + + + /* General case */ + + { + /* Declaration of the intermediary variable */ + mpfr_t t, te,ti; + + /* Flag calcul exacte */ + int exact=0; + + /* Declaration of the size variable */ + mp_prec_t Nx = MPFR_PREC(x); /* Precision of input variable */ + mp_prec_t Ny = MPFR_PREC(y); /* Precision of input variable */ + mp_prec_t Nz = MPFR_PREC(z); /* Precision of input variable */ + + mp_prec_t Nt; /* Precision of the intermediary variable */ + mp_prec_t err; /* Precision of error */ + + /* compute the precision of intermediary variable */ + Nt=MAX(MAX(Nx,Ny),Nz); + + /* compute the size of intermediary variable -- see algorithms.ps */ + Nt=Nt+2+_mpfr_ceil_log2(Nt); + + /* initialise the intermediary variables */ + mpfr_init(t); + mpfr_init(te); + mpfr_init(ti); + + /* Hypot */ + do { + /* reactualisation of the precision */ + mpfr_set_prec(t,Nt); + mpfr_set_prec(te,Nt); + mpfr_set_prec(ti,Nt); + + /* computations of hypot */ + if(abs(mpfr_mul(te,x,x,GMP_RNDN))) /* x^2 */ + exact=1; + if(abs(mpfr_mul(ti,y,y,GMP_RNDN))) /* y^2 */ + exact=1; + if(abs(mpfr_add(t,te,ti,GMP_RNDD))) /* x^2+y^2*/ + exact=1; + if(abs(mpfr_sqrt(t,t,GMP_RNDN))) /* sqrt(x^2+y^2)*/ + exact=1; + + /* estimation of the error */ + err=Nt-(2); + + Nt += 10; + + } while ((mpfr_can_round(t,err,GMP_RNDN,rnd_mode,Nz)!=1) && exact); + + mpfr_set(z,t,rnd_mode); + mpfr_clear(t); + mpfr_clear(ti); + mpfr_clear(te); + + } + return 1; +} |