Skip to content

Commit e6ea9c2

Browse files
authored
Use fast path for sqrt and inv (#230)
* Use fast routes for sqrt and inv * Fix inv and sqrt for BigFloat * Fix sqrt and inv for BigFloat * Uncomment Float32 test * Add simple tests for sqrt and inv
1 parent a6846a2 commit e6ea9c2

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/intervals/rounding.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ for (op, f) in ( (:+, :add), (:-, :sub), (:*, :mul), (:/, :div) )
9292
end
9393
end
9494

95+
# inv and sqrt:
96+
97+
# inv(::IntervalRounding{:tight}, a::T, r::RoundingMode) where T<:Union{Float32, Float64} = inv_round(a, r)
98+
#
99+
# sqrt(::IntervalRounding{:tight}, a::T, r::RoundingMode) where T<:Union{Float32, Float64} = sqrt_round(a, r)
100+
101+
95102
for T in (Float32, Float64)
96103
for mode in (:Down, :Up)
97104

@@ -103,7 +110,7 @@ for T in (Float32, Float64)
103110
@eval inv(::IntervalRounding{:tight},
104111
a::$T, $mode1) = inv_round(a, $mode2)
105112

106-
@eval sqrt(::IntervalRounding{:tight},
113+
@eval sqrt(::IntervalRounding{:tight},
107114
a::$T, $mode1) = sqrt_round(a, $mode2)
108115
end
109116
end
@@ -175,13 +182,19 @@ for mode in (:Down, :Up)
175182
# functions not in CRlibm:
176183
for f in (:sqrt, :inv, :tanh, :asinh, :acosh, :atanh)
177184

185+
178186
@eval function $f(::IntervalRounding{:slow},
179187
a::T, $mode1) where T<:AbstractFloat
180188
setrounding(T, $mode2) do
181189
$f(a)
182190
end
183191
end
184192

193+
@eval function $f(::IntervalRounding{:tight},
194+
a::T, $mode1) where T<:AbstractFloat
195+
$f(IntervalRounding{:slow}(), a, $mode2)
196+
end
197+
185198

186199
@eval $f(::IntervalRounding{:accurate},
187200
a::T, $mode1) where {T<:AbstractFloat} = $directed($f(a))
@@ -229,10 +242,18 @@ function _setrounding(::Type{Interval}, rounding_type::Symbol)
229242
@eval $f(a::T, b::T, r::RoundingMode) where {T<:AbstractFloat} = $f($roundtype, a, b, r)
230243
end
231244

245+
# unary functions:
246+
247+
for f in (:sqrt, :inv)
248+
@eval $f(a::T, r::RoundingMode) where {T<:AbstractFloat} = $f($roundtype, a, r)
249+
end
250+
251+
232252
if rounding_type == :tight # for remaining functions, use CRlibm
233253
roundtype = IntervalRounding{:slow}()
234254
end
235255

256+
236257
for f in (:^, :atan)
237258

238259
@eval $f(a::T, b::T, r::RoundingMode) where {T<:AbstractFloat} = $f($roundtype, a, b, r)
@@ -242,7 +263,7 @@ function _setrounding(::Type{Interval}, rounding_type::Symbol)
242263

243264
# unary functions:
244265
for f in vcat(CRlibm.functions,
245-
[:sqrt, :inv, :tanh, :asinh, :acosh, :atanh])
266+
[:tanh, :asinh, :acosh, :atanh])
246267

247268
@eval $f(a::T, r::RoundingMode) where {T<:AbstractFloat} = $f($roundtype, a, r)
248269

test/interval_tests/numeric.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,24 @@ end
272272
@test pow(y, @interval(2.1)) == Interval(0.0, 10.045108566305146)
273273
end
274274

275-
# comment out for now:
276-
#=
275+
@testset "sqrt" begin
276+
@test sqrt(2..3) == Interval(1.414213562373095, 1.7320508075688774)
277+
278+
@test sqrt(big(2..3)) == Interval(big"1.414213562373095048801688724209698078569671875376948073176679737990732478462102", big"1.732050807568877293527446341505872366942805253810380628055806979451933016908815")
279+
end
280+
281+
@testset "inv" begin
282+
@test inv(2..3) == Interval(0.3333333333333333, 0.5)
283+
@test inv(big(2..3)) == Interval(big"3.333333333333333333333333333333333333333333333333333333333333333333333333333305e-01", big"5.0e-01")
284+
end
285+
277286
@testset "Float32 intervals" begin
278287

279288
a = Interval{Float32}(1e38)
280289
b = Interval{Float32}(1e2)
281290
@test a * b == Interval{Float32}(floatmax(Float32), Inf)
282291
end
283-
=#
292+
293+
294+
284295
end

0 commit comments

Comments
 (0)