14a238c70SJohn Marino /* mpfr_root -- kth root.
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 /* The computation of y = x^(1/k) is done as follows:
274a238c70SJohn Marino
284a238c70SJohn Marino Let x = sign * m * 2^(k*e) where m is an integer
294a238c70SJohn Marino
304a238c70SJohn Marino with 2^(k*(n-1)) <= m < 2^(k*n) where n = PREC(y)
314a238c70SJohn Marino
324a238c70SJohn Marino and m = s^k + r where 0 <= r and m < (s+1)^k
334a238c70SJohn Marino
344a238c70SJohn Marino we want that s has n bits i.e. s >= 2^(n-1), or m >= 2^(k*(n-1))
354a238c70SJohn Marino i.e. m must have at least k*(n-1)+1 bits
364a238c70SJohn Marino
374a238c70SJohn Marino then, not taking into account the sign, the result will be
384a238c70SJohn Marino x^(1/k) = s * 2^e or (s+1) * 2^e according to the rounding mode.
394a238c70SJohn Marino */
404a238c70SJohn Marino
414a238c70SJohn Marino int
mpfr_root(mpfr_ptr y,mpfr_srcptr x,unsigned long k,mpfr_rnd_t rnd_mode)424a238c70SJohn Marino mpfr_root (mpfr_ptr y, mpfr_srcptr x, unsigned long k, mpfr_rnd_t rnd_mode)
434a238c70SJohn Marino {
444a238c70SJohn Marino mpz_t m;
454a238c70SJohn Marino mpfr_exp_t e, r, sh;
464a238c70SJohn Marino mpfr_prec_t n, size_m, tmp;
474a238c70SJohn Marino int inexact, negative;
484a238c70SJohn Marino MPFR_SAVE_EXPO_DECL (expo);
494a238c70SJohn Marino
504a238c70SJohn Marino MPFR_LOG_FUNC
514a238c70SJohn Marino (("x[%Pu]=%.*Rg k=%lu rnd=%d",
524a238c70SJohn Marino mpfr_get_prec (x), mpfr_log_prec, x, k, rnd_mode),
534a238c70SJohn Marino ("y[%Pu]=%.*Rg inexact=%d",
544a238c70SJohn Marino mpfr_get_prec (y), mpfr_log_prec, y, inexact));
554a238c70SJohn Marino
564a238c70SJohn Marino if (MPFR_UNLIKELY (k <= 1))
574a238c70SJohn Marino {
584a238c70SJohn Marino if (k < 1) /* k==0 => y=x^(1/0)=x^(+Inf) */
594a238c70SJohn Marino #if 0
604a238c70SJohn Marino /* For 0 <= x < 1 => +0.
614a238c70SJohn Marino For x = 1 => 1.
624a238c70SJohn Marino For x > 1, => +Inf.
634a238c70SJohn Marino For x < 0 => NaN.
644a238c70SJohn Marino */
654a238c70SJohn Marino {
664a238c70SJohn Marino if (MPFR_IS_NEG (x) && !MPFR_IS_ZERO (x))
674a238c70SJohn Marino {
684a238c70SJohn Marino MPFR_SET_NAN (y);
694a238c70SJohn Marino MPFR_RET_NAN;
704a238c70SJohn Marino }
714a238c70SJohn Marino inexact = mpfr_cmp (x, __gmpfr_one);
724a238c70SJohn Marino if (inexact == 0)
734a238c70SJohn Marino return mpfr_set_ui (y, 1, rnd_mode); /* 1 may be Out of Range */
744a238c70SJohn Marino else if (inexact < 0)
754a238c70SJohn Marino return mpfr_set_ui (y, 0, rnd_mode); /* 0+ */
764a238c70SJohn Marino else
774a238c70SJohn Marino {
784a238c70SJohn Marino mpfr_set_inf (y, 1);
794a238c70SJohn Marino return 0;
804a238c70SJohn Marino }
814a238c70SJohn Marino }
824a238c70SJohn Marino #endif
834a238c70SJohn Marino {
844a238c70SJohn Marino MPFR_SET_NAN (y);
854a238c70SJohn Marino MPFR_RET_NAN;
864a238c70SJohn Marino }
874a238c70SJohn Marino else /* y =x^(1/1)=x */
884a238c70SJohn Marino return mpfr_set (y, x, rnd_mode);
894a238c70SJohn Marino }
904a238c70SJohn Marino
914a238c70SJohn Marino /* Singular values */
924a238c70SJohn Marino else if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
934a238c70SJohn Marino {
944a238c70SJohn Marino if (MPFR_IS_NAN (x))
954a238c70SJohn Marino {
964a238c70SJohn Marino MPFR_SET_NAN (y); /* NaN^(1/k) = NaN */
974a238c70SJohn Marino MPFR_RET_NAN;
984a238c70SJohn Marino }
994a238c70SJohn Marino else if (MPFR_IS_INF (x)) /* +Inf^(1/k) = +Inf
1004a238c70SJohn Marino -Inf^(1/k) = -Inf if k odd
1014a238c70SJohn Marino -Inf^(1/k) = NaN if k even */
1024a238c70SJohn Marino {
1034a238c70SJohn Marino if (MPFR_IS_NEG(x) && (k % 2 == 0))
1044a238c70SJohn Marino {
1054a238c70SJohn Marino MPFR_SET_NAN (y);
1064a238c70SJohn Marino MPFR_RET_NAN;
1074a238c70SJohn Marino }
1084a238c70SJohn Marino MPFR_SET_INF (y);
1094a238c70SJohn Marino MPFR_SET_SAME_SIGN (y, x);
1104a238c70SJohn Marino MPFR_RET (0);
1114a238c70SJohn Marino }
1124a238c70SJohn Marino else /* x is necessarily 0: (+0)^(1/k) = +0
1134a238c70SJohn Marino (-0)^(1/k) = -0 */
1144a238c70SJohn Marino {
1154a238c70SJohn Marino MPFR_ASSERTD (MPFR_IS_ZERO (x));
1164a238c70SJohn Marino MPFR_SET_ZERO (y);
1174a238c70SJohn Marino MPFR_SET_SAME_SIGN (y, x);
1184a238c70SJohn Marino MPFR_RET (0);
1194a238c70SJohn Marino }
1204a238c70SJohn Marino }
1214a238c70SJohn Marino
1224a238c70SJohn Marino /* Returns NAN for x < 0 and k even */
1234a238c70SJohn Marino else if (MPFR_IS_NEG (x) && (k % 2 == 0))
1244a238c70SJohn Marino {
1254a238c70SJohn Marino MPFR_SET_NAN (y);
1264a238c70SJohn Marino MPFR_RET_NAN;
1274a238c70SJohn Marino }
1284a238c70SJohn Marino
1294a238c70SJohn Marino /* General case */
1304a238c70SJohn Marino MPFR_SAVE_EXPO_MARK (expo);
1314a238c70SJohn Marino mpz_init (m);
1324a238c70SJohn Marino
1334a238c70SJohn Marino e = mpfr_get_z_2exp (m, x); /* x = m * 2^e */
1344a238c70SJohn Marino if ((negative = MPFR_IS_NEG(x)))
1354a238c70SJohn Marino mpz_neg (m, m);
1364a238c70SJohn Marino r = e % (mpfr_exp_t) k;
1374a238c70SJohn Marino if (r < 0)
1384a238c70SJohn Marino r += k; /* now r = e (mod k) with 0 <= e < r */
1394a238c70SJohn Marino /* x = (m*2^r) * 2^(e-r) where e-r is a multiple of k */
1404a238c70SJohn Marino
1414a238c70SJohn Marino MPFR_MPZ_SIZEINBASE2 (size_m, m);
1424a238c70SJohn Marino /* for rounding to nearest, we want the round bit to be in the root */
1434a238c70SJohn Marino n = MPFR_PREC (y) + (rnd_mode == MPFR_RNDN);
1444a238c70SJohn Marino
1454a238c70SJohn Marino /* we now multiply m by 2^(r+k*sh) so that root(m,k) will give
1464a238c70SJohn Marino exactly n bits: we want k*(n-1)+1 <= size_m + k*sh + r <= k*n
1474a238c70SJohn Marino i.e. sh = floor ((kn-size_m-r)/k) */
1484a238c70SJohn Marino if ((mpfr_exp_t) size_m + r > k * (mpfr_exp_t) n)
1494a238c70SJohn Marino sh = 0; /* we already have too many bits */
1504a238c70SJohn Marino else
1514a238c70SJohn Marino sh = (k * (mpfr_exp_t) n - (mpfr_exp_t) size_m - r) / k;
1524a238c70SJohn Marino sh = k * sh + r;
1534a238c70SJohn Marino if (sh >= 0)
1544a238c70SJohn Marino {
1554a238c70SJohn Marino mpz_mul_2exp (m, m, sh);
1564a238c70SJohn Marino e = e - sh;
1574a238c70SJohn Marino }
1584a238c70SJohn Marino else if (r > 0)
1594a238c70SJohn Marino {
1604a238c70SJohn Marino mpz_mul_2exp (m, m, r);
1614a238c70SJohn Marino e = e - r;
1624a238c70SJohn Marino }
1634a238c70SJohn Marino
1644a238c70SJohn Marino /* invariant: x = m*2^e, with e divisible by k */
1654a238c70SJohn Marino
1664a238c70SJohn Marino /* we reuse the variable m to store the kth root, since it is not needed
1674a238c70SJohn Marino any more: we just need to know if the root is exact */
1684a238c70SJohn Marino inexact = mpz_root (m, m, k) == 0;
1694a238c70SJohn Marino
1704a238c70SJohn Marino MPFR_MPZ_SIZEINBASE2 (tmp, m);
1714a238c70SJohn Marino sh = tmp - n;
1724a238c70SJohn Marino if (sh > 0) /* we have to flush to 0 the last sh bits from m */
1734a238c70SJohn Marino {
1744a238c70SJohn Marino inexact = inexact || ((mpfr_exp_t) mpz_scan1 (m, 0) < sh);
1754a238c70SJohn Marino mpz_fdiv_q_2exp (m, m, sh);
1764a238c70SJohn Marino e += k * sh;
1774a238c70SJohn Marino }
1784a238c70SJohn Marino
1794a238c70SJohn Marino if (inexact)
1804a238c70SJohn Marino {
1814a238c70SJohn Marino if (negative)
1824a238c70SJohn Marino rnd_mode = MPFR_INVERT_RND (rnd_mode);
1834a238c70SJohn Marino if (rnd_mode == MPFR_RNDU || rnd_mode == MPFR_RNDA
1844a238c70SJohn Marino || (rnd_mode == MPFR_RNDN && mpz_tstbit (m, 0)))
1854a238c70SJohn Marino inexact = 1, mpz_add_ui (m, m, 1);
1864a238c70SJohn Marino else
1874a238c70SJohn Marino inexact = -1;
1884a238c70SJohn Marino }
1894a238c70SJohn Marino
1904a238c70SJohn Marino /* either inexact is not zero, and the conversion is exact, i.e. inexact
1914a238c70SJohn Marino is not changed; or inexact=0, and inexact is set only when
1924a238c70SJohn Marino rnd_mode=MPFR_RNDN and bit (n+1) from m is 1 */
1934a238c70SJohn Marino inexact += mpfr_set_z (y, m, MPFR_RNDN);
1944a238c70SJohn Marino MPFR_SET_EXP (y, MPFR_GET_EXP (y) + e / (mpfr_exp_t) k);
1954a238c70SJohn Marino
1964a238c70SJohn Marino if (negative)
1974a238c70SJohn Marino {
1984a238c70SJohn Marino MPFR_CHANGE_SIGN (y);
1994a238c70SJohn Marino inexact = -inexact;
2004a238c70SJohn Marino }
2014a238c70SJohn Marino
2024a238c70SJohn Marino mpz_clear (m);
2034a238c70SJohn Marino MPFR_SAVE_EXPO_FREE (expo);
2044a238c70SJohn Marino return mpfr_check_range (y, inexact, rnd_mode);
2054a238c70SJohn Marino }
206