Skip to content

Commit

Permalink
Specialised version of mid for alpha=0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders committed May 2, 2017
1 parent c77ec21 commit c9f9f2d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/intervals/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ doc"""
Find the midpoint (or, in general, an intermediate point) at a distance α along the interval `a`. The default is the true midpoint at α=0.5.
"""
function mid{T}(a::Interval{T}, α=0.5)
function mid{T}(a::Interval{T}, α)

isempty(a) && return convert(T, NaN)
isentire(a) && return zero(a.lo)
Expand All @@ -325,6 +325,19 @@ function mid{T}(a::Interval{T}, α=0.5)
return (1-α) * a.lo + α * a.hi # rounds to nearest
end


function mid{T}(a::Interval{T}) # specialized version for α=0.5

isempty(a) && return convert(T, NaN)
isentire(a) && return zero(a.lo)

a.lo == -&& return nextfloat(a.lo)
a.hi == +&& return prevfloat(a.hi)

return 0.5 * (a.lo + a.hi) # rounds to nearest
end


mid{T}(a::Interval{Rational{T}}) = (1//2) * (a.lo + a.hi)


Expand Down

0 comments on commit c9f9f2d

Please sign in to comment.