Reviving a thread from three years ago (feel free to point me to something 
more recent) ...

In Clojure 1.6:

(class (+ 1 1.0M))   ; ==> java.math.BigDecimal
(class (+ 1.0 1.0M)) ; ==> java.lang.Double

So combining a BigDecimal with a Long produces a BigDecimal, but combining 
it with a Double produces a Double.  The "demotion" to Double is implied by 
the statement about contagion the Clojure data structures 
page<http://clojure.org/data_structures#Data%20Structures-Numbers>, although 
nothing is said about the promotion from Long to BigDecimal:

Contagion
BigInts and floating point types are "contagious" across operations. That 
is, any integer operation involving a BigInt will result in a BigInt, and 
any operation involving a double or float will result in a double.

To me, the fact that BigDecimal is contagious sometimes but not always, 
seems confusing in a way that could encourage bugs.  The fact that BigInts 
are contagious would also lead one to assume that BigDecimals are 
contagious.

puzzler's post in this thread explained that the rationale for making 
Doubles rather than BigDecimals contagious is that numeric type contagion 
should reflect the contagion of imprecision in calculations, which is going 
to happen no matter type is given to the result, once you introduce any 
imprecise number into the calculations, i.e. in this case, a Double.  And I 
see that when combining a Long with a BigDecimal, imprecision is not 
introduced.  

I understand the point, but I'm not sure that I agree:  If you used a 
Double, you knew (or should have known) that you were getting imprecision.  
And pmbauer's question about Floats wasn't answered.  It's still true in 
Clojure 1.6 that Doubles are contagious over Floats:
(class (+ (float 1.0) 1.0)) ; ==> java.lang.Double

I think that at the very least, if BigDecimals are contagious only some of 
the time, it would be worth putting big warning signs in the documentation 
announcing this fact.  (I understand, though, that Clojure documentation is 
a work in progress, and is lagging in some ways behind the language, as 
suggested by other recent threads.)

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

Reply via email to