Skip to content

Commit

Permalink
Add tests for bigfloat and and add case fot n=0
Browse files Browse the repository at this point in the history
  • Loading branch information
krish8484 committed Aug 29, 2020
1 parent c6f7ca3 commit 3b43a9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,14 @@ hypot(a::Interval{BigFloat}, b::Interval{BigFloat}) = sqrt(a^2 + b^2)

hypot(a::Interval{T}, b::Interval{T}) where T= atomic(Interval{T}, hypot(big53(a), big53(b)))

"""
nthroot(a::Interval{BigFloat}, n::Integer)
Compute the real n-th root of Interval.
"""
function nthroot(a::Interval{BigFloat}, n::Integer)
n == 1 && return a
n < 0 && a == zero(a) && return emptyinterval(a)
isempty(a) && return a
if n > 0
a.hi < 0 && iseven(n) && return emptyinterval(BigFloat)
if a.lo < 0 && a.hi >= 0 && iseven(n)
Expand All @@ -342,16 +349,14 @@ function nthroot(a::Interval{BigFloat}, n::Integer)
ccall((:mpfr_rootn_ui, :libmpfr), Int32 , (Ref{BigFloat}, Ref{BigFloat}, Culong, MPFRRoundingMode) , high , a.hi , ui, MPFRRoundUp)
b = interval(low , high)
return b
end
if n < 0
elseif n < 0
return inv(nthroot(a, -n))
elseif n == 0
return emptyinterval(a)
end
end

function nthroot(a::Interval{T}, n::Integer) where T
n == 1 && return a
n < 0 && a == zero(a) && return emptyinterval(a)
isempty(a) && return a
b = nthroot(big53(a), n)
return convert(Interval{T}, b)
end
12 changes: 12 additions & 0 deletions test/interval_tests/numeric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ end
@test nthroot(∅, 4) ==
@test nthroot(∅, -3) ==
@test nthroot(∅, -4) ==
@test nthroot(Interval(1, 2), 0) ==
@test nthroot(Interval(5, 8), 0) ==
@test nthroot(Interval(1, 7), 0) ==
@test nthroot(Interval(8, 27), 3) == Interval(2, 3)
@test nthroot(Interval(0, 27), 3) == Interval(0, 3)
@test nthroot(Interval(-27, 0), 3) == Interval(-3, 0)
Expand All @@ -414,6 +417,15 @@ end
@test nthroot(Interval(16, 81), -4) == Interval(1/3, 1/2)
@test nthroot(Interval(0, 81), -4) == Interval(1/3, Inf)
@test nthroot(Interval(-81, 0), -4) ==
@test nthroot(Interval(-81, 1), 1) == Interval(-81, 1)
@test nthroot(Interval(-81, 81), -4) == Interval(1/3, Inf)
@test nthroot(Interval(-81, -16), -4) ==
@test nthroot(Interval(-81, -16), 1) == Interval(-81, -16)
@test nthroot(Interval{BigFloat}(16, 81), 4) == Interval{BigFloat}(2, 3)
@test nthroot(Interval{BigFloat}(0, 81), 4) == Interval{BigFloat}(0, 3)
@test nthroot(Interval{BigFloat}(-81, 0), 4) == Interval{BigFloat}(0)
@test nthroot(Interval{BigFloat}(-81, 81), 4) == Interval{BigFloat}(0, 3)
@test nthroot(Interval{BigFloat}(-27, 27), -3) == Interval{BigFloat}(-Inf, Inf)
@test nthroot(Interval{BigFloat}(-81, -16), -4) ==
@test nthroot(Interval{BigFloat}(-81, -16), 1) == Interval{BigFloat}(-81, -16)
end

0 comments on commit 3b43a9d

Please sign in to comment.