@@ -369,24 +369,41 @@ end
369
369
"""
370
370
mid(a::Interval, α=0.5)
371
371
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`.
373
374
374
375
Assumes 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.
375
381
"""
376
382
function mid (a:: Interval{T} , α) where T
377
383
378
384
isempty (a) && return convert (T, NaN )
379
- isentire (a) && return zero (a. lo)
380
385
381
- a. lo == - ∞ && return nextfloat (- ∞)
382
- a. hi == + ∞ && return prevfloat (+ ∞)
386
+ lo = ( a. lo == - ∞ ? nextfloat (- ∞) : a . lo )
387
+ hi = ( a. hi == + ∞ ? prevfloat (+ ∞) : a . hi )
383
388
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
388
396
end
389
397
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
+ """
390
407
function mid (a:: Interval{T} ) where T
391
408
392
409
isempty (a) && return convert (T, NaN )
@@ -395,9 +412,13 @@ function mid(a::Interval{T}) where T
395
412
a. lo == - ∞ && return nextfloat (a. lo)
396
413
a. hi == + ∞ && return prevfloat (a. hi)
397
414
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
401
422
end
402
423
403
424
mid (a:: Interval{Rational{T}} ) where T = (1 // 2 ) * (a. lo + a. hi)
0 commit comments