Using java.math.BigInteger in Nashorn / jjs?
Sundararajan Athijegannathan
sundararajan.athijegannathan at oracle.com
Tue Dec 29 11:22:48 UTC 2015
Hi,
I initially that the recent fix by Hannes (see also:
http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-December/005762.html).would
fix this issue as well. But, I applied patch by Hannes and verified that
the issue is not fixed by that patch. I've filed a new bug:
https://bugs.openjdk.java.net/browse/JDK-8146264
Thanks,
-Sundar
**
On 12/29/2015 7:23 AM, Roland Bouman wrote:
> Please consider the concrete example of a Java function I would like to get
> working in Nashorn / jss javascript below:
>
> 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 < n; i++) {
> fib = prev.add(next);
> prev = next;
> next = fib;
> }
> return fib;
> }
>
> We can test with these values:
>
> n=77: 5527939700884757
> n=78: 8944394323791464
> n=79: 14472334024676221
>
> So far so good. Now I try the - what I think is - equivalent function in
> javascript:
>
> 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 < n; i++) {
> fib = prev.add(next);
> prev = next;
> next = fib;
> }
> return fib;
> }
>
> However, now we get different results:
>
> n=77: 5527939700884757
> n=78: 8944394323791464
> n=79: 14472334024676220
>
> Note that the value for n=79 is off by one.
More information about the nashorn-dev
mailing list