On Tue, 12 Oct 2010 20:53:16 +0200, cej38 <junkerme...@gmail.com> wrote:

On Oct 12, 12:50 pm, David Sletten <da...@bosatsu.net> wrote:
This discussion may help:http://www.gettingclojure.com/cookbook:numbers#comparing-floats

I originally tried something like float= described in the link, I give
the definition here

(defn float=
  ([x y] (float= x y 0.00001))
  ([x y epsilon]
     (let [scale (if (or (zero? x) (zero? y)) 1 (Math/abs x))]
       (<= (Math/abs (- x y)) (* scale epsilon)))) )

And the truth-table that was given with the function definition:

(float= 0.01 0.0) => false
(float= 0.001 0.0) => false
(float= 0.0001 0.0) => false
(float= 0.00001 0.0) => true


And this works for the problem that I discussed before
user=> (float= 0.0001 (- 12.305 12.3049))
true


But I can come up with a use case where it fails:

user=> (float= 12.3049 12.305)
true


The problem is that the IEEE specification does a great job of
comparing floats, but does a crap job of doing simple math on them.



and again it does exactly what it promises to do:

epsilon is maybe wrong-named here because it is not absolute value, but amount of significant figures. it is multiplied by x to get precision. so what you are asking is

|12.3049 - 12.305| <= 12.3049 * 0.00001 which holds true

--
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

Reply via email to