diff options
author | tege <tege@gmplib.org> | 2002-10-19 08:43:40 +0200 |
---|---|---|
committer | tege <tege@gmplib.org> | 2002-10-19 08:43:40 +0200 |
commit | af8d800b681012fed482072949c5f6aff26e85ee (patch) | |
tree | f33713398b8ebb79eab423b33d200b28b7d42302 /mpz/rootrem.c | |
parent | ae0b561700ef35d04883445d45eebdd30ee8ed1f (diff) | |
download | gmp-af8d800b681012fed482072949c5f6aff26e85ee.tar.gz |
*** empty log message ***
Diffstat (limited to 'mpz/rootrem.c')
-rw-r--r-- | mpz/rootrem.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/mpz/rootrem.c b/mpz/rootrem.c new file mode 100644 index 000000000..78d418471 --- /dev/null +++ b/mpz/rootrem.c @@ -0,0 +1,89 @@ +/* mpz_rootrem(root, rem, u, nth) -- Set ROOT to floor(U^(1/nth)) and + set REM to the remainder. Return an indication if the result is exact. + +Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> /* for NULL */ +#include <stdlib.h> /* for abort */ +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + +int +mpz_rootrem (mpz_ptr root, mpz_ptr rem, mpz_srcptr u, unsigned long int nth) +{ + mp_ptr rootp, up, remp; + mp_size_t us, un, rootn, remn; + + up = PTR(u); + us = SIZ(u); + + /* even roots of negatives provoke an exception */ + if (us < 0 && (nth & 1) == 0) + SQRT_OF_NEGATIVE; + + /* root extraction interpreted as c^(1/nth) means a zeroth root should + provoke a divide by zero, do this even if c==0 */ + if (nth == 0) + DIVIDE_BY_ZERO; + + if (us == 0) + { + if (root != NULL) + SIZ(root) = 0; + SIZ(rem) = 0; + return 1; /* exact result */ + } + + un = ABS (us); + rootn = (un - 1) / nth + 1; + + if (root != NULL) + { + rootp = MPZ_REALLOC (root, rootn); + up = PTR(u); + } + else + { + rootp = __GMP_ALLOCATE_FUNC_LIMBS (rootn); + } + + MPZ_REALLOC (rem, un); + remp = PTR(rem); + + if (nth == 1) + { + MPN_COPY (rootp, up, un); + remn = 0; + } + else + { + remn = mpn_rootrem (rootp, remp, up, un, nth); + } + + if (root != NULL) + SIZ(root) = us >= 0 ? rootn : -rootn; + else + __GMP_FREE_FUNC_LIMBS (rootp, rootn); + + SIZ(rem) = remn; + + return remn == 0; +} |