From bruno.borges at gmail.com Thu Sep 1 22:50:50 2022
From: bruno.borges at gmail.com (Bruno Borges)
Date: Thu, 1 Sep 2022 15:50:50 -0700
Subject: Math exercise not computing correctly
Message-ID:
Hey all,
I am playing with factorization, and the following problem does not eval to
the right value.
I tried the same with Node and it worked fine.
2 * 5 * 103 * 3030214670981671 = 3121121111111121130
-> https://www.wolframalpha.com/input?i=2+*+5+*+103+*+3030214670981671
Java computes the correct result: 3121121111111121130
Nashorn (v15.4) computes the following result: 3121121111111120896
Node.js (v16.14.2) computes the following result: 3121121111111121000
As you may know, this is because numbers in JavaScript are represented as
double, and then we lose precision.
The solution for Node.js, is the use of BigInt(number), or appending the
letter 'n' after each number:
> 2n * 5n * 103n * 3030214670981671n
3121121111111121130n
What would be the best workaround for Nashorn?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From sundararajan.athijegannathan at oracle.com Fri Sep 2 11:04:23 2022
From: sundararajan.athijegannathan at oracle.com (Sundararajan Athijegannathan)
Date: Fri, 2 Sep 2022 11:04:23 +0000
Subject: Math exercise not computing correctly
In-Reply-To:
References:
Message-ID:
Hmm.. I think using Java's BigInteger from nashorn is an option.
java.math.BigInteger.valueOf(2 * 5 * 103).multiply(new java.math.BigInteger("3030214670981671")
-Sundar
________________________________
From: nashorn-dev on behalf of Bruno Borges
Sent: 02 September 2022 04:20
To: nashorn-dev at openjdk.org
Subject: Math exercise not computing correctly
Hey all,
I am playing with factorization, and the following problem does not eval to the right value.
I tried the same with Node and it worked fine.
2 * 5 * 103 * 3030214670981671 = 3121121111111121130
-> https://www.wolframalpha.com/input?i=2+*+5+*+103+*+3030214670981671
Java computes the correct result: 3121121111111121130
Nashorn (v15.4) computes the following result: 3121121111111120896
Node.js (v16.14.2) computes the following result: 3121121111111121000
As you may know, this is because numbers in JavaScript are represented as double, and then we lose precision.
The solution for Node.js, is the use of BigInt(number), or appending the letter 'n' after each number:
> 2n * 5n * 103n * 3030214670981671n
3121121111111121130n
What would be the best workaround for Nashorn?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: