title: How to use java.math.BigInteger in jjs / Nashorn?
body: <p>I would like to use java.math.BigInteger in a nashorn / jss JavaScript. </p>
  
  <p>By way of example, let's say I want to calculate Fibonacci sequence numbers. Numbers will need to remain exact, even if they become very large. </p>
  
  <p>Working Java code looks like this:</p>
  
  <pre><code>public static BigInteger fibonacci(int n) {
    BigInteger prev = new BigInteger("0");
    if (n == 0) return prev;
  
    BigInteger next = new BigInteger("1");
    if (n == 1) return next;
  
    BigInteger fib = null;
    int i;
    for (i = 1; i &lt; n; i++) {
      fib = prev.add(next);
      prev = next;
      next = fib;
    }
    return fib;
  }
  </code></pre>
  
  <p>We can test with: </p>
  
  <ul>
  <li>n=77:  5527939700884757</li>
  <li>n=78:  8944394323791464</li>
  <li>n=79: 14472334024676221</li>
  </ul>
  
  <p>So far so good.</p>
  
  <p>Equivalent JavaScript code below:</p>
  
  <pre><code>function fibonacci(n) {
    var BigInteger = Java.type("java.math.BigInteger");
    prev = new BigInteger("0");
    if (n == 0) return prev;
  
    next = new BigInteger("1");
    if (n == 1) return next;
  
    var i, fib = null;
    for (i = 1; i &lt; n; i++) {
      fib = prev.add(next);
      prev = next;
      next = fib;
    }
    return fib;
  }
  </code></pre>
  
  <p>Now we get: </p>
  
  <ul>
  <li>n=77:  5527939700884757</li>
  <li>n=78:  8944394323791464</li>
  <li>n=79: 14472334024676220</li>
  </ul>
  
  <p>Note that the value for 79 is one off - it's wrong.</p>
  
  <p>I suspect the problem is that somewhere, the BigNumber values are re-interpreted as plain JavaScript Numbers. (by "somewhere" I suspect this already happens as the supposedly BigInteger is passed to the .add method)</p>
  
  <p>For example, if I you do:</p>
  
  <pre><code>var BigInteger = Java.type("java.math.BigInteger");
  print(new BigInteger("14472334024676221"));
  </code></pre>
  
  <p>The output is <code>14472334024676220</code>, not <code>14472334024676221</code>.
  This happens even if I explicitly call <code>.toString()</code> on the BigInteger object.</p>
  
  <p>How do I get past this?</p>
  
  <p>UPDATE: @Dici asked if I looked for a threshold. I did - I found:</p>
  
  <pre><code>var str, BigInteger = Java.type("java.math.BigInteger");
  str = "9999999999999998";
  print(str + ": " + new BigInteger(str));
  str = "9999999999999999";
  print(str + ": " + new BigInteger(str));
  </code></pre>
  
  <p>will output:</p>
  
  <ul>
  <li>9999999999999998: 9999999999999998</li>
  <li>9999999999999999: 10000000000000000</li>
  </ul>
  
  <p>I'm not sure it it's a matter of "treshold", or of some particular numbers having inaccuracies though.</p>
  
  <p>UPDATE 2:</p>
  
  <p>This is now reported as a bug: <a href="https://bugs.openjdk.java.net/browse/JDK-8146264" rel="nofollow">https://bugs.openjdk.java.net/browse/JDK-8146264</a>
  Bug report was done by a Oracle JDK/Nashorn developer so I guess it's the real thing. Keeping my fingers crossed.</p>