On Oct 14, 2010, at 12:07 PM, cej38 wrote: > I am kinda sorry that I started this whole thing. I don't need > another lesson in limits. The simple fact of the matter is that, in > my code, I run into a place where I have a comparison (= some-value > (some-function some-data)), the function, data, and value can change. > In a use case that I am interested in, I run into the problem stated, > > user=> (= 0.0001 (- 12.305 12.3049)) > false > > I am OK with replacing the = function with something like float= > discussed above, but whatever I change it two needs to work. If > anyone has found a way, that reliably works, please post it here. > Further, <, >, <=, and >= would also be appreciated. Thank you. > >
If you define 2 of them, then you get the rest for free. We already have float=: (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)))) ) If x < y, then in our case x should be more than epsilon below y: (defn float< ([x y] (float< x y 0.00001)) ([x y epsilon] (let [scale (if (or (zero? x) (zero? y)) 1 (Math/abs x))] (< x (- y (* scale epsilon)))) )) (defn float<= ([x y] (or (float< x y) (float= x y))) ([x y epsilon] (or (float< x y epsilon) (float= x y epsilon)))) (defn float> ([x y] (not (float<= x y))) ([x y epsilon] (not (float<= x y epsilon)))) (defn float>= ([x y] (or (float> x y) (float= x y))) ([x y epsilon] (or (float> x y epsilon) (float= x y epsilon)))) Then you determine how strict epsilon needs to be: (float< 12.3049 12.305) => false (float< 12.3049 12.305 1e-6) => true (float<= 12.305 12.3049) => true (float<= 12.305 12.3049 1e-6) => false (float> 12.305 12.3049 1e-6) => true Have all good days, David Sletten -- 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