I suggest you read the article a bit more closely. Here are the details of your 
specific case.

The number that you are typing as 12.305 is actually being stored in the 
computer as this:
1100.0100111000010100011110101110000101000111101011100

This number is actually this fraction:
1731774794212311/140737488355328

So here is the true value in decimal:
12.304999999999999715782905695959

It is not possible to represent 12.305 any closer than that on conventional 
hardware.

Likewise your 12.3049 is this:
1100.0100111000001101111011010010100010001100111001110

Which is this fraction:
3463521440926951/281474976710656

Or in decimal:
12.304899999999999948840923025272

This is the best approximation for 12.3049.

When you scale epsilon (0.00001) by about 12, you can see that these two 
numbers are "equal" as expected in the sense defined by float=.

Have all good days,
David Sletten

On Oct 12, 2010, at 2:53 PM, cej38 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.
> 
> 
> -- 
> 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



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