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:
parent
12a547c7aa
commit
40af75c140
@ -14,29 +14,30 @@ maybe default val =
|
|||||||
log10 x = logBase 10 x
|
log10 x = logBase 10 x
|
||||||
expt b x = b^(toFloat 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 =
|
niceNumber x round =
|
||||||
let exp = floor (log10 x)
|
let exp = floor (log10 x)
|
||||||
f = x / (expt 10.0 exp)
|
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
|
in
|
||||||
if round
|
if round
|
||||||
then
|
then
|
||||||
nfRound * expt 10 exp
|
(niceDigitRound f) * expt 10 exp
|
||||||
else
|
else
|
||||||
nf * expt 10 exp
|
(niceDigit f) * expt 10 exp
|
||||||
|
|
||||||
looseLabels ticks min max =
|
looseLabels ticks min max =
|
||||||
let
|
let
|
||||||
|
@ -22,6 +22,10 @@ specs =
|
|||||||
, test "1-32" <|
|
, test "1-32" <|
|
||||||
\_ ->
|
\_ ->
|
||||||
let (u, v, _) = looseLabels 8 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)
|
in Expect.equal (0, 40) (u, v)
|
||||||
, test "1-4" <|
|
, test "1-4" <|
|
||||||
\_ ->
|
\_ ->
|
||||||
|
Loading…
Reference in New Issue
Block a user