Kiran Gangadharan

Rationals vs Decimals in Clojure

  • Use rationals if accuracy is your main concern. For example, operations on decimals are not associative.
(let [a 1.0e50
      b -1.0e50
      c 17.0e00]
  (= (+ (+ a b) c) (+ a (+ b c)))) ; => false

From the above example, one can infer that decimals are subjected to Floating Point corruption. You can find more examples here.

  • Rationals are slower in terms of operations on them when compared to decimals. There is an overhead involved when doing operations on rationals.