<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hmm.. I think using Java's BigInteger from nashorn is an option.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<p style="margin:0.0px 0.0px 0.0px 0.0px;font:14.0px Menlo;color:#000000"><span style="font-variant-ligatures:no-common-ligatures"><span class="Apple-converted-space"> java.math.</span>BigInteger.valueOf(2 * 5 * 103).multiply(new java.math.BigInteger("3030214670981671")</span></p>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
-Sundar</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> nashorn-dev <nashorn-dev-retn@openjdk.org> on behalf of Bruno Borges <bruno.borges@gmail.com><br>
<b>Sent:</b> 02 September 2022 04:20<br>
<b>To:</b> nashorn-dev@openjdk.org <nashorn-dev@openjdk.org><br>
<b>Subject:</b> Math exercise not computing correctly</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hey all,
<div><br>
</div>
<div>I am playing with factorization, and the following problem does not eval to the right value.</div>
<div><br>
</div>
<div>I tried the same with Node and it worked fine.</div>
<div><br>
</div>
<div>2 * 5 * 103 * 3030214670981671 = 3121121111111121130</div>
<div><br>
</div>
<div>-> <a href="https://www.wolframalpha.com/input?i=2+*+5+*+103+*+3030214670981671">
https://www.wolframalpha.com/input?i=2+*+5+*+103+*+3030214670981671</a> <br>
</div>
<div><br>
</div>
<div>
<div>Java computes the correct result: <span style="color:rgb(26,133,255); font-family:"SF Mono",Monaco,Menlo,Courier,monospace; font-size:12px; white-space:pre-wrap">3121121111111121130</span></div>
</div>
<div>Nashorn (v15.4) computes the following result: 3121121111111120896</div>
<div>Node.js (v16.14.2) computes the following result: 3121121111111121000</div>
<div><br>
</div>
<div>
<div>
<div dir="ltr" class="x_gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr">
<div>As you may know, this is because numbers in JavaScript are represented as double, and then we lose precision.</div>
<div><br>
</div>
<div>The solution for Node.js, is the use of BigInt(number), or appending the letter 'n' after each number:</div>
<div><br>
</div>
<div>> 2n * 5n * 103n * 3030214670981671n<br>
3121121111111121130n<br>
</div>
<div><br>
</div>
<div>What would be the best workaround for Nashorn?</div>
<div><font face="Roboto, RobotoDraft, Helvetica, Arial, sans-serif"></font></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>