numba / numba

NumPy aware dynamic Python compiler using LLVM
https://numba.pydata.org/
BSD 2-Clause "Simplified" License
10.01k stars 1.13k forks source link

Feature Request: arbitrary precision arithmetic. #9066

Closed c2j closed 1 year ago

c2j commented 1 year ago

Reporting a bug

Same program segment wrap jit or not, but different result.

from numba import jit     # numba==0.57.1  Python 3.8.3 in Linux

def lucas_lehmer_check_Mersenne(n):
    p = n
    s = 4
    m = 2**p - 1
    for _ in range(p-2):
        s = (s**2 - 2) % m
    return s == 0

@jit
def lucas_lehmer_check_Mersenne_jit(n):
    p = n
    s = 4
    m = 2**p - 1
    for _ in range(p-2):
        s = (s**2 - 2) % m
    return s == 0

print(lucas_lehmer_check_Mersenne(64))    # False
print(lucas_lehmer_check_Mersenne_jit(64)).  # True
max9111 commented 1 year ago

There is no arbitrary-precision integer in Numba

CPython does support this, but Numba supports max. 64 bit integers.

from numba import njit     # numba==0.57.1  Python 3.8.3 in Linux
import numba as nb

def lucas_lehmer_check_Mersenne(n):
    p = n
    s = 4
    m = 2**p - 1
    for _ in range(p-2):
        if s**2 > (2**64-1):
            print("Overflow! Can't work in Numba with uint64.")
        s = (s**2 - 2) % m
    return s == 0

print(lucas_lehmer_check_Mersenne(64)) 
esc commented 1 year ago

@c2j thank you for submitting this! @max9111 thank you for triaging this. I have labeled this as a feature request for arbitrary precision arithmetic and updated the issue title accordingly.

stuartarchibald commented 1 year ago

Thanks for looking at this @max9111 @esc, I think this is a duplicate of #5005 which is asking for "big integer" support, closing as a duplicate.

c2j commented 1 year ago

@c2j thank you for submitting this! @max9111 thank you for triaging this. I have labeled this as a feature request for arbitrary precision arithmetic and updated the issue title accordingly.

If numba is not support big int, WHY there is no throw an ERROR?

stuartarchibald commented 1 year ago

Numba compiles functions based on the types of the arguments presented to the function and a lot of "rules" about various operations, it does not check whether the values presented to it at runtime will overflow those types. It's a bit like e.g. C language in this respect. The cost of doing an e.g. overflow check on every operation would massively impact the ability to do things like vectorize loops as there would be so many branches present. With NumPy's NEP-50 proposal it's possible that Numba will gain overflow checking as a mode of operation but the details are not worked out yet.