Skip to content

Instantly share code, notes, and snippets.

@cossio
Created November 6, 2018 22:03
Show Gist options
  • Save cossio/ccade03eae7ae7b924a75f27d30c47e0 to your computer and use it in GitHub Desktop.
Save cossio/ccade03eae7ae7b924a75f27d30c47e0 to your computer and use it in GitHub Desktop.
midpoint of interval
"""midpoint of the interval [a,b]"""
function midpoint(a::Float64, b::Float64)
#= Based on F. Goualard. 2014, Table VII
DOI: 10.1145/2493882 =#
if !(a ≤ b)
return NaN
elseif a == -Inf
if b == Inf
return 0.
else
return nextfloat(-Inf)
end
elseif b == Inf
return prevfloat(Inf)
end
mid = 0.5*(a + b)
if isfinite(mid)
return mid
else
return 0.5 * a + 0.5 * b
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment