From 40af75c14052ee8af7fc8520822b6cff44e62796 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 17 Nov 2024 00:19:39 +0000 Subject: [PATCH] nice-numbers: remove hardcoded values for rounding in preparation for paramatrising digits-that-are-nice, compute the boundaries between one number and the next --- frontend/src/Lib.elm | 33 +++++++++++++++++---------------- frontend/tests/LibTest.elm | 4 ++++ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/frontend/src/Lib.elm b/frontend/src/Lib.elm index 7648c82..c2d1494 100644 --- a/frontend/src/Lib.elm +++ b/frontend/src/Lib.elm @@ -14,29 +14,30 @@ maybe default val = log10 x = logBase 10 x expt b x = b^(toFloat x) + +niceDigitRound f = + let digits = [1, 2, 5, 10] + breaks = List.map2 (\ a b -> (a+b)/2) (List.drop 1 digits) digits + m = List.map2 Tuple.pair digits breaks + in case List.filter (\ d -> (f < Tuple.second d)) m of + ((a,_) :: _) -> a + _ -> 10 + +niceDigit f = + let niceDigits = [1, 2, 5] + in case List.filter ((<) f) niceDigits of + (a :: _) -> a + _ -> 10 + niceNumber x round = let exp = floor (log10 x) f = x / (expt 10.0 exp) - nfRound = if f < 1.5 - then 1 - else if f < 3 - then 2 - else if f < 7 - then 5 - else 10 - nf = if f <= 1 - then 1 - else if f <= 2 - then 2 - else if f <= 5 - then 5 - else 10 in if round then - nfRound * expt 10 exp + (niceDigitRound f) * expt 10 exp else - nf * expt 10 exp + (niceDigit f) * expt 10 exp looseLabels ticks min max = let diff --git a/frontend/tests/LibTest.elm b/frontend/tests/LibTest.elm index 7d1423d..0e5b572 100644 --- a/frontend/tests/LibTest.elm +++ b/frontend/tests/LibTest.elm @@ -22,6 +22,10 @@ specs = , test "1-32" <| \_ -> let (u, v, _) = looseLabels 8 1 32 + in Expect.equal (0, 35) (u, v) + , test "1-36" <| + \_ -> + let (u, v, _) = looseLabels 8 1 36 in Expect.equal (0, 40) (u, v) , test "1-4" <| \_ ->