Because ratio values reduce to lowest terms and, for integral values where the lowest term is N/1, are auto-converted to BigInts (and formerly Longs), the current behavior of {{clojure.core/numerator}} and {{clojure.core/denominator}} yield unexpected results.
`
user=> (numerator 1/3)
1
user=> (numerator (+ 1/3 2/3))
ClassCastException clojure.lang.BigInt cannot be cast to clojure.lang.Ratio clojure.core/numerator (core.clj:3306)
user=> (denominator 1/3)
3
user=> (denominator (+ 1/3 2/3))
ClassCastException clojure.lang.BigInt cannot be cast to clojure.lang.Ratio clojure.core/denominator (core.clj:3314)
user=>
`
It's confusing to not support numerator and denominator on integer types as this requires you to always check ratio? before invoking them.
Proposed: Extend numerator and denominator to also work on integer types (long, BigInt, BigInteger) by routing to overloaded methods on Numbers for the desired types.
Patch: clj-1435.patch
Screened by: Alex Miller
Questions from screening for Rich:
- numerator and denominator are tagged as returning java.math.BigInteger (not clojure.lang.BigInt) and that's what I followed in the patch. Seems like maybe that should be BigInt though? Not sure on what basis to make that decision.
- Should numerator and denominator accept both BigInteger and BigInt?