diff --git a/src/ticks.jl b/src/ticks.jl index 7241e63..8ddb977 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -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} = ( diff --git a/test/runtests.jl b/test/runtests.jl index 3e47fbe..3144783 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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