On Friday, June 12, 2015 at 12:12:01 AM UTC-4, Andy Fingerhut wrote: > > Some related facts, but not sure if they offer a perfect justification for > _why_ rational? and decimal? return what they do. > > (source rational?) reveals that it returns true for integers, ratios, and > decimal?, where (source decimal?) reveals it is true if it is of type > BigDecimal. Arithmetic operations like + - * / quot rem given integers and > ratios should always return exact answers as other integers or ratios > (barring division by 0). I don't think that is true for BigDecimal's, > though, at least with division. > > Regarding rational numbers as ones that can be written as a fraction, all > float and double values except NaN, and positive and negative infinity can > be written exactly as fractions, too. However, + - * / usually give only > approximate answers, not exact. >
Also, these floats and doubles always have a denominator that's a power of 2. Which means thirds, fifths, tenths, and the like generally won't be represented exactly (unlike with Ratio), though halves, quarters, and eighths can be. BigDecimal has a power-of-10 denominator, so it can additionally represent fifths and tenths exactly, but still not thirds (rounded, or non-repeating decimal exception if you haven't set a precision limit). Most of the numeric types and their behavior are documented fully in Java's documentation -- the language documentation for floats and doubles, and the Javadocs for the java.math.BigDecimal class for BigDecimal. The clojure.lang.Ratio class is best understood as simply keeping track of a numerator/denominator pair of BigIntegers which can stay exact, but in some numerical uses will quickly gobble up memory using huger and huger denominators (and numerators, most likely) to do so. If the space (and slowdown!) becomes a problem you then need to decide on a precision restriction and use floats, doubles, or BigDecimals -- or "work smarter, not harder" by using a different algorithm to do whatever you are trying to do. (A perturbation theoretic approach might allow you to use a few high precision Ratios or BigDecimals and do most of the work with deltas from these that are maintained in ordinary doubles, for example. It depends on the numerics you're doing whether, and how, you can do this.) -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.