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

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

1.1       ohara       1: /* mpfr_sin_cos -- sine and cosine of a floating-point number
                      2:
                      3: Copyright 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: /* (y, z) <- (sin(x), cos(x)), return value is 0 iff both results are exact */
                     28: int
                     29: mpfr_sin_cos (mpfr_ptr y, mpfr_ptr z, mpfr_srcptr x, mp_rnd_t rnd_mode)
                     30: {
                     31:   int prec, m, ok, e, inexact, neg;
                     32:   mpfr_t c, k;
                     33:
                     34:   if (MPFR_IS_NAN(x) || MPFR_IS_INF(x))
                     35:     {
                     36:       MPFR_SET_NAN(y);
                     37:       MPFR_SET_NAN(z);
                     38:       MPFR_RET_NAN;
                     39:     }
                     40:
                     41:   if (MPFR_IS_ZERO(x))
                     42:     {
                     43:       MPFR_CLEAR_FLAGS(y);
                     44:       MPFR_SET_ZERO(y);
                     45:       MPFR_SET_SAME_SIGN(y, x);
                     46:       mpfr_set_ui (z, 1, GMP_RNDN);
                     47:       MPFR_RET(0);
                     48:     }
                     49:
                     50:   prec = MAX(MPFR_PREC(y), MPFR_PREC(z));
                     51:   m = prec + _mpfr_ceil_log2 ((double) prec) + ABS(MPFR_EXP(x)) + 13;
                     52:
                     53:   mpfr_init2 (c, m);
                     54:   mpfr_init2 (k, m);
                     55:
                     56:   /* first determine sign */
                     57:   mpfr_const_pi (c, GMP_RNDN);
                     58:   mpfr_mul_2ui (c, c, 1, GMP_RNDN); /* 2*Pi */
                     59:   mpfr_div (k, x, c, GMP_RNDN);      /* x/(2*Pi) */
                     60:   mpfr_floor (k, k);                 /* floor(x/(2*Pi)) */
                     61:   mpfr_mul (c, k, c, GMP_RNDN);
                     62:   mpfr_sub (k, x, c, GMP_RNDN);      /* 0 <= k < 2*Pi */
                     63:   mpfr_const_pi (c, GMP_RNDN); /* cached */
                     64:   neg = mpfr_cmp (k, c) > 0;
                     65:   mpfr_clear (k);
                     66:
                     67:   do
                     68:     {
                     69:       mpfr_cos (c, x, GMP_RNDZ);
                     70:       if ((ok = mpfr_can_round (c, m, GMP_RNDZ, rnd_mode, MPFR_PREC(z))))
                     71:         {
                     72:           inexact = mpfr_set (z, c, rnd_mode);
                     73:           mpfr_mul (c, c, c, GMP_RNDU);
                     74:           mpfr_ui_sub (c, 1, c, GMP_RNDN);
                     75:           e = 2 + (-MPFR_EXP(c)) / 2;
                     76:           mpfr_sqrt (c, c, GMP_RNDN);
                     77:           if (neg)
                     78:             mpfr_neg (c, c, GMP_RNDN);
                     79:
                     80:           /* the absolute error on c is at most 2^(e-m) = 2^(EXP(c)-err) */
                     81:           e = MPFR_EXP(c) + m - e;
                     82:           ok = (e >= 0) && mpfr_can_round (c, e, GMP_RNDN, rnd_mode,
                     83:                                            MPFR_PREC(y));
                     84:         }
                     85:
                     86:       if (ok == 0)
                     87:        {
                     88:          m += _mpfr_ceil_log2 ((double) m);
                     89:          mpfr_set_prec (c, m);
                     90:        }
                     91:     }
                     92:   while (ok == 0);
                     93:
                     94:   inexact = mpfr_set (y, c, rnd_mode) || inexact;
                     95:
                     96:   mpfr_clear (c);
                     97:
                     98:   return inexact; /* inexact */
                     99: }

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