Annotation of OpenXM_contrib/gmp/mpfr/cosh.c, Revision 1.1
1.1 ! ohara 1: /* mpfr_cosh -- hyperbolic cosine
! 2:
! 3: Copyright 2001, 2002 Free Software Foundation.
! 4:
! 5: This file is part of the MPFR Library.
! 6:
! 7: The MPFR Library is free software; you can redistribute it and/or modify
! 8: it under the terms of the GNU Lesser General Public License as published by
! 9: the Free Software Foundation; either version 2.1 of the License, or (at your
! 10: option) any later version.
! 11:
! 12: The MPFR Library is distributed in the hope that it will be useful, but
! 13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! 15: License for more details.
! 16:
! 17: You should have received a copy of the GNU Lesser General Public License
! 18: along with the MPFR Library; see the file COPYING.LIB. If not, write to
! 19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 20: MA 02111-1307, USA. */
! 21:
! 22: #include <stdio.h>
! 23: #include "gmp.h"
! 24: #include "gmp-impl.h"
! 25: #include "mpfr.h"
! 26: #include "mpfr-impl.h"
! 27:
! 28: /* The computation of cosh is done by
! 29:
! 30: cosh= 1/2[e^(x)+e^(-x)]
! 31: */
! 32:
! 33: int
! 34: mpfr_cosh (mpfr_ptr y, mpfr_srcptr xt , mp_rnd_t rnd_mode)
! 35: {
! 36:
! 37: /****** Declaration ******/
! 38: mpfr_t x;
! 39: mp_prec_t Nxt = MPFR_PREC(xt);
! 40: int inexact =0;
! 41:
! 42: if (MPFR_IS_NAN(xt))
! 43: {
! 44: MPFR_SET_NAN(y);
! 45: MPFR_RET_NAN;
! 46: }
! 47: MPFR_CLEAR_NAN(y);
! 48:
! 49: if (MPFR_IS_INF(xt))
! 50: {
! 51: MPFR_SET_INF(y);
! 52: if (MPFR_SIGN(y) < 0)
! 53: MPFR_CHANGE_SIGN(y);
! 54: MPFR_RET(0);
! 55: }
! 56:
! 57: MPFR_CLEAR_INF(y);
! 58:
! 59: if (MPFR_IS_ZERO(xt))
! 60: return mpfr_set_ui(y,1,rnd_mode); /* cosh(0) = 1 */
! 61:
! 62: mpfr_init2(x,Nxt);
! 63: mpfr_set4(x, xt, GMP_RNDN, 1);
! 64:
! 65: /* General case */
! 66: {
! 67: /* Declaration of the intermediary variable */
! 68: mpfr_t t, te,ti;
! 69:
! 70: /* Declaration of the size variable */
! 71: mp_prec_t Nx = Nxt; /* Precision of input variable */
! 72: mp_prec_t Ny = MPFR_PREC(y); /* Precision of input variable */
! 73:
! 74: mp_prec_t Nt; /* Precision of the intermediary variable */
! 75: long int err; /* Precision of error */
! 76:
! 77: /* compute the precision of intermediary variable */
! 78: Nt=MAX(Nx,Ny);
! 79: /* the optimal number of bits : see algorithms.ps */
! 80: Nt=Nt+3+_mpfr_ceil_log2(Nt);
! 81:
! 82:
! 83: /* initialise of intermediary variable */
! 84: mpfr_init(t);
! 85: mpfr_init(te);
! 86: mpfr_init(ti);
! 87:
! 88:
! 89: /* First computation of cosh */
! 90: do {
! 91:
! 92: /* reactualisation of the precision */
! 93:
! 94: mpfr_set_prec(t,Nt);
! 95: mpfr_set_prec(te,Nt);
! 96: mpfr_set_prec(ti,Nt);
! 97:
! 98: /* compute cosh */
! 99: mpfr_exp(te,x,GMP_RNDD); /* exp(x) */
! 100: mpfr_ui_div(ti,1,te,GMP_RNDU); /* 1/exp(x) */
! 101: mpfr_add(t,te,ti,GMP_RNDN); /* exp(x) + 1/exp(x)*/
! 102: mpfr_div_2ui(t,t,1,GMP_RNDN); /* 1/2(exp(x) + 1/exp(x))*/
! 103:
! 104: /* estimation of the error */
! 105: err=Nt-3;
! 106:
! 107: /* actualisation of the precision */
! 108: Nt += 10;
! 109:
! 110: } while ((err <0) || !mpfr_can_round(t,err,GMP_RNDN,rnd_mode,Ny));
! 111:
! 112: inexact = mpfr_set(y,t,rnd_mode);
! 113:
! 114: mpfr_clear(t);
! 115: mpfr_clear(ti);
! 116: mpfr_clear(te);
! 117: }
! 118: mpfr_clear(x);
! 119: return inexact;
! 120:
! 121: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>