by reikonomusha · 12y ago · 0 comments
It is better to represent as integers or fixed-precision numbers. That way, you are dealing with exact quantities.
x = 0.1 #=> 0.1 10.times { x+=0.1 } #=> 10 x #=> 1.0999999999999999
an infinite number of them in fact
-> (for/sum ([_ (in-range 10)]) (/ 1 10))
1
Yeah, I know, I'm cheating - `(/ 1 10)` returns a rational? which is not flonum?. Using `0.1` makes it inexact like it should be: -> (for/sum ([_ (in-range 10)]) 0.1)
0.9999999999999999 (/ 10 17)
=> 10/17
(+ (/ 10 17) (/ 4 9))
=> 158/153
=]That said, yes, floats are a bad idea for financial arithmetic.