@@ -43,7 +43,7 @@ export fmpz, FlintZZ, FlintIntegerRing, parent, show, convert, hash,
4343 euler_phi, fibonacci, moebius_mu, primorial, rising_factorial,
4444 number_of_partitions, canonical_unit, isunit, isequal, addeq!, mul!,
4545 issquare, square_root, issquare_with_square_root, next_prime,
46- iszero, rand, rand_bits, binomial, factorial, rand_bits_prime
46+ iszero, rand, rand_bits, binomial, factorial, rand_bits_prime, iroot
4747
4848# ##############################################################################
4949#
@@ -1132,16 +1132,36 @@ function Base.sqrt(x::fmpz)
11321132end
11331133
11341134@doc Markdown. doc"""
1135- root(x::fmpz, n::Int)
1135+ root(x::fmpz, n::Int; check::Bool=true )
11361136
1137- Return the floor of the $n $-the root of $x $. We require $n > 0$ and that
1138- $x \g eq 0$ if $n $ is even.
1137+ Return the $n $-the root of $x $. We require $n > 0$ and that
1138+ $x \g eq 0$ if $n $ is even. By default the function tests whether the input was
1139+ a perfect $n $-th power and if not raises an exception. If `check=false` this
1140+ check is omitted.
11391141"""
1140- function root (x:: fmpz , n:: Int )
1142+ function root (x:: fmpz , n:: Int ; check :: Bool = true )
11411143 x < 0 && iseven (n) && throw (DomainError ((x, n), " Argument `x` must be positive if exponent `n` is even" ))
11421144 n <= 0 && throw (DomainError (n, " Exponent must be positive" ))
11431145 z = fmpz ()
1144- ccall ((:fmpz_root , libflint), Nothing,
1146+ res = ccall ((:fmpz_root , libflint), Bool,
1147+ (Ref{fmpz}, Ref{fmpz}, Int), z, x, n)
1148+ #= Check disabled until flint-2.9 comes out
1149+ check && !res && error("Not a perfect n-th power (n = $n)")
1150+ =#
1151+ return z
1152+ end
1153+
1154+ @doc Markdown. doc"""
1155+ iroot(x::fmpz, n::Int)
1156+
1157+ Return the integer truncation of the $n $-the root of $x $ (round towards zero).
1158+ We require $n > 0$ and that $x \g eq 0$ if $n $ is even.
1159+ """
1160+ function iroot (x:: fmpz , n:: Int )
1161+ x < 0 && iseven (n) && throw (DomainError ((x, n), " Argument `x` must be positive if exponent `n` is even" ))
1162+ n <= 0 && throw (DomainError (n, " Exponent must be positive" ))
1163+ z = fmpz ()
1164+ ccall ((:fmpz_root , libflint), Bool,
11451165 (Ref{fmpz}, Ref{fmpz}, Int), z, x, n)
11461166 return z
11471167end
0 commit comments