<div dir="ltr"><div dir="ltr">On Fri, Dec 13, 2024 at 7:22 AM Raffaello Giulietti <<a href="mailto:raffaello.giulietti@oracle.com">raffaello.giulietti@oracle.com</a>> wrote:</div><div class="gmail_quote gmail_quote_container"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">long high = Math.multiplyHigh(a, b);<br>
long low = a * b;<br>
if (high == 0) return low;<br>
return "the big integer consisting of high and low";<br>
</blockquote><div><br></div><div>There's a flaw in this logic: the overflowed multiplication may result in a value greater than MAX_VALUE but less than 2^64-1 (unsigned int64 max) and those values need to be rejected. I believe checking for a sign change as follows covers that situation but please give me something better!</div><div><br></div>long high = Math.multiplyHigh(value, other);<br>long low = value * other;<br>if (high == 0 && low >= 0 || value < 0 || high == 0 && other < 0) {<br> return asFixnum(context, low);<br><div>}</div><div><br></div><div>Regarding an API change in JDK, what's the best process to make that happen? I've created one JEP before but this seems too small for that.</div><div><br></div><div>We have had several proposals and I add one more here:</div><div><br></div><div>* multiplyExact(long a, long b, long fail) returning fail to indicate overflow</div><div>* multiplyExact(long a, long b, LongBinaryOperator o) calls o on overflow</div><div>* <T super ArithmeticException> multiplyExact(long a, long b, T exception) to allow pre-allocating an exception and avoiding the stack trace</div><div><br></div><div>And we would add equivalent methods for the other *Exact operations. All of these forms would meet my needs (my LongBinaryOperator would also just throw a pre-allocated exception).</div></div></div>