On Fri, Dec 13, 2024 at 7:22 AM Raffaello Giulietti < raffaello.giulie...@oracle.com> wrote:
> long high = Math.multiplyHigh(a, b); > long low = a * b; > if (high == 0) return low; > return "the big integer consisting of high and low"; > 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! long high = Math.multiplyHigh(value, other); long low = value * other; if (high == 0 && low >= 0 || value < 0 || high == 0 && other < 0) { return asFixnum(context, low); } 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. We have had several proposals and I add one more here: * multiplyExact(long a, long b, long fail) returning fail to indicate overflow * multiplyExact(long a, long b, LongBinaryOperator o) calls o on overflow * <T super ArithmeticException> multiplyExact(long a, long b, T exception) to allow pre-allocating an exception and avoiding the stack trace 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).