Skip to content

Commit

Permalink
Merge pull request #420 from gwater/patch-1
Browse files Browse the repository at this point in the history
Make big53() thread safe using lock
  • Loading branch information
Kolaru committed Oct 1, 2020
2 parents f56c6c9 + a33c555 commit 4d0eba5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/intervals/precision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ const parameters = IntervalParameters()

"`bigequiv` creates an equivalent `BigFloat` interval to a given `AbstractFloat` interval."
function bigequiv(a::Interval{T}) where {T <: AbstractFloat}
setprecision(Interval, precision(T)) do # precision of T
atomic(Interval{BigFloat}, a)
return guarded_setprecision(Interval, precision(T)) do
return atomic(Interval{BigFloat}, a)
end
end

function bigequiv(x::AbstractFloat)
setprecision(precision(x)) do
BigFloat(x)
end
bigequiv(x::AbstractFloat) = guarded_setprecision(precision(x)) do
return BigFloat(x)
end

# NOTE: prevents multiple threads from calling setprecision() concurrently
const precision_lock = ReentrantLock()

guarded_setprecision(f, ::Type{Interval}, prec::Integer) = lock(precision_lock) do
return setprecision(f, Interval, prec)
end

guarded_setprecision(f, prec::Integer) = lock(precision_lock) do
return setprecision(f, prec)
end

setprecision(::Type{Interval}, ::Type{Float64}) = parameters.precision_type = Float64
# does not change the BigFloat precision


function setprecision(::Type{Interval}, ::Type{T}, prec::Integer) where T<:AbstractFloat
#println("SETTING BIGFLOAT PRECISION TO $precision")
setprecision(BigFloat, prec)
Expand Down
5 changes: 5 additions & 0 deletions test/interval_tests/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ end
@test isequal(x, y)
@test isequal(hash(x), hash(y))

x = 0.1
y = IntervalArithmetic.bigequiv(x)
@test isequal(x, y)
@test isequal(hash(x), hash(y))

x = interval(1, 2)
y = interval(1, 3)
@test !isequal(x, y)
Expand Down

0 comments on commit 4d0eba5

Please sign in to comment.