Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Base.floatrange with simplified implementation #792

Merged
merged 2 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ function histrange(v::AbstractArray{T}, n::Integer, closed::Symbol=:left) where
histrange(F(lo), F(hi), n, closed)
end

# Base.floatrange has bugs and is not public API anymore
# See https://github.com/JuliaLang/julia/issues/45336
# Use a simplified implementation instead
function _floatrange(start::F, step::F, len::Integer, divisor::F) where F
start = Base.TwicePrecision{Float64}((start, divisor))
step = Base.TwicePrecision{Float64}((step, divisor))
StepRangeLen(start, step, len)
end

function histrange(lo::F, hi::F, n::Integer, closed::Symbol=:left) where F
if hi == lo
start = F(hi)
Expand Down Expand Up @@ -96,7 +105,7 @@ function histrange(lo::F, hi::F, n::Integer, closed::Symbol=:left) where F
len += one(F)
end
end
Base.floatrange(start,step,Int(len),divisor)
_floatrange(start,step,Int(len),divisor)
metab0t marked this conversation as resolved.
Show resolved Hide resolved
end

histrange(vs::NTuple{N,AbstractVector},nbins::NTuple{N,Integer},closed::Symbol) where {N} =
Expand Down
4 changes: 4 additions & 0 deletions test/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ end
@test l <= typemin(Int)
@test h >= typemax(Int)

# Issue 616/667
@test StatsBase.histrange([1.0 for i in 1:100], 10, :left) == 1.0:1.0:2.0
@test StatsBase.histrange([1.05 for i in 1:100], 10, :left) == 1.05:1.0:2.05

@test_throws ArgumentError StatsBase.histrange([1, 10], 0, :left)
@test_throws ArgumentError StatsBase.histrange([1, 10], -1, :left)
@test_throws ArgumentError StatsBase.histrange([1.0, 10.0], 0, :left)
Expand Down