This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
Hey all. Got a function that has the following line in it: (if (> clicks 0) (* 100 (float (/ total-earnings clicks))) 0)
. In a situation where clicks
is 12 and total-earnings
is 10, running the function in CIDER gives me a Non-terminating decimal expansion; no exact representable decimal result.
error. Yet, if I just run (* 100 (float (/ 10 12)))
in the REPL, I get 83.33333134651184.
Probably doing something incredibly naive… but I can’t figure out why running the entire function fails with the non-terminating decimal expansion error
I would assume the difference has something to do with the REPL lowering the precision level
You can use the with-precision
function to work around the issue: (* 100 (with-precision 5 (/ total-earnings clicks)))
This is the origin of the issue: http://stackoverflow.com/questions/4591206/arithmeticexception-non-terminating-decimal-expansion-no-exact-representable
Here's a description of with-precision
: https://clojuredocs.org/clojure.core/with-precision