[Python] How can I avoid getting the wrong answer when calculating with njit in Python?

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    I use a njit library from numba to improve the speed of my code in Python. I got a wrong answer. At the same time, if I do not use njit, my code gives a correct answer. Here are two examples

    from numba import njit


    @njit
    def main_with_njit():
    a = 94906267
    print(f'main_with_njit: a**2 = {a ** 2}') # -> a**2 = 9007199515875288
    # the type is int64


    main_with_njit()


    def main_without_njit():
    a = 94906267
    print(f'main_without_njit: a**2 = {a ** 2}') # -> a**2 = 9007199515875289
    # the type is <class 'int'>


    main_without_njit()


    How can I use njit without getting a wrong answer?


    I use Python 3.11.3 with PyCharm 2023.1.2 (Community Edition) on Windows, 64 bit, 0.61.0 version of numba

    I can reproduce the problem originally described by Roshan N using the math library.

    import math


    print(math.pow(94906267, 2)) # -> 9007199515875288.0

    Continue reading...

Compartilhe esta Página