Details

Type: Enhancement

Status: Open

Priority: Major

Resolution: Unresolved

Affects Version/s: Release 1.6

Fix Version/s: None

Component/s: None

Labels:

Patch:Code and Test

Approval:Prescreened
Description
Because ratio values reduce to lowest terms and, for integral values where the lowest term is N/1, are autoconverted 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: clj1435.patch
Prescreening questions:
1. 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.
2. Should numerator and denominator accept both BigInteger and BigInt?
I don't know the official stance on this ticket, but will add some notes.
Aaron, numerator and denominator are pretty clearly documented to work on Ratio types only.
It is pretty easy to write mynumerator and mydenominator that work exactly as you wish, checking for the type of arg and using numerator, denominator for Ratio types, and doing whatever you think is correct for other numeric types.