Clojure

GC Issue 21: bigdec truncates rational

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Reported by tomfaulhaber, Dec 28, 2008
The clojure.core/bigdec function truncates rationals to ints:

(bigdec 1/2) => 0M

As opposed to:

(bigdec 0.5) => 0.5M

This happens because rationals fall into the number? case in bigdec which
converts the rational to a long before converting to BigDecimal.

I'm not sure that this behavior is completely wrong (since there's no way
to specify the behavior of 1/3 in the current API, for instance), but we
should probably at least document it.


Comment 1 by jbondeson, Jan 30, 2009
I discussed this along with another issue here:
http://groups.google.com/group/clojure/browse_thread/thread/1f8029b8556ce851/9b7538b192490097?lnk=gst&q=ratio#9b7538b192490097

(bigdec (/ 1 3)) should probably behave exactly like (/ 1M 3) which throws an
ArithmeticException unless you are inside a "with-precision" block.

I have attached a rather simple fix for this to have Ratio -> BigDecimal conversion
behave like this.
 bigdec-ratio.diff
521 bytes Download
Comment 2 by richhickey, Feb 09, 2009
fixed in svn 1257 - thanks!
Status: Fixed

People

  • Assignee:
    Unassigned
    Reporter:
    Anonymous
Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: