<< Back to previous view

[CLJ-952] bigdec does not properly convert a clojure.lang.BigInt Created: 12/Mar/12  Updated: 18/May/12  Resolved: 18/May/12

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: Release 1.3, Release 1.4
Fix Version/s: Release 1.5

Type: Defect Priority: Major
Reporter: Paul Stadig Assignee: Unassigned
Resolution: Completed Votes: 2
Labels: None

Attachments: Text File clj-952-make-bigdec-work-on-bigints-patch1.txt    
Patch: Code and Test
Approval: Ok

 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)



 Comments   
Comment by Andy Fingerhut [ 21/Mar/12 10:55 AM ]

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.

Comment by Paul Stadig [ 21/Mar/12 11:51 AM ]

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

Comment by Alan Dipert [ 20/Apr/12 1:35 PM ]

Looks good to me, thanks.

Generated at Sat Oct 25 20:17:44 CDT 2014 using JIRA 4.4#649-r158309.