> > - why does (+ 4.2 8.4) return 12.600000000000001 and (+ 1.5 2.6) 4.1? > Since 4.2, 8.4 and (+ 4.2 8.4) are java Doubles why does it not behave > as expected? What does clojure do in the background?
That's not a bug. Doubles have a standard. Clojure implementation follows the standard. (as most programming languages does) The thing is that a floating point number have not infinite precision. The only warranty is that it does not create this kind of problem on integer values. You can learn more on this on wikipedia I believe. http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding > > - I also have a question regarding float precison in big operations, > consider the following code: > (defn ope [a b] (+ a (* b (Math/sqrt b)))) > > (reduce ope (range 1000000)) > 3.999995000001039E14 > > vs > > (loop [i 1 x 0] > (if (> i 1000000) > x > (recur (inc i) (ope x i)))) > 4.000005000001039E14 (which is what we get if we do a for loop in > java) > > I had the same problem in haskell, why are the two results different? > I would say that the last number of range is not included in the sum. 10^6 * 10^(6/2) = 10^9 Which is about the difference. range is exclusive on the right. (doc range) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en