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

speed up postdecimal digits #140

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ticks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ function bounding_order_of_magnitude(xspan::T, base::T) where {T}
end

function postdecimal_digits(x::T) where {T}
for i in floor(Int, log10(floatmin(T))):ceil(Int, log10(floatmax(T)))
isinteger(x) && return 0
max = ceil(Int, log10(floatmax(T)))
for i in 0:max
x == floor(x; digits = i) && return i
end
return 0
return max
end

fallback_ticks(x_min::T, x_max::T, k_min, k_max) where {T} = (
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,22 @@ end
@test stats.bytes < 1_000 # ~ 848 (on 1.6)
@test stats.time < 1e-3 # ~ 0.45ms (on 1.6)
end

# ----------------------
# digits

@testset "Decimal digits" begin
@test PlotUtils.postdecimal_digits(1.245) == 3
@test PlotUtils.postdecimal_digits(11.234) == 3
@test PlotUtils.postdecimal_digits(1 / 3) == 16
@test PlotUtils.postdecimal_digits(1.234e-2) == 5
@test PlotUtils.postdecimal_digits(1.234f-3) == 6
@test PlotUtils.postdecimal_digits(0.284882f0) == 6
@test PlotUtils.postdecimal_digits(0.5517515f0) == 7
@test PlotUtils.postdecimal_digits(0.1578114989219891) == 16
@test PlotUtils.postdecimal_digits(0.35722002265961517) == 17
@test PlotUtils.postdecimal_digits(0.046297550780936114) == 18
@test PlotUtils.postdecimal_digits(2.5e-22) == 25
@test PlotUtils.postdecimal_digits(floatmin(Float64)) == 309
@test PlotUtils.postdecimal_digits(floatmin(Float32)) == 39
end