Skip to content

Commit

Permalink
Make median! type stable for small float types (JuliaLang#29902)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Nov 5, 2018
1 parent 1f2b16f commit 5c464e9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/Statistics/src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ Like [`median`](@ref), but may overwrite the input vector.
function median!(v::AbstractVector)
isempty(v) && throw(ArgumentError("median of an empty array is undefined, $(repr(v))"))
eltype(v)>:Missing && any(ismissing, v) && return missing
(eltype(v)<:AbstractFloat || eltype(v)>:AbstractFloat) && any(isnan, v) && return NaN
(eltype(v)<:AbstractFloat || eltype(v)>:AbstractFloat) && any(isnan, v) && return convert(eltype(v), NaN)
inds = axes(v, 1)
n = length(inds)
mid = div(first(inds)+last(inds),2)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/Statistics/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ end
@test median!([1 2; 3 4]) == 2.5

@test invoke(median, Tuple{AbstractVector}, 1:10) == median(1:10) == 5.5

@test @inferred(median(Float16[1, 2, NaN])) === Float16(NaN)
@test @inferred(median(Float16[1, 2, 3])) === Float16(2)
@test @inferred(median(Float32[1, 2, NaN])) === NaN32
@test @inferred(median(Float32[1, 2, 3])) === 2.0f0
end

@testset "mean" begin
Expand Down

0 comments on commit 5c464e9

Please sign in to comment.