1 /* mpz_root(root, u, nth) -- Set ROOT to floor(U^(1/nth)).
2 Return an indication if the result is exact.
4 Copyright 1999, 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
21 #include <stdio.h> /* for NULL */
26 mpz_root (mpz_ptr root, mpz_srcptr u, unsigned long int nth)
29 mp_size_t us, un, rootn, remn;
34 /* even roots of negatives provoke an exception */
35 if (us < 0 && (nth & 1) == 0)
38 /* root extraction interpreted as c^(1/nth) means a zeroth root should
39 provoke a divide by zero, do this even if c==0 */
47 return 1; /* exact result */
51 rootn = (un - 1) / nth + 1;
55 /* FIXME: Perhaps disallow root == NULL */
56 if (root != NULL && u != root)
57 rootp = MPZ_REALLOC (root, rootn);
59 rootp = TMP_ALLOC_LIMBS (rootn);
65 MPN_COPY (rootp, up, un);
70 remn = mpn_rootrem (rootp, NULL, up, un, (mp_limb_t) nth);
75 SIZ(root) = us >= 0 ? rootn : -rootn;
77 MPN_COPY (up, rootp, rootn);