14a238c70SJohn Marino /* mpfr_atan2 -- arc-tan 2 of a floating-point number
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 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
pi_div_2ui(mpfr_ptr dest,int i,int neg,mpfr_rnd_t rnd_mode)274a238c70SJohn Marino pi_div_2ui (mpfr_ptr dest, int i, int neg, mpfr_rnd_t rnd_mode)
284a238c70SJohn Marino {
294a238c70SJohn Marino int inexact;
304a238c70SJohn Marino MPFR_SAVE_EXPO_DECL (expo);
314a238c70SJohn Marino
324a238c70SJohn Marino MPFR_SAVE_EXPO_MARK (expo);
334a238c70SJohn Marino if (neg) /* -PI/2^i */
344a238c70SJohn Marino {
354a238c70SJohn Marino inexact = - mpfr_const_pi (dest, MPFR_INVERT_RND (rnd_mode));
364a238c70SJohn Marino MPFR_CHANGE_SIGN (dest);
374a238c70SJohn Marino }
384a238c70SJohn Marino else /* PI/2^i */
394a238c70SJohn Marino {
404a238c70SJohn Marino inexact = mpfr_const_pi (dest, rnd_mode);
414a238c70SJohn Marino }
424a238c70SJohn Marino mpfr_div_2ui (dest, dest, i, rnd_mode); /* exact */
434a238c70SJohn Marino MPFR_SAVE_EXPO_FREE (expo);
444a238c70SJohn Marino return mpfr_check_range (dest, inexact, rnd_mode);
454a238c70SJohn Marino }
464a238c70SJohn Marino
474a238c70SJohn Marino int
mpfr_atan2(mpfr_ptr dest,mpfr_srcptr y,mpfr_srcptr x,mpfr_rnd_t rnd_mode)484a238c70SJohn Marino mpfr_atan2 (mpfr_ptr dest, mpfr_srcptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
494a238c70SJohn Marino {
504a238c70SJohn Marino mpfr_t tmp, pi;
514a238c70SJohn Marino int inexact;
524a238c70SJohn Marino mpfr_prec_t prec;
534a238c70SJohn Marino mpfr_exp_t e;
544a238c70SJohn Marino MPFR_SAVE_EXPO_DECL (expo);
554a238c70SJohn Marino MPFR_ZIV_DECL (loop);
564a238c70SJohn Marino
574a238c70SJohn Marino MPFR_LOG_FUNC
584a238c70SJohn Marino (("y[%Pu]=%.*Rg x[%Pu]=%.*Rg rnd=%d",
594a238c70SJohn Marino mpfr_get_prec (y), mpfr_log_prec, y,
604a238c70SJohn Marino mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
614a238c70SJohn Marino ("atan[%Pu]=%.*Rg inexact=%d",
624a238c70SJohn Marino mpfr_get_prec (dest), mpfr_log_prec, dest, inexact));
634a238c70SJohn Marino
644a238c70SJohn Marino /* Special cases */
654a238c70SJohn Marino if (MPFR_ARE_SINGULAR (x, y))
664a238c70SJohn Marino {
674a238c70SJohn Marino /* atan2(0, 0) does not raise the "invalid" floating-point
684a238c70SJohn Marino exception, nor does atan2(y, 0) raise the "divide-by-zero"
694a238c70SJohn Marino floating-point exception.
704a238c70SJohn Marino -- atan2(±0, -0) returns ±pi.313)
714a238c70SJohn Marino -- atan2(±0, +0) returns ±0.
724a238c70SJohn Marino -- atan2(±0, x) returns ±pi, for x < 0.
734a238c70SJohn Marino -- atan2(±0, x) returns ±0, for x > 0.
744a238c70SJohn Marino -- atan2(y, ±0) returns -pi/2 for y < 0.
754a238c70SJohn Marino -- atan2(y, ±0) returns pi/2 for y > 0.
764a238c70SJohn Marino -- atan2(±oo, -oo) returns ±3pi/4.
774a238c70SJohn Marino -- atan2(±oo, +oo) returns ±pi/4.
784a238c70SJohn Marino -- atan2(±oo, x) returns ±pi/2, for finite x.
794a238c70SJohn Marino -- atan2(±y, -oo) returns ±pi, for finite y > 0.
804a238c70SJohn Marino -- atan2(±y, +oo) returns ±0, for finite y > 0.
814a238c70SJohn Marino */
824a238c70SJohn Marino if (MPFR_IS_NAN (x) || MPFR_IS_NAN (y))
834a238c70SJohn Marino {
844a238c70SJohn Marino MPFR_SET_NAN (dest);
854a238c70SJohn Marino MPFR_RET_NAN;
864a238c70SJohn Marino }
874a238c70SJohn Marino if (MPFR_IS_ZERO (y))
884a238c70SJohn Marino {
894a238c70SJohn Marino if (MPFR_IS_NEG (x)) /* +/- PI */
904a238c70SJohn Marino {
914a238c70SJohn Marino set_pi:
924a238c70SJohn Marino if (MPFR_IS_NEG (y))
934a238c70SJohn Marino {
944a238c70SJohn Marino inexact = mpfr_const_pi (dest, MPFR_INVERT_RND (rnd_mode));
954a238c70SJohn Marino MPFR_CHANGE_SIGN (dest);
964a238c70SJohn Marino return -inexact;
974a238c70SJohn Marino }
984a238c70SJohn Marino else
994a238c70SJohn Marino return mpfr_const_pi (dest, rnd_mode);
1004a238c70SJohn Marino }
1014a238c70SJohn Marino else /* +/- 0 */
1024a238c70SJohn Marino {
1034a238c70SJohn Marino set_zero:
1044a238c70SJohn Marino MPFR_SET_ZERO (dest);
1054a238c70SJohn Marino MPFR_SET_SAME_SIGN (dest, y);
1064a238c70SJohn Marino return 0;
1074a238c70SJohn Marino }
1084a238c70SJohn Marino }
1094a238c70SJohn Marino if (MPFR_IS_ZERO (x))
1104a238c70SJohn Marino {
1114a238c70SJohn Marino return pi_div_2ui (dest, 1, MPFR_IS_NEG (y), rnd_mode);
1124a238c70SJohn Marino }
1134a238c70SJohn Marino if (MPFR_IS_INF (y))
1144a238c70SJohn Marino {
1154a238c70SJohn Marino if (!MPFR_IS_INF (x)) /* +/- PI/2 */
1164a238c70SJohn Marino return pi_div_2ui (dest, 1, MPFR_IS_NEG (y), rnd_mode);
1174a238c70SJohn Marino else if (MPFR_IS_POS (x)) /* +/- PI/4 */
1184a238c70SJohn Marino return pi_div_2ui (dest, 2, MPFR_IS_NEG (y), rnd_mode);
1194a238c70SJohn Marino else /* +/- 3*PI/4: Ugly since we have to round properly */
1204a238c70SJohn Marino {
1214a238c70SJohn Marino mpfr_t tmp2;
1224a238c70SJohn Marino MPFR_ZIV_DECL (loop2);
1234a238c70SJohn Marino mpfr_prec_t prec2 = MPFR_PREC (dest) + 10;
1244a238c70SJohn Marino
1254a238c70SJohn Marino MPFR_SAVE_EXPO_MARK (expo);
1264a238c70SJohn Marino mpfr_init2 (tmp2, prec2);
1274a238c70SJohn Marino MPFR_ZIV_INIT (loop2, prec2);
1284a238c70SJohn Marino for (;;)
1294a238c70SJohn Marino {
1304a238c70SJohn Marino mpfr_const_pi (tmp2, MPFR_RNDN);
1314a238c70SJohn Marino mpfr_mul_ui (tmp2, tmp2, 3, MPFR_RNDN); /* Error <= 2 */
1324a238c70SJohn Marino mpfr_div_2ui (tmp2, tmp2, 2, MPFR_RNDN);
1334a238c70SJohn Marino if (mpfr_round_p (MPFR_MANT (tmp2), MPFR_LIMB_SIZE (tmp2),
1344a238c70SJohn Marino MPFR_PREC (tmp2) - 2,
1354a238c70SJohn Marino MPFR_PREC (dest) + (rnd_mode == MPFR_RNDN)))
1364a238c70SJohn Marino break;
1374a238c70SJohn Marino MPFR_ZIV_NEXT (loop2, prec2);
1384a238c70SJohn Marino mpfr_set_prec (tmp2, prec2);
1394a238c70SJohn Marino }
1404a238c70SJohn Marino MPFR_ZIV_FREE (loop2);
1414a238c70SJohn Marino if (MPFR_IS_NEG (y))
1424a238c70SJohn Marino MPFR_CHANGE_SIGN (tmp2);
1434a238c70SJohn Marino inexact = mpfr_set (dest, tmp2, rnd_mode);
1444a238c70SJohn Marino mpfr_clear (tmp2);
1454a238c70SJohn Marino MPFR_SAVE_EXPO_FREE (expo);
1464a238c70SJohn Marino return mpfr_check_range (dest, inexact, rnd_mode);
1474a238c70SJohn Marino }
1484a238c70SJohn Marino }
1494a238c70SJohn Marino MPFR_ASSERTD (MPFR_IS_INF (x));
1504a238c70SJohn Marino if (MPFR_IS_NEG (x))
1514a238c70SJohn Marino goto set_pi;
1524a238c70SJohn Marino else
1534a238c70SJohn Marino goto set_zero;
1544a238c70SJohn Marino }
1554a238c70SJohn Marino
1564a238c70SJohn Marino /* When x is a power of two, we call directly atan(y/x) since y/x is
1574a238c70SJohn Marino exact. */
1584a238c70SJohn Marino if (MPFR_UNLIKELY (MPFR_IS_POWER_OF_2 (x)))
1594a238c70SJohn Marino {
1604a238c70SJohn Marino int r;
1614a238c70SJohn Marino mpfr_t yoverx;
1624a238c70SJohn Marino unsigned int saved_flags = __gmpfr_flags;
1634a238c70SJohn Marino
1644a238c70SJohn Marino mpfr_init2 (yoverx, MPFR_PREC (y));
1654a238c70SJohn Marino if (MPFR_LIKELY (mpfr_div_2si (yoverx, y, MPFR_GET_EXP (x) - 1,
1664a238c70SJohn Marino MPFR_RNDN) == 0))
1674a238c70SJohn Marino {
1684a238c70SJohn Marino /* Here the flags have not changed due to mpfr_div_2si. */
1694a238c70SJohn Marino r = mpfr_atan (dest, yoverx, rnd_mode);
1704a238c70SJohn Marino mpfr_clear (yoverx);
1714a238c70SJohn Marino return r;
1724a238c70SJohn Marino }
1734a238c70SJohn Marino else
1744a238c70SJohn Marino {
1754a238c70SJohn Marino /* Division is inexact because of a small exponent range */
1764a238c70SJohn Marino mpfr_clear (yoverx);
1774a238c70SJohn Marino __gmpfr_flags = saved_flags;
1784a238c70SJohn Marino }
1794a238c70SJohn Marino }
1804a238c70SJohn Marino
1814a238c70SJohn Marino MPFR_SAVE_EXPO_MARK (expo);
1824a238c70SJohn Marino
1834a238c70SJohn Marino /* Set up initial prec */
1844a238c70SJohn Marino prec = MPFR_PREC (dest) + 3 + MPFR_INT_CEIL_LOG2 (MPFR_PREC (dest));
1854a238c70SJohn Marino mpfr_init2 (tmp, prec);
1864a238c70SJohn Marino
1874a238c70SJohn Marino MPFR_ZIV_INIT (loop, prec);
1884a238c70SJohn Marino if (MPFR_IS_POS (x))
1894a238c70SJohn Marino /* use atan2(y,x) = atan(y/x) */
1904a238c70SJohn Marino for (;;)
1914a238c70SJohn Marino {
1924a238c70SJohn Marino int div_inex;
1934a238c70SJohn Marino MPFR_BLOCK_DECL (flags);
1944a238c70SJohn Marino
1954a238c70SJohn Marino MPFR_BLOCK (flags, div_inex = mpfr_div (tmp, y, x, MPFR_RNDN));
1964a238c70SJohn Marino if (div_inex == 0)
1974a238c70SJohn Marino {
1984a238c70SJohn Marino /* Result is exact. */
1994a238c70SJohn Marino inexact = mpfr_atan (dest, tmp, rnd_mode);
2004a238c70SJohn Marino goto end;
2014a238c70SJohn Marino }
2024a238c70SJohn Marino
2034a238c70SJohn Marino /* Error <= ulp (tmp) except in case of underflow or overflow. */
2044a238c70SJohn Marino
2054a238c70SJohn Marino /* If the division underflowed, since |atan(z)/z| < 1, we have
2064a238c70SJohn Marino an underflow. */
2074a238c70SJohn Marino if (MPFR_UNDERFLOW (flags))
2084a238c70SJohn Marino {
2094a238c70SJohn Marino int sign;
2104a238c70SJohn Marino
2114a238c70SJohn Marino /* In the case MPFR_RNDN with 2^(emin-2) < |y/x| < 2^(emin-1):
2124a238c70SJohn Marino The smallest significand value S > 1 of |y/x| is:
2134a238c70SJohn Marino * 1 / (1 - 2^(-px)) if py <= px,
2144a238c70SJohn Marino * (1 - 2^(-px) + 2^(-py)) / (1 - 2^(-px)) if py >= px.
2154a238c70SJohn Marino Therefore S - 1 > 2^(-pz), where pz = max(px,py). We have:
2164a238c70SJohn Marino atan(|y/x|) > atan(z), where z = 2^(emin-2) * (1 + 2^(-pz)).
2174a238c70SJohn Marino > z - z^3 / 3.
2184a238c70SJohn Marino > 2^(emin-2) * (1 + 2^(-pz) - 2^(2 emin - 5))
2194a238c70SJohn Marino Assuming pz <= -2 emin + 5, we can round away from zero
2204a238c70SJohn Marino (this is what mpfr_underflow always does on MPFR_RNDN).
2214a238c70SJohn Marino In the case MPFR_RNDN with |y/x| <= 2^(emin-2), we round
2224a238c70SJohn Marino toward zero, as |atan(z)/z| < 1. */
2234a238c70SJohn Marino MPFR_ASSERTN (MPFR_PREC_MAX <=
2244a238c70SJohn Marino 2 * (mpfr_uexp_t) - MPFR_EMIN_MIN + 5);
2254a238c70SJohn Marino if (rnd_mode == MPFR_RNDN && MPFR_IS_ZERO (tmp))
2264a238c70SJohn Marino rnd_mode = MPFR_RNDZ;
2274a238c70SJohn Marino sign = MPFR_SIGN (tmp);
2284a238c70SJohn Marino mpfr_clear (tmp);
2294a238c70SJohn Marino MPFR_SAVE_EXPO_FREE (expo);
2304a238c70SJohn Marino return mpfr_underflow (dest, rnd_mode, sign);
2314a238c70SJohn Marino }
2324a238c70SJohn Marino
2334a238c70SJohn Marino mpfr_atan (tmp, tmp, MPFR_RNDN); /* Error <= 2*ulp (tmp) since
2344a238c70SJohn Marino abs(D(arctan)) <= 1 */
2354a238c70SJohn Marino /* TODO: check that the error bound is correct in case of overflow. */
2364a238c70SJohn Marino /* FIXME: Error <= ulp(tmp) ? */
2374a238c70SJohn Marino if (MPFR_LIKELY (MPFR_CAN_ROUND (tmp, prec - 2, MPFR_PREC (dest),
2384a238c70SJohn Marino rnd_mode)))
2394a238c70SJohn Marino break;
2404a238c70SJohn Marino MPFR_ZIV_NEXT (loop, prec);
2414a238c70SJohn Marino mpfr_set_prec (tmp, prec);
2424a238c70SJohn Marino }
2434a238c70SJohn Marino else /* x < 0 */
2444a238c70SJohn Marino /* Use sign(y)*(PI - atan (|y/x|)) */
2454a238c70SJohn Marino {
2464a238c70SJohn Marino mpfr_init2 (pi, prec);
2474a238c70SJohn Marino for (;;)
2484a238c70SJohn Marino {
2494a238c70SJohn Marino mpfr_div (tmp, y, x, MPFR_RNDN); /* Error <= ulp (tmp) */
2504a238c70SJohn Marino /* If tmp is 0, we have |y/x| <= 2^(-emin-2), thus
2514a238c70SJohn Marino atan|y/x| < 2^(-emin-2). */
2524a238c70SJohn Marino MPFR_SET_POS (tmp); /* no error */
2534a238c70SJohn Marino mpfr_atan (tmp, tmp, MPFR_RNDN); /* Error <= 2*ulp (tmp) since
2544a238c70SJohn Marino abs(D(arctan)) <= 1 */
2554a238c70SJohn Marino mpfr_const_pi (pi, MPFR_RNDN); /* Error <= ulp(pi) /2 */
2564a238c70SJohn Marino e = MPFR_NOTZERO(tmp) ? MPFR_GET_EXP (tmp) : __gmpfr_emin - 1;
2574a238c70SJohn Marino mpfr_sub (tmp, pi, tmp, MPFR_RNDN); /* see above */
2584a238c70SJohn Marino if (MPFR_IS_NEG (y))
2594a238c70SJohn Marino MPFR_CHANGE_SIGN (tmp);
2604a238c70SJohn Marino /* Error(tmp) <= (1/2+2^(EXP(pi)-EXP(tmp)-1)+2^(e-EXP(tmp)+1))*ulp
2614a238c70SJohn Marino <= 2^(MAX (MAX (EXP(PI)-EXP(tmp)-1, e-EXP(tmp)+1),
2624a238c70SJohn Marino -1)+2)*ulp(tmp) */
2634a238c70SJohn Marino e = MAX (MAX (MPFR_GET_EXP (pi)-MPFR_GET_EXP (tmp) - 1,
2644a238c70SJohn Marino e - MPFR_GET_EXP (tmp) + 1), -1) + 2;
2654a238c70SJohn Marino if (MPFR_LIKELY (MPFR_CAN_ROUND (tmp, prec - e, MPFR_PREC (dest),
2664a238c70SJohn Marino rnd_mode)))
2674a238c70SJohn Marino break;
2684a238c70SJohn Marino MPFR_ZIV_NEXT (loop, prec);
2694a238c70SJohn Marino mpfr_set_prec (tmp, prec);
2704a238c70SJohn Marino mpfr_set_prec (pi, prec);
2714a238c70SJohn Marino }
2724a238c70SJohn Marino mpfr_clear (pi);
2734a238c70SJohn Marino }
2744a238c70SJohn Marino inexact = mpfr_set (dest, tmp, rnd_mode);
2754a238c70SJohn Marino
2764a238c70SJohn Marino end:
2774a238c70SJohn Marino MPFR_ZIV_FREE (loop);
2784a238c70SJohn Marino mpfr_clear (tmp);
2794a238c70SJohn Marino MPFR_SAVE_EXPO_FREE (expo);
2804a238c70SJohn Marino return mpfr_check_range (dest, inexact, rnd_mode);
2814a238c70SJohn Marino }
282