Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result while calculating big powers #7598

Closed
2 tasks done
JK2Kgit opened this issue Nov 24, 2021 · 2 comments
Closed
2 tasks done

Wrong result while calculating big powers #7598

JK2Kgit opened this issue Nov 24, 2021 · 2 comments

Comments

@JK2Kgit
Copy link

JK2Kgit commented Nov 24, 2021

Reporting a bug

  • I have tried using the latest released version of Numba (most recent is
    visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
  • I have included a self contained code sample to reproduce the problem.
    i.e. it's possible to run as 'python bug.py'.

While performing calculation on big powers function started to return diffrent result after adding @njit

Reproducabe with this code

from numba import njit

def calc(prime, i):
    return (2 ** (3 ** i) - i) % prime

@njit
def ncalc(prime, i):
    return (2 ** (3 ** i) - i) % prime

print(calc(3, 6))
print(ncalc(3, 6))
@JK2Kgit
Copy link
Author

JK2Kgit commented Nov 24, 2021

More Verbose example

from numba import njit

def calc(prime, i):
    print(prime, i)
    x = 3 ** i
    y = 2 ** x
    z = y - i
    w = z % prime
    print(x, "\n", y, "\n", z, "\n", w)
    return (2 ** (3 ** i) - i) % prime

@njit
def ncalc(prime, i):
    print(prime, i)
    x = 3 ** i
    y = 2 ** x
    z = y - i
    w = z % prime
    print(x, "\n", y, "\n", z, "\n", w)
    return (2 ** (3 ** i) - i) % prime

print(calc(3, 11))
print('\n\n')
print(ncalc(3, 11))

After analiazying that looks like buffer overflow

@stuartarchibald
Copy link
Contributor

Thanks for the report. Unfortunately for the case presented, Numba does not support big integers. As a result the calculations overflow the 64bit int (which is the maximum size Numba supports). Closing as a duplicate of #5005, there are some suggestions there about calling into GMP as a potential workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants