Can anyone let me know why pow()
function returns incorrect value?
print(pow(2, 64))
Output:
0
Can anyone let me know why pow()
function returns incorrect value?
print(pow(2, 64))
Output:
0
Overflow. The default integer type in Mojo is 64 bits wide on 64-bit systems.
64-integer overflow. Try
print(pow(UInt128(2), 64))
Mojo’s Int
type is not of arbitrary precision. You need to be careful when touching edge cases. Wrapping the literals using explicit constructors would be good practice. For this case, Int128
or Int256
may help.
If you want a Python-like integral types, you need to use the BigInt
type.
I also made some discussion on this topic in the Mojo Miji Integers and Overflow of Integers.