@@ -369,24 +369,41 @@ end
369369"""
370370 mid(a::Interval, α=0.5)
371371
372- 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.
372+ Find an intermediate point at a relative position `α`` in the interval `a`.
373+ The default is the true midpoint at `α = 0.5`.
373374
374375Assumes 0 ≤ α ≤ 1.
376+
377+ Warning: if the parameter `α = 0.5` is explicitely set, the behavior differs
378+ from the default case if the provided `Interval` is not finite, since when
379+ `α` is provided `mid` simply replaces `+∞` (respectively `-∞`) by `prevfloat(+∞)`
380+ (respecively `nextfloat(-∞)`) for the computation of the intermediate point.
375381"""
376382function mid (a:: Interval{T} , α) where T
377383
378384 isempty (a) && return convert (T, NaN )
379- isentire (a) && return zero (a. lo)
380385
381- a. lo == - ∞ && return nextfloat (- ∞)
382- a. hi == + ∞ && return prevfloat (+ ∞)
386+ lo = ( a. lo == - ∞ ? nextfloat (- ∞) : a . lo )
387+ hi = ( a. hi == + ∞ ? prevfloat (+ ∞) : a . hi )
383388
384- # @assert 0 ≤ α ≤ 1
385-
386- # return (1-α) * a.lo + α * a.hi # rounds to nearest
387- return α* (a. hi - a. lo) + a. lo # rounds to nearest
389+ midpoint = α * (hi - lo) + lo
390+ isfinite (midpoint) && return midpoint
391+ #= Fallback in case of overflow: hi - lo == +∞.
392+ This case can not be the default one as it does not pass several
393+ IEEE1788-2015 tests for small floats.
394+ =#
395+ return (1 - α) * lo + α * hi
388396end
389397
398+ """
399+ mid(a::Interval)
400+
401+ Find the midpoint of interval `a`.
402+
403+ For intervals of the form `[-∞, x]` or `[x, +∞]` where `x` is finite, return
404+ respectively `nextfloat(-∞)` and `prevfloat(+∞)`. Note that it differs from the
405+ behavior of `mid(a, α=0.5)`.
406+ """
390407function mid (a:: Interval{T} ) where T
391408
392409 isempty (a) && return convert (T, NaN )
@@ -395,9 +412,13 @@ function mid(a::Interval{T}) where T
395412 a. lo == - ∞ && return nextfloat (a. lo)
396413 a. hi == + ∞ && return prevfloat (a. hi)
397414
398- # @assert 0 ≤ α ≤ 1
399-
400- return 0.5 * (a. lo + a. hi)
415+ midpoint = 0.5 * (a. lo + a. hi)
416+ isfinite (midpoint) && return midpoint
417+ #= Fallback in case of overflow: a.hi + a.lo == +∞ or a.hi + a.lo == -∞.
418+ This case can not be the default one as it does not pass several
419+ IEEE1788-2015 tests for small floats.
420+ =#
421+ return 0.5 * a. lo + 0.5 * a. hi
401422end
402423
403424mid (a:: Interval{Rational{T}} ) where T = (1 // 2 ) * (a. lo + a. hi)
0 commit comments