nice-numbers: remove hardcoded values for rounding

in preparation for paramatrising digits-that-are-nice, compute
the boundaries between one number and the next
This commit is contained in:
Daniel Barlow 2024-11-17 00:19:39 +00:00
parent 12a547c7aa
commit 40af75c140
2 changed files with 21 additions and 16 deletions

View File

@ -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

View File

@ -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" <|
\_ ->