Interesting Rounding Issue in Clojure
We have a shared library that has a lot of really useful functions in it for all the apps we build, and one of the really useful things is to round to a fixed number of decimal places. Now, this isn't Rocket Science, but it is nice to have written well once, and then not have to mess with ever again.
The trick is that you need it to be fast for all Clojure datatypes as well. And several of the more common implementations aren't very fast due to boxing and the overhead associated with that. So we made some pretty simple little functions:
(defn to-2dp "Function to simply resolve a number to a decimal (double), and then round it to 2 DP and return it. Pretty simple, but very useful." [x] (/ (math/round (* 100.0 (parse-double x))) 100.0)) (defn to-3dp "Function to simply resolve a number to a decimal (double), and then round it to 3 DP and return it. Pretty simple, but very useful." [x] (/ (math/round (* 1000.0 (parse-double x))) 1000.0))
but we had a problem. Some of the inputs gave us a lot of grief because of some nasty rounding. And it wasn't all values - it was just those values that had a hard time being expressed in the floating point format.
The solution was to accept some of the inefficiency of rat eJVM BigDecimal and the advanced representation it had, and use that:
(defn to-2dp "Function to simply resolve a number to a decimal (double), and then round it to 2 DP and return it. Pretty simple, but very useful." [x] (/ (math/round (* 100.0 (bigdec (parse-double x)))) 100.0)) (defn to-3dp "Function to simply resolve a number to a decimal (double), and then round it to 3 DP and return it. Pretty simple, but very useful." [x] (/ (math/round (* 1000.0 (bigdec (parse-double x)))) 1000.0))
and then things settled down.