On Tue, 12 Oct 2010 12:35:24 -0700 (PDT) cej38 <junkerme...@gmail.com> wrote:
> The more that I think about it, the more I would rather have a set of > equalities that always work. float= was a good try. Then you can't use floats. As others have explained, floats are imprecise by nature, being limited to finite binary fractions. To make matters worse, you don't input the numbers in binary, but in decimal, which means most of the fractions you can input can't be represented as a float - so you get an approximation. Given that the numbers are "fuzzy", the concept of equality also becomes "fuzzy" - whether two numbers are equal will depend on the context. So you have to choose the equality that's appropriate for the context. If you want precise numbers, Clojure has three options: 1) If you can represent everything as integers, then BigInteger is probably the easiest to use, with the obvious drawback that it can't handle fractional values, nor can it represent as large a value as a float since you run out of memory. Letting units represent 1/1000's: user> (- 123050 123049) 1 2) If you're going to stay in the world of decimal fractions, use BigDecimal. This has some of the problems of floats, but between allowing arbitrary precision and the input representation matching the internal representation, they're not nearly as obnoxious. user> (- 12.305M 12.3049M) 0.0001M 3) Clojure's rationals let you represent all rational values, until you run out of memory. If you want a decimal approximation after the calculation is done, that's easy to get as well: user> (- 12305/1000 123049/10000) 1/10000 user> (float (- 12305/1000 123049/10000)) 1.0E-4 <mike -- Mike Meyer <m...@mired.org> http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information. O< ascii ribbon campaign - stop html mail - www.asciiribbon.org -- 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