Skip to content
Snippets Groups Projects
Commit 84e9e400 authored by Simon Belak's avatar Simon Belak
Browse files

Fix a bug in order-of-magnitude (x<=0) and moved it to util

parent a29ba4df
No related branches found
No related tags found
No related merge requests found
......@@ -71,16 +71,12 @@
(let [scale (/ precision)]
(/ (floor (* x scale)) scale)))
(defn- order-of-magnitude
[x]
(floor (/ (Math/log x) (Math/log 10))))
(def ^:private ^:const pleasing-numbers [1 1.25 2 2.5 3 5 7.5 10])
(defn- nicer-bin-width
[min-value max-value num-bins]
(let [min-bin-width (calculate-bin-width min-value max-value num-bins)
scale (expt 10 (order-of-magnitude min-bin-width))]
scale (expt 10 (u/order-of-magnitude min-bin-width))]
(->> pleasing-numbers
(map (partial * scale))
(drop-while (partial > min-bin-width))
......
......@@ -880,3 +880,11 @@
[m & {:keys [present non-nil]}]
(merge (select-keys m present)
(select-non-nil-keys m non-nil)))
(defn order-of-magnitude
"Return the order of magnitude as a power of 10 of a given number."
[x]
(if (zero? x)
0
(long (math/floor (/ (Math/log (math/abs x))
(Math/log 10))))))
......@@ -32,11 +32,6 @@
(mapv (partial ceil-to 1.0) [1 1.1 1.8])
(mapv (partial ceil-to 15.0) [1.0 15.0 16.0])])
(expect
[-2.0 -1.0 0.0 1.0 2.0 3.0]
(map order-of-magnitude [0.01 0.5 4 12 444 1023]))
(expect
[20.0 2000.0]
[(nicer-bin-width 27 135 8)
......
......@@ -242,3 +242,7 @@
(select-keys-when {:a 100, :b nil, :d 200, :e nil}
:present #{:a :b :c}
:non-nil #{:d :e :f}))
(expect
[-2 -1 0 1 2 3 0 3]
(map order-of-magnitude [0.01 0.5 4 12 444 1023 0 -1444]))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment