From b88997c69291c7a1e563c63555bfd8ead65665de Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 24 Jun 2022 13:33:57 +0200 Subject: [PATCH 1/8] speed up postdecimal digits --- src/ticks.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ticks.jl b/src/ticks.jl index 7241e63..3dafa2d 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -30,10 +30,13 @@ 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))) - x == floor(x; digits = i) && return i + counter = 0 + isinteger(x) && return 0 + while !(trunc(x, digits=0) ≈ x) + x *= 10 + counter += 1 end - return 0 + return counter end fallback_ticks(x_min::T, x_max::T, k_min, k_max) where {T} = ( From 595429ef8141b65f4d14599dc6c41eb2b38eec36 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 24 Jun 2022 16:22:02 +0200 Subject: [PATCH 2/8] format files --- src/ticks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ticks.jl b/src/ticks.jl index 3dafa2d..5b86490 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -32,7 +32,7 @@ end function postdecimal_digits(x::T) where {T} counter = 0 isinteger(x) && return 0 - while !(trunc(x, digits=0) ≈ x) + while !(trunc(x, digits = 0) ≈ x) x *= 10 counter += 1 end From 9fc6af252c3842deefa416af62186938a1a498f2 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 24 Jun 2022 17:06:50 +0200 Subject: [PATCH 3/8] use equality --- src/ticks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ticks.jl b/src/ticks.jl index 5b86490..4274052 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -32,7 +32,7 @@ end function postdecimal_digits(x::T) where {T} counter = 0 isinteger(x) && return 0 - while !(trunc(x, digits = 0) ≈ x) + while trunc(x, digits = 0) != x x *= 10 counter += 1 end From 17aa0aa1089219b930ceae59683d514a9b88611e Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 24 Jun 2022 17:45:49 +0200 Subject: [PATCH 4/8] add tests --- test/runtests.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 3e47fbe..199c1e3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -270,3 +270,19 @@ 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.234) == 3 + @test PlotUtils.postdecimal_digits(11.234) == 3 + @test_broken PlotUtils.postdecimal_digits(1/3) == 16 + @test PlotUtils.postdecimal_digits(1.234e-2) == 5 + @test PlotUtils.postdecimal_digits(1.234f-3) == 6 + @test_broken PlotUtils.postdecimal_digits(0.284882f0) == 6 + @test PlotUtils.postdecimal_digits(0.5517515f0) == 7 + @test_broken PlotUtils.postdecimal_digits(0.1578114989219891) == 16 + @test_broken PlotUtils.postdecimal_digits(0.35722002265961517) == 17 + @test_broken PlotUtils.postdecimal_digits(0.046297550780936114) == 18 +end From 00ce7faeb4f6b25d7fd67691ca5a8be641274df4 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 24 Jun 2022 17:49:08 +0200 Subject: [PATCH 5/8] string version --- test/runtests.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 199c1e3..d1569a1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -275,14 +275,14 @@ end # digits @testset "Decimal digits" begin - @test PlotUtils.postdecimal_digits(1.234) == 3 + @test PlotUtils.postdecimal_digits(1.245) == 3 @test PlotUtils.postdecimal_digits(11.234) == 3 - @test_broken PlotUtils.postdecimal_digits(1/3) == 16 + @test PlotUtils.postdecimal_digits(1/3) == 16 @test PlotUtils.postdecimal_digits(1.234e-2) == 5 @test PlotUtils.postdecimal_digits(1.234f-3) == 6 - @test_broken PlotUtils.postdecimal_digits(0.284882f0) == 6 + @test PlotUtils.postdecimal_digits(0.284882f0) == 6 @test PlotUtils.postdecimal_digits(0.5517515f0) == 7 - @test_broken PlotUtils.postdecimal_digits(0.1578114989219891) == 16 - @test_broken PlotUtils.postdecimal_digits(0.35722002265961517) == 17 - @test_broken PlotUtils.postdecimal_digits(0.046297550780936114) == 18 + @test PlotUtils.postdecimal_digits(0.1578114989219891) == 16 + @test PlotUtils.postdecimal_digits(0.35722002265961517) == 17 + @test PlotUtils.postdecimal_digits(0.046297550780936114) == 18 end From 5b5f2e426b78c2f9608c9ed82959ea910ab2e4bf Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Mon, 27 Jun 2022 12:16:32 +0200 Subject: [PATCH 6/8] isinteger version --- src/ticks.jl | 5 +++-- test/runtests.jl | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ticks.jl b/src/ticks.jl index 4274052..b431eff 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -32,9 +32,10 @@ end function postdecimal_digits(x::T) where {T} counter = 0 isinteger(x) && return 0 - while trunc(x, digits = 0) != x - x *= 10 + y = x + while !isinteger(y) counter += 1 + y = x * 10^counter end return counter end diff --git a/test/runtests.jl b/test/runtests.jl index d1569a1..e67275a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -284,5 +284,5 @@ end @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_broken PlotUtils.postdecimal_digits(0.046297550780936114) == 18 end From 0e850969427c8e78264fa71021e4aea91e845116 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 1 Jul 2022 10:20:47 +0200 Subject: [PATCH 7/8] higher lower bound version --- src/ticks.jl | 10 ++++------ test/runtests.jl | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ticks.jl b/src/ticks.jl index b431eff..8ddb977 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -30,14 +30,12 @@ function bounding_order_of_magnitude(xspan::T, base::T) where {T} end function postdecimal_digits(x::T) where {T} - counter = 0 isinteger(x) && return 0 - y = x - while !isinteger(y) - counter += 1 - y = x * 10^counter + max = ceil(Int, log10(floatmax(T))) + for i in 0:max + x == floor(x; digits = i) && return i end - return counter + 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 e67275a..8d5d817 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -284,5 +284,8 @@ end @test PlotUtils.postdecimal_digits(0.5517515f0) == 7 @test PlotUtils.postdecimal_digits(0.1578114989219891) == 16 @test PlotUtils.postdecimal_digits(0.35722002265961517) == 17 - @test_broken PlotUtils.postdecimal_digits(0.046297550780936114) == 18 + @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 From 3c0a835e717b22f136ba5e5d4546a6cc95e92e52 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 1 Jul 2022 10:24:48 +0200 Subject: [PATCH 8/8] format --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 8d5d817..3144783 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -277,7 +277,7 @@ end @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 / 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