In Julia 1.7.2 multiplication of 3037000691
and 3037000693
returns the negative product -9223370870501072753
. The product I expected is 9223373203208478863
.
function m(a::BigInt, b::BigInt)::BigInt
a * b
end
This function gives the same negative result.
Any ideas on how to get the correct answer in Julia 1.7.2 to 3037000691 * 3037000693
?
P.S.
I did run into this issue doing some math on (big) twin prime numbers.
Try calling your function like this:
Or changing your function to any of the following:
Note you technically only need to cast either
a
orb
, but I think that decreases readability.Example usage:
In this case, you should use
m1
, which usesInt128
s only since it is not possible to multiply twoInt64
values to overflow anInt128
:In general, not using
BigInt
's whenever possible and being explicit about the types in your functions will result in significant performance gains: