xref: /dragonfly/contrib/mpfr/src/cos.c (revision ab6d115f)
14a238c70SJohn Marino /* mpfr_cos -- cosine 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_cos_fast(mpfr_ptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)274a238c70SJohn Marino mpfr_cos_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 (NULL, y, x, rnd_mode);
324a238c70SJohn Marino   inex = inex >> 2; /* 0: exact, 1: rounded up, 2: rounded down */
334a238c70SJohn Marino   return (inex == 2) ? -1 : inex;
344a238c70SJohn Marino }
354a238c70SJohn Marino 
364a238c70SJohn Marino /* f <- 1 - r/2! + r^2/4! + ... + (-1)^l r^l/(2l)! + ...
374a238c70SJohn Marino    Assumes |r| < 1/2, and f, r have the same precision.
384a238c70SJohn Marino    Returns e such that the error on f is bounded by 2^e ulps.
394a238c70SJohn Marino */
404a238c70SJohn Marino static int
mpfr_cos2_aux(mpfr_ptr f,mpfr_srcptr r)414a238c70SJohn Marino mpfr_cos2_aux (mpfr_ptr f, mpfr_srcptr r)
424a238c70SJohn Marino {
434a238c70SJohn Marino   mpz_t x, t, s;
444a238c70SJohn Marino   mpfr_exp_t ex, l, m;
454a238c70SJohn Marino   mpfr_prec_t p, q;
464a238c70SJohn Marino   unsigned long i, maxi, imax;
474a238c70SJohn Marino 
484a238c70SJohn Marino   MPFR_ASSERTD(mpfr_get_exp (r) <= -1);
494a238c70SJohn Marino 
504a238c70SJohn Marino   /* compute minimal i such that i*(i+1) does not fit in an unsigned long,
514a238c70SJohn Marino      assuming that there are no padding bits. */
524a238c70SJohn Marino   maxi = 1UL << (CHAR_BIT * sizeof(unsigned long) / 2);
534a238c70SJohn Marino   if (maxi * (maxi / 2) == 0) /* test checked at compile time */
544a238c70SJohn Marino     {
554a238c70SJohn Marino       /* can occur only when there are padding bits. */
564a238c70SJohn Marino       /* maxi * (maxi-1) is representable iff maxi * (maxi / 2) != 0 */
574a238c70SJohn Marino       do
584a238c70SJohn Marino         maxi /= 2;
594a238c70SJohn Marino       while (maxi * (maxi / 2) == 0);
604a238c70SJohn Marino     }
614a238c70SJohn Marino 
624a238c70SJohn Marino   mpz_init (x);
634a238c70SJohn Marino   mpz_init (s);
644a238c70SJohn Marino   mpz_init (t);
654a238c70SJohn Marino   ex = mpfr_get_z_2exp (x, r); /* r = x*2^ex */
664a238c70SJohn Marino 
674a238c70SJohn Marino   /* remove trailing zeroes */
684a238c70SJohn Marino   l = mpz_scan1 (x, 0);
694a238c70SJohn Marino   ex += l;
704a238c70SJohn Marino   mpz_fdiv_q_2exp (x, x, l);
714a238c70SJohn Marino 
724a238c70SJohn Marino   /* since |r| < 1, r = x*2^ex, and x is an integer, necessarily ex < 0 */
734a238c70SJohn Marino 
744a238c70SJohn Marino   p = mpfr_get_prec (f); /* same than r */
754a238c70SJohn Marino   /* bound for number of iterations */
764a238c70SJohn Marino   imax = p / (-mpfr_get_exp (r));
774a238c70SJohn Marino   imax += (imax == 0);
784a238c70SJohn Marino   q = 2 * MPFR_INT_CEIL_LOG2(imax) + 4; /* bound for (3l)^2 */
794a238c70SJohn Marino 
804a238c70SJohn Marino   mpz_set_ui (s, 1); /* initialize sum with 1 */
814a238c70SJohn Marino   mpz_mul_2exp (s, s, p + q); /* scale all values by 2^(p+q) */
824a238c70SJohn Marino   mpz_set (t, s); /* invariant: t is previous term */
834a238c70SJohn Marino   for (i = 1; (m = mpz_sizeinbase (t, 2)) >= q; i += 2)
844a238c70SJohn Marino     {
854a238c70SJohn Marino       /* adjust precision of x to that of t */
864a238c70SJohn Marino       l = mpz_sizeinbase (x, 2);
874a238c70SJohn Marino       if (l > m)
884a238c70SJohn Marino         {
894a238c70SJohn Marino           l -= m;
904a238c70SJohn Marino           mpz_fdiv_q_2exp (x, x, l);
914a238c70SJohn Marino           ex += l;
924a238c70SJohn Marino         }
934a238c70SJohn Marino       /* multiply t by r */
944a238c70SJohn Marino       mpz_mul (t, t, x);
954a238c70SJohn Marino       mpz_fdiv_q_2exp (t, t, -ex);
964a238c70SJohn Marino       /* divide t by i*(i+1) */
974a238c70SJohn Marino       if (i < maxi)
984a238c70SJohn Marino         mpz_fdiv_q_ui (t, t, i * (i + 1));
994a238c70SJohn Marino       else
1004a238c70SJohn Marino         {
1014a238c70SJohn Marino           mpz_fdiv_q_ui (t, t, i);
1024a238c70SJohn Marino           mpz_fdiv_q_ui (t, t, i + 1);
1034a238c70SJohn Marino         }
1044a238c70SJohn Marino       /* if m is the (current) number of bits of t, we can consider that
1054a238c70SJohn Marino          all operations on t so far had precision >= m, so we can prove
1064a238c70SJohn Marino          by induction that the relative error on t is of the form
1074a238c70SJohn Marino          (1+u)^(3l)-1, where |u| <= 2^(-m), and l=(i+1)/2 is the # of loops.
1084a238c70SJohn Marino          Since |(1+x^2)^(1/x) - 1| <= 4x/3 for |x| <= 1/2,
1094a238c70SJohn Marino          for |u| <= 1/(3l)^2, the absolute error is bounded by
1104a238c70SJohn Marino          4/3*(3l)*2^(-m)*t <= 4*l since |t| < 2^m.
1114a238c70SJohn Marino          Therefore the error on s is bounded by 2*l*(l+1). */
1124a238c70SJohn Marino       /* add or subtract to s */
1134a238c70SJohn Marino       if (i % 4 == 1)
1144a238c70SJohn Marino         mpz_sub (s, s, t);
1154a238c70SJohn Marino       else
1164a238c70SJohn Marino         mpz_add (s, s, t);
1174a238c70SJohn Marino     }
1184a238c70SJohn Marino 
1194a238c70SJohn Marino   mpfr_set_z (f, s, MPFR_RNDN);
1204a238c70SJohn Marino   mpfr_div_2ui (f, f, p + q, MPFR_RNDN);
1214a238c70SJohn Marino 
1224a238c70SJohn Marino   mpz_clear (x);
1234a238c70SJohn Marino   mpz_clear (s);
1244a238c70SJohn Marino   mpz_clear (t);
1254a238c70SJohn Marino 
1264a238c70SJohn Marino   l = (i - 1) / 2; /* number of iterations */
1274a238c70SJohn Marino   return 2 * MPFR_INT_CEIL_LOG2 (l + 1) + 1; /* bound is 2l(l+1) */
1284a238c70SJohn Marino }
1294a238c70SJohn Marino 
1304a238c70SJohn Marino int
mpfr_cos(mpfr_ptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)1314a238c70SJohn Marino mpfr_cos (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
1324a238c70SJohn Marino {
1334a238c70SJohn Marino   mpfr_prec_t K0, K, precy, m, k, l;
1344a238c70SJohn Marino   int inexact, reduce = 0;
1354a238c70SJohn Marino   mpfr_t r, s, xr, c;
1364a238c70SJohn Marino   mpfr_exp_t exps, cancel = 0, expx;
1374a238c70SJohn Marino   MPFR_ZIV_DECL (loop);
1384a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
1394a238c70SJohn Marino   MPFR_GROUP_DECL (group);
1404a238c70SJohn Marino 
1414a238c70SJohn Marino   MPFR_LOG_FUNC (
1424a238c70SJohn Marino     ("x[%Pu]=%*.Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
1434a238c70SJohn Marino     ("y[%Pu]=%*.Rg inexact=%d", mpfr_get_prec (y), mpfr_log_prec, y,
1444a238c70SJohn Marino      inexact));
1454a238c70SJohn Marino 
1464a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
1474a238c70SJohn Marino     {
1484a238c70SJohn Marino       if (MPFR_IS_NAN (x) || MPFR_IS_INF (x))
1494a238c70SJohn Marino         {
1504a238c70SJohn Marino           MPFR_SET_NAN (y);
1514a238c70SJohn Marino           MPFR_RET_NAN;
1524a238c70SJohn Marino         }
1534a238c70SJohn Marino       else
1544a238c70SJohn Marino         {
1554a238c70SJohn Marino           MPFR_ASSERTD (MPFR_IS_ZERO (x));
1564a238c70SJohn Marino           return mpfr_set_ui (y, 1, rnd_mode);
1574a238c70SJohn Marino         }
1584a238c70SJohn Marino     }
1594a238c70SJohn Marino 
1604a238c70SJohn Marino   MPFR_SAVE_EXPO_MARK (expo);
1614a238c70SJohn Marino 
1624a238c70SJohn Marino   /* cos(x) = 1-x^2/2 + ..., so error < 2^(2*EXP(x)-1) */
1634a238c70SJohn Marino   expx = MPFR_GET_EXP (x);
1644a238c70SJohn Marino   MPFR_SMALL_INPUT_AFTER_SAVE_EXPO (y, __gmpfr_one, -2 * expx,
1654a238c70SJohn Marino                                     1, 0, rnd_mode, expo, {});
1664a238c70SJohn Marino 
1674a238c70SJohn Marino   /* Compute initial precision */
1684a238c70SJohn Marino   precy = MPFR_PREC (y);
1694a238c70SJohn Marino 
1704a238c70SJohn Marino   if (precy >= MPFR_SINCOS_THRESHOLD)
1714a238c70SJohn Marino     {
1724a238c70SJohn Marino       MPFR_SAVE_EXPO_FREE (expo);
1734a238c70SJohn Marino       return mpfr_cos_fast (y, x, rnd_mode);
1744a238c70SJohn Marino     }
1754a238c70SJohn Marino 
1764a238c70SJohn Marino   K0 = __gmpfr_isqrt (precy / 3);
1774a238c70SJohn Marino   m = precy + 2 * MPFR_INT_CEIL_LOG2 (precy) + 2 * K0;
1784a238c70SJohn Marino 
1794a238c70SJohn Marino   if (expx >= 3)
1804a238c70SJohn Marino     {
1814a238c70SJohn Marino       reduce = 1;
1824a238c70SJohn Marino       /* As expx + m - 1 will silently be converted into mpfr_prec_t
1834a238c70SJohn Marino          in the mpfr_init2 call, the assert below may be useful to
1844a238c70SJohn Marino          avoid undefined behavior. */
1854a238c70SJohn Marino       MPFR_ASSERTN (expx + m - 1 <= MPFR_PREC_MAX);
1864a238c70SJohn Marino       mpfr_init2 (c, expx + m - 1);
1874a238c70SJohn Marino       mpfr_init2 (xr, m);
1884a238c70SJohn Marino     }
1894a238c70SJohn Marino 
1904a238c70SJohn Marino   MPFR_GROUP_INIT_2 (group, m, r, s);
1914a238c70SJohn Marino   MPFR_ZIV_INIT (loop, m);
1924a238c70SJohn Marino   for (;;)
1934a238c70SJohn Marino     {
1944a238c70SJohn Marino       /* If |x| >= 4, first reduce x cmod (2*Pi) into xr, using mpfr_remainder:
1954a238c70SJohn Marino          let e = EXP(x) >= 3, and m the target precision:
1964a238c70SJohn Marino          (1) c <- 2*Pi              [precision e+m-1, nearest]
1974a238c70SJohn Marino          (2) xr <- remainder (x, c) [precision m, nearest]
1984a238c70SJohn Marino          We have |c - 2*Pi| <= 1/2ulp(c) = 2^(3-e-m)
1994a238c70SJohn Marino                  |xr - x - k c| <= 1/2ulp(xr) <= 2^(1-m)
2004a238c70SJohn Marino                  |k| <= |x|/(2*Pi) <= 2^(e-2)
2014a238c70SJohn Marino          Thus |xr - x - 2kPi| <= |k| |c - 2Pi| + 2^(1-m) <= 2^(2-m).
2024a238c70SJohn Marino          It follows |cos(xr) - cos(x)| <= 2^(2-m). */
2034a238c70SJohn Marino       if (reduce)
2044a238c70SJohn Marino         {
2054a238c70SJohn Marino           mpfr_const_pi (c, MPFR_RNDN);
2064a238c70SJohn Marino           mpfr_mul_2ui (c, c, 1, MPFR_RNDN); /* 2Pi */
2074a238c70SJohn Marino           mpfr_remainder (xr, x, c, MPFR_RNDN);
2084a238c70SJohn Marino           if (MPFR_IS_ZERO(xr))
2094a238c70SJohn Marino             goto ziv_next;
2104a238c70SJohn Marino           /* now |xr| <= 4, thus r <= 16 below */
2114a238c70SJohn Marino           mpfr_mul (r, xr, xr, MPFR_RNDU); /* err <= 1 ulp */
2124a238c70SJohn Marino         }
2134a238c70SJohn Marino       else
2144a238c70SJohn Marino         mpfr_mul (r, x, x, MPFR_RNDU); /* err <= 1 ulp */
2154a238c70SJohn Marino 
2164a238c70SJohn Marino       /* now |x| < 4 (or xr if reduce = 1), thus |r| <= 16 */
2174a238c70SJohn Marino 
2184a238c70SJohn Marino       /* we need |r| < 1/2 for mpfr_cos2_aux, i.e., EXP(r) - 2K <= -1 */
2194a238c70SJohn Marino       K = K0 + 1 + MAX(0, MPFR_GET_EXP(r)) / 2;
2204a238c70SJohn Marino       /* since K0 >= 0, if EXP(r) < 0, then K >= 1, thus EXP(r) - 2K <= -3;
2214a238c70SJohn Marino          otherwise if EXP(r) >= 0, then K >= 1/2 + EXP(r)/2, thus
2224a238c70SJohn Marino          EXP(r) - 2K <= -1 */
2234a238c70SJohn Marino 
2244a238c70SJohn Marino       MPFR_SET_EXP (r, MPFR_GET_EXP (r) - 2 * K); /* Can't overflow! */
2254a238c70SJohn Marino 
2264a238c70SJohn Marino       /* s <- 1 - r/2! + ... + (-1)^l r^l/(2l)! */
2274a238c70SJohn Marino       l = mpfr_cos2_aux (s, r);
2284a238c70SJohn Marino       /* l is the error bound in ulps on s */
2294a238c70SJohn Marino       MPFR_SET_ONE (r);
2304a238c70SJohn Marino       for (k = 0; k < K; k++)
2314a238c70SJohn Marino         {
2324a238c70SJohn Marino           mpfr_sqr (s, s, MPFR_RNDU);            /* err <= 2*olderr */
2334a238c70SJohn Marino           MPFR_SET_EXP (s, MPFR_GET_EXP (s) + 1); /* Can't overflow */
2344a238c70SJohn Marino           mpfr_sub (s, s, r, MPFR_RNDN);         /* err <= 4*olderr */
2354a238c70SJohn Marino           if (MPFR_IS_ZERO(s))
2364a238c70SJohn Marino             goto ziv_next;
2374a238c70SJohn Marino           MPFR_ASSERTD (MPFR_GET_EXP (s) <= 1);
2384a238c70SJohn Marino         }
2394a238c70SJohn Marino 
2404a238c70SJohn Marino       /* The absolute error on s is bounded by (2l+1/3)*2^(2K-m)
2414a238c70SJohn Marino          2l+1/3 <= 2l+1.
2424a238c70SJohn Marino          If |x| >= 4, we need to add 2^(2-m) for the argument reduction
2434a238c70SJohn Marino          by 2Pi: if K = 0, this amounts to add 4 to 2l+1/3, i.e., to add
2444a238c70SJohn Marino          2 to l; if K >= 1, this amounts to add 1 to 2*l+1/3. */
2454a238c70SJohn Marino       l = 2 * l + 1;
2464a238c70SJohn Marino       if (reduce)
2474a238c70SJohn Marino         l += (K == 0) ? 4 : 1;
2484a238c70SJohn Marino       k = MPFR_INT_CEIL_LOG2 (l) + 2*K;
2494a238c70SJohn Marino       /* now the error is bounded by 2^(k-m) = 2^(EXP(s)-err) */
2504a238c70SJohn Marino 
2514a238c70SJohn Marino       exps = MPFR_GET_EXP (s);
2524a238c70SJohn Marino       if (MPFR_LIKELY (MPFR_CAN_ROUND (s, exps + m - k, precy, rnd_mode)))
2534a238c70SJohn Marino         break;
2544a238c70SJohn Marino 
2554a238c70SJohn Marino       if (MPFR_UNLIKELY (exps == 1))
2564a238c70SJohn Marino         /* s = 1 or -1, and except x=0 which was already checked above,
2574a238c70SJohn Marino            cos(x) cannot be 1 or -1, so we can round if the error is less
2584a238c70SJohn Marino            than 2^(-precy) for directed rounding, or 2^(-precy-1) for rounding
2594a238c70SJohn Marino            to nearest. */
2604a238c70SJohn Marino         {
2614a238c70SJohn Marino           if (m > k && (m - k >= precy + (rnd_mode == MPFR_RNDN)))
2624a238c70SJohn Marino             {
2634a238c70SJohn Marino               /* If round to nearest or away, result is s = 1 or -1,
2644a238c70SJohn Marino                  otherwise it is round(nexttoward (s, 0)). However in order to
2654a238c70SJohn Marino                  have the inexact flag correctly set below, we set |s| to
2664a238c70SJohn Marino                  1 - 2^(-m) in all cases. */
2674a238c70SJohn Marino               mpfr_nexttozero (s);
2684a238c70SJohn Marino               break;
2694a238c70SJohn Marino             }
2704a238c70SJohn Marino         }
2714a238c70SJohn Marino 
2724a238c70SJohn Marino       if (exps < cancel)
2734a238c70SJohn Marino         {
2744a238c70SJohn Marino           m += cancel - exps;
2754a238c70SJohn Marino           cancel = exps;
2764a238c70SJohn Marino         }
2774a238c70SJohn Marino 
2784a238c70SJohn Marino     ziv_next:
2794a238c70SJohn Marino       MPFR_ZIV_NEXT (loop, m);
2804a238c70SJohn Marino       MPFR_GROUP_REPREC_2 (group, m, r, s);
2814a238c70SJohn Marino       if (reduce)
2824a238c70SJohn Marino         {
2834a238c70SJohn Marino           mpfr_set_prec (xr, m);
2844a238c70SJohn Marino           mpfr_set_prec (c, expx + m - 1);
2854a238c70SJohn Marino         }
2864a238c70SJohn Marino     }
2874a238c70SJohn Marino   MPFR_ZIV_FREE (loop);
2884a238c70SJohn Marino   inexact = mpfr_set (y, s, rnd_mode);
2894a238c70SJohn Marino   MPFR_GROUP_CLEAR (group);
2904a238c70SJohn Marino   if (reduce)
2914a238c70SJohn Marino     {
2924a238c70SJohn Marino       mpfr_clear (xr);
2934a238c70SJohn Marino       mpfr_clear (c);
2944a238c70SJohn Marino     }
2954a238c70SJohn Marino 
2964a238c70SJohn Marino   MPFR_SAVE_EXPO_FREE (expo);
2974a238c70SJohn Marino   return mpfr_check_range (y, inexact, rnd_mode);
2984a238c70SJohn Marino }
299