Note that there are some explanatory texts on larger screens.

plurals
  1. POClojure BigInt is not Java BigInteger
    primarykey
    data
    text
    <p>I'm trying to use BigDecimals in Clojure to model (if necessary) arbitrary precision numbers. I have a strange error when I try to instantiate a BigDecimal from an unscaled value and scale factor:</p> <pre><code>user=&gt; 1.31M 1.31M (OK) user=&gt; (class 1.31M) java.math.BigDecimal (OK) user=&gt; (.unscaledValue 1.31M) 131 (OK) user=&gt; (.scale 1.31M) 2 (OK) user=&gt; (.movePointLeft (BigDecimal. 131) 2) 1.31M (OK) user=&gt; (BigDecimal. (BigInteger. "131") 2) 1.31M user=&gt; (BigDecimal. 131N 2) (WRONG!!!) IllegalArgumentException No matching ctor found for class java.math.BigDecimal clojure.lang.Reflector.invokeConstructor (Reflector.java:183) user=&gt; (BigDecimal. (BigInteger. "131") 2) 1.31M </code></pre> <p>The problem here is that the clojure big integer IS NOT a java.math.BigInteger object. Even (bigint x) doesn't work:</p> <pre><code>user=&gt; (doc bigint) ------------------------- clojure.core/bigint ([x]) Coerce to BigInt nil </code></pre> <p>And by the way BigInteger constructor doesn't directly accept numeric values. I know I could also do something like:</p> <pre><code>user=&gt; (BigDecimal. (BigInteger. (.toByteArray (.unscaledValue 1.31M))) (.scale 1.31M)) 1.31M </code></pre> <p>My question is: is there a more idiomatic way of directly manage BigInteger objects from Clojure? Or am I stuck to wrap everything in a custom library, like:</p> <pre><code>user=&gt; (defn my-bigint [x] (BigInteger. (.toString x))) #'user/my-bigint user=&gt; (my-bigint 131) 131 user=&gt; (BigDecimal. (my-bigint 131) 2) 1.31M </code></pre> <p>Thanks in advance for the help!</p> <p>UPDATE: I <strong>NEED</strong> a BigInteger for serialization purposes: my idea is to store a BigDecimal as a byte array and an integer. My problem is that in Clojure, if I want, I cannot pass the result of .unscaledValue back and forth 'cause Clojure doesn't handle BigInteger created from Integers (neither do Java, for what it matters): </p> <pre><code>user=&gt; (BigInteger. 3) IllegalArgumentException No matching ctor found for class java.math.BigInteger clojure.lang.Reflector.invokeConstructor (Reflector.java:183) </code></pre> <p>A call to .toString on the number is unuseful for the semantics of serialization (and more error prone). I would like to know if in Clojure there is an idiomatic way to write something like:</p> <pre><code>user=&gt; (bigdec 131N 2) </code></pre> <p>No .movePointLeft (creation of two different objects without benefits), no .toString (I have a number, I stringify it and then create a BigInteger, another number, from it?), no slow and indirect method: just plain BigInteger and scale value.</p> <p>Vinz</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload