bigdec does not properly convert a clojure.lang.BigInt

Description

bigdec handles java.math.BigInteger when converting to java.math.BigDecimal, but it does not handle clojure.lang.BigInt. Instead it treats a clojure.lang.BigInt as a Number, by casting it to long. This causes the following error:

Clojure 1.4.0-beta3
user=> (bigdec (inc (bigint Long/MAX_VALUE)))
IllegalArgumentException Value out of range for long: 9223372036854775808 clojure.lang.RT.longCast (RT.java:1123)

Environment

None

Attachments

1

Activity

Show:

Alan Dipert April 20, 2012 at 7:35 PM

Looks good to me, thanks.

Paul Stadig March 21, 2012 at 5:51 PM

I was originally thinking of something like (BigDecimal. (.toBigInteger ^clojure.lang.BigInt x)). Adding a toBigDecimal method to clojure.lang.BigInt saves some object allocations and such. Probably more of a micro optimization, but it works.

 

Clojure 1.4.0-master-SNAPSHOT
user=> (bigdec (inc (bigint Long/MAX_VALUE)))
9223372036854775808M

Thanks, Andy!

+1

Andy Fingerhut March 21, 2012 at 4:55 PM

Add a case to bigdec to handle BigInts. Also eliminate a reflection warning in the ratio case while we are in there. Paul's failing case has been added to tests, fails before the fix, and passes after. Attempted to make it as run-time efficient as possible by creating a new BigInt/toBigDecimal, patterned after the existing BigInt/toBigInteger.

Completed

Details

Assignee

Reporter

Approval

Ok

Patch

Code and Test

Priority

Affects versions

Fix versions

Created March 12, 2012 at 8:21 PM
Updated May 18, 2012 at 6:32 PM
Resolved May 18, 2012 at 6:32 PM