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

Annotation of OpenXM_contrib/gmp/mpfr/sin.c, Revision 1.1.1.1

1.1       ohara       1: /* mpfr_sin -- sine of a floating-point number
                      2:
                      3: Copyright 2001, 2002 Free Software Foundation, Inc.
                      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 "gmp.h"
                     23: #include "gmp-impl.h"
                     24: #include "mpfr.h"
                     25: #include "mpfr-impl.h"
                     26:
                     27: int
                     28: mpfr_sin (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
                     29: {
                     30:   int precy, m, ok, e, inexact, neg;
                     31:   mpfr_t c, k;
                     32:
                     33:   if (MPFR_IS_NAN(x) || MPFR_IS_INF(x))
                     34:     {
                     35:       MPFR_SET_NAN(y);
                     36:       MPFR_RET_NAN;
                     37:     }
                     38:
                     39:   if (MPFR_IS_ZERO(x))
                     40:     {
                     41:       MPFR_CLEAR_FLAGS(y);
                     42:       MPFR_SET_ZERO(y);
                     43:       MPFR_SET_SAME_SIGN(y, x);
                     44:       MPFR_RET(0);
                     45:     }
                     46:
                     47:   precy = MPFR_PREC(y);
                     48:   m = precy + _mpfr_ceil_log2 ((double) precy) + ABS(MPFR_EXP(x)) + 13;
                     49:
                     50:   mpfr_init2 (c, m);
                     51:   mpfr_init2 (k, m);
                     52:
                     53:   /* first determine sign */
                     54:   mpfr_const_pi (c, GMP_RNDN);
                     55:   mpfr_mul_2ui (c, c, 1, GMP_RNDN); /* 2*Pi */
                     56:   mpfr_div (k, x, c, GMP_RNDN);      /* x/(2*Pi) */
                     57:   mpfr_floor (k, k);                 /* floor(x/(2*Pi)) */
                     58:   mpfr_mul (c, k, c, GMP_RNDN);
                     59:   mpfr_sub (k, x, c, GMP_RNDN);      /* 0 <= k < 2*Pi */
                     60:   mpfr_const_pi (c, GMP_RNDN); /* cached */
                     61:   neg = mpfr_cmp (k, c) > 0;
                     62:   mpfr_clear (k);
                     63:
                     64:   do
                     65:     {
                     66:       mpfr_cos (c, x, GMP_RNDZ);
                     67:       mpfr_mul (c, c, c, GMP_RNDU);
                     68:       mpfr_ui_sub (c, 1, c, GMP_RNDN);
                     69:       e = 2 + (-MPFR_EXP(c)) / 2;
                     70:       mpfr_sqrt (c, c, GMP_RNDN);
                     71:       if (neg)
                     72:        mpfr_neg (c, c, GMP_RNDN);
                     73:
                     74:       /* the absolute error on c is at most 2^(e-m) = 2^(EXP(c)-err) */
                     75:       e = MPFR_EXP(c) + m - e;
                     76:       ok = (e >= 0) && mpfr_can_round (c, e, GMP_RNDN, rnd_mode, precy);
                     77:
                     78:       if (ok == 0)
                     79:        {
                     80:          m += BITS_PER_MP_LIMB;
                     81:          mpfr_set_prec (c, m);
                     82:        }
                     83:     }
                     84:   while (!ok);
                     85:
                     86:   inexact = mpfr_set (y, c, rnd_mode);
                     87:
                     88:   mpfr_clear (c);
                     89:
                     90:   return inexact; /* inexact */
                     91: }

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