xref: /dflybsd-src/contrib/mpfr/src/sin.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_sin -- sine of a floating-point number
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino static int
mpfr_sin_fast(mpfr_ptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)274a238c70SJohn Marino mpfr_sin_fast (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
284a238c70SJohn Marino {
294a238c70SJohn Marino   int inex;
304a238c70SJohn Marino 
314a238c70SJohn Marino   inex = mpfr_sincos_fast (y, NULL, x, rnd_mode);
324a238c70SJohn Marino   inex = inex & 3; /* 0: exact, 1: rounded up, 2: rounded down */
334a238c70SJohn Marino   return (inex == 2) ? -1 : inex;
344a238c70SJohn Marino }
354a238c70SJohn Marino 
364a238c70SJohn Marino int
mpfr_sin(mpfr_ptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)374a238c70SJohn Marino mpfr_sin (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
384a238c70SJohn Marino {
394a238c70SJohn Marino   mpfr_t c, xr;
404a238c70SJohn Marino   mpfr_srcptr xx;
414a238c70SJohn Marino   mpfr_exp_t expx, err;
424a238c70SJohn Marino   mpfr_prec_t precy, m;
434a238c70SJohn Marino   int inexact, sign, reduce;
444a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
454a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
464a238c70SJohn Marino 
474a238c70SJohn Marino   MPFR_LOG_FUNC
484a238c70SJohn Marino     (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
494a238c70SJohn Marino      ("y[%Pu]=%.*Rg inexact=%d", mpfr_get_prec (y), mpfr_log_prec, y,
504a238c70SJohn Marino       inexact));
514a238c70SJohn Marino 
524a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
534a238c70SJohn Marino     {
544a238c70SJohn Marino       if (MPFR_IS_NAN (x) || MPFR_IS_INF (x))
554a238c70SJohn Marino         {
564a238c70SJohn Marino           MPFR_SET_NAN (y);
574a238c70SJohn Marino           MPFR_RET_NAN;
584a238c70SJohn Marino 
594a238c70SJohn Marino         }
604a238c70SJohn Marino       else /* x is zero */
614a238c70SJohn Marino         {
624a238c70SJohn Marino           MPFR_ASSERTD (MPFR_IS_ZERO (x));
634a238c70SJohn Marino           MPFR_SET_ZERO (y);
644a238c70SJohn Marino           MPFR_SET_SAME_SIGN (y, x);
654a238c70SJohn Marino           MPFR_RET (0);
664a238c70SJohn Marino         }
674a238c70SJohn Marino     }
684a238c70SJohn Marino 
694a238c70SJohn Marino   /* sin(x) = x - x^3/6 + ... so the error is < 2^(3*EXP(x)-2) */
704a238c70SJohn Marino   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -2 * MPFR_GET_EXP (x), 2, 0,
714a238c70SJohn Marino                                     rnd_mode, {});
724a238c70SJohn Marino 
734a238c70SJohn Marino   MPFR_SAVE_EXPO_MARK (expo);
744a238c70SJohn Marino 
754a238c70SJohn Marino   /* Compute initial precision */
764a238c70SJohn Marino   precy = MPFR_PREC (y);
774a238c70SJohn Marino 
784a238c70SJohn Marino   if (precy >= MPFR_SINCOS_THRESHOLD)
794a238c70SJohn Marino     return mpfr_sin_fast (y, x, rnd_mode);
804a238c70SJohn Marino 
814a238c70SJohn Marino   m = precy + MPFR_INT_CEIL_LOG2 (precy) + 13;
824a238c70SJohn Marino   expx = MPFR_GET_EXP (x);
834a238c70SJohn Marino 
844a238c70SJohn Marino   mpfr_init (c);
854a238c70SJohn Marino   mpfr_init (xr);
864a238c70SJohn Marino 
874a238c70SJohn Marino   MPFR_ZIV_INIT (loop, m);
884a238c70SJohn Marino   for (;;)
894a238c70SJohn Marino     {
904a238c70SJohn Marino       /* first perform argument reduction modulo 2*Pi (if needed),
914a238c70SJohn Marino          also helps to determine the sign of sin(x) */
924a238c70SJohn Marino       if (expx >= 2) /* If Pi < x < 4, we need to reduce too, to determine
934a238c70SJohn Marino                         the sign of sin(x). For 2 <= |x| < Pi, we could avoid
944a238c70SJohn Marino                         the reduction. */
954a238c70SJohn Marino         {
964a238c70SJohn Marino           reduce = 1;
974a238c70SJohn Marino           /* As expx + m - 1 will silently be converted into mpfr_prec_t
984a238c70SJohn Marino              in the mpfr_set_prec call, the assert below may be useful to
994a238c70SJohn Marino              avoid undefined behavior. */
1004a238c70SJohn Marino           MPFR_ASSERTN (expx + m - 1 <= MPFR_PREC_MAX);
1014a238c70SJohn Marino           mpfr_set_prec (c, expx + m - 1);
1024a238c70SJohn Marino           mpfr_set_prec (xr, m);
1034a238c70SJohn Marino           mpfr_const_pi (c, MPFR_RNDN);
1044a238c70SJohn Marino           mpfr_mul_2ui (c, c, 1, MPFR_RNDN);
1054a238c70SJohn Marino           mpfr_remainder (xr, x, c, MPFR_RNDN);
1064a238c70SJohn Marino           /* The analysis is similar to that of cos.c:
1074a238c70SJohn Marino              |xr - x - 2kPi| <= 2^(2-m). Thus we can decide the sign
1084a238c70SJohn Marino              of sin(x) if xr is at distance at least 2^(2-m) of both
1094a238c70SJohn Marino              0 and +/-Pi. */
1104a238c70SJohn Marino           mpfr_div_2ui (c, c, 1, MPFR_RNDN);
1114a238c70SJohn Marino           /* Since c approximates Pi with an error <= 2^(2-expx-m) <= 2^(-m),
1124a238c70SJohn Marino              it suffices to check that c - |xr| >= 2^(2-m). */
1134a238c70SJohn Marino           if (MPFR_SIGN (xr) > 0)
1144a238c70SJohn Marino             mpfr_sub (c, c, xr, MPFR_RNDZ);
1154a238c70SJohn Marino           else
1164a238c70SJohn Marino             mpfr_add (c, c, xr, MPFR_RNDZ);
1174a238c70SJohn Marino           if (MPFR_IS_ZERO(xr)
1184a238c70SJohn Marino               || MPFR_GET_EXP(xr) < (mpfr_exp_t) 3 - (mpfr_exp_t) m
1194a238c70SJohn Marino               || MPFR_IS_ZERO(c)
1204a238c70SJohn Marino               || MPFR_GET_EXP(c) < (mpfr_exp_t) 3 - (mpfr_exp_t) m)
1214a238c70SJohn Marino             goto ziv_next;
1224a238c70SJohn Marino 
1234a238c70SJohn Marino           /* |xr - x - 2kPi| <= 2^(2-m), thus |sin(xr) - sin(x)| <= 2^(2-m) */
1244a238c70SJohn Marino           xx = xr;
1254a238c70SJohn Marino         }
1264a238c70SJohn Marino       else /* the input argument is already reduced */
1274a238c70SJohn Marino         {
1284a238c70SJohn Marino           reduce = 0;
1294a238c70SJohn Marino           xx = x;
1304a238c70SJohn Marino         }
1314a238c70SJohn Marino 
1324a238c70SJohn Marino       sign = MPFR_SIGN(xx);
1334a238c70SJohn Marino       /* now that the argument is reduced, precision m is enough */
1344a238c70SJohn Marino       mpfr_set_prec (c, m);
1354a238c70SJohn Marino       mpfr_cos (c, xx, MPFR_RNDZ);    /* can't be exact */
1364a238c70SJohn Marino       mpfr_nexttoinf (c);           /* now c = cos(x) rounded away */
1374a238c70SJohn Marino       mpfr_mul (c, c, c, MPFR_RNDU); /* away */
1384a238c70SJohn Marino       mpfr_ui_sub (c, 1, c, MPFR_RNDZ);
1394a238c70SJohn Marino       mpfr_sqrt (c, c, MPFR_RNDZ);
1404a238c70SJohn Marino       if (MPFR_IS_NEG_SIGN(sign))
1414a238c70SJohn Marino         MPFR_CHANGE_SIGN(c);
1424a238c70SJohn Marino 
1434a238c70SJohn Marino       /* Warning: c may be 0! */
1444a238c70SJohn Marino       if (MPFR_UNLIKELY (MPFR_IS_ZERO (c)))
1454a238c70SJohn Marino         {
1464a238c70SJohn Marino           /* Huge cancellation: increase prec a lot! */
1474a238c70SJohn Marino           m = MAX (m, MPFR_PREC (x));
1484a238c70SJohn Marino           m = 2 * m;
1494a238c70SJohn Marino         }
1504a238c70SJohn Marino       else
1514a238c70SJohn Marino         {
1524a238c70SJohn Marino           /* the absolute error on c is at most 2^(3-m-EXP(c)),
1534a238c70SJohn Marino              plus 2^(2-m) if there was an argument reduction.
1544a238c70SJohn Marino              Since EXP(c) <= 1, 3-m-EXP(c) >= 2-m, thus the error
1554a238c70SJohn Marino              is at most 2^(3-m-EXP(c)) in case of argument reduction. */
1564a238c70SJohn Marino           err = 2 * MPFR_GET_EXP (c) + (mpfr_exp_t) m - 3 - (reduce != 0);
1574a238c70SJohn Marino           if (MPFR_CAN_ROUND (c, err, precy, rnd_mode))
1584a238c70SJohn Marino             break;
1594a238c70SJohn Marino 
1604a238c70SJohn Marino           /* check for huge cancellation (Near 0) */
1614a238c70SJohn Marino           if (err < (mpfr_exp_t) MPFR_PREC (y))
1624a238c70SJohn Marino             m += MPFR_PREC (y) - err;
1634a238c70SJohn Marino           /* Check if near 1 */
1644a238c70SJohn Marino           if (MPFR_GET_EXP (c) == 1)
1654a238c70SJohn Marino             m += m;
1664a238c70SJohn Marino         }
1674a238c70SJohn Marino 
1684a238c70SJohn Marino     ziv_next:
1694a238c70SJohn Marino       /* Else generic increase */
1704a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, m);
1714a238c70SJohn Marino     }
1724a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
1734a238c70SJohn Marino 
1744a238c70SJohn Marino   inexact = mpfr_set (y, c, rnd_mode);
1754a238c70SJohn Marino   /* inexact cannot be 0, since this would mean that c was representable
1764a238c70SJohn Marino      within the target precision, but in that case mpfr_can_round will fail */
1774a238c70SJohn Marino 
1784a238c70SJohn Marino   mpfr_clear (c);
1794a238c70SJohn Marino   mpfr_clear (xr);
1804a238c70SJohn Marino 
1814a238c70SJohn Marino   MPFR_SAVE_EXPO_FREE (expo);
1824a238c70SJohn Marino   return mpfr_check_range (y, inexact, rnd_mode);
1834a238c70SJohn Marino }
184