Search code examples
pythonnumba

Machine epsilon with Numba


I am trying to accelerate my code using Numba.In my code, I have to use the machine epsilon declared in numpy.finfo(float).eps which is equal to 2**-52 according to the documentation. My question is the following:

Is there something to replace the machine eps in Numba?

if Not, is declaring it as a constant has any drawback?


Solution

  • You can use np.finfo(np.float64).eps in Numba njit functions. This works well with Numba 0.54.1. See the following code:

    @nb.njit('float64()')
    def test():
        return np.finfo(np.float64).eps
    test() # Returns 2.220446049250313e-16
    

    Declaring a constant is also fine. In both cases Numba generate a good assembly code that directly return the (precomputed) constant.