Strange numba behaviour

Hi all,
I’m experiencing a weird behaviour with this code. I would expect that w_numba == w but this is not the case. if Nx <6 the results are the same but for higher number the numba compiled method returns nan.
Could you plese help me?
Many thanks
Regards

import numpy as np
from numba import njit


@njit
def dp_formula_with_numba(a: np.ndarray, b: float, x: np.ndarray):
    """ formula dp """
    term1 = np.arcsinh((b - 1) / (a ** .5))
    return ((1 - b) * np.arcsinh(b / (a ** .5)) - ((b ** 2 - 2 * b + a + 1) ** .5) + ((b ** 2 + a) ** .5) +
            term1 * (b - 1)) * ((1 - x ** 2) ** .5)


def dp_formula(a, b, x):
    """ formula dp """
    term1 = np.arcsinh((b - 1) / (a ** .5))
    return ((1 - b) * np.arcsinh(b / (a ** .5)) - ((b ** 2 - 2 * b + a + 1) ** .5) + ((b ** 2 + a) ** .5) +
            term1 * (b - 1)) * ((1 - x ** 2) ** .5)


Nx = 8
Nint = int(Nx/2)
XX1 = np.arange(0, Nint + 1) / Nint
XX1[0] = 0.0025
XX1[Nint] = 0.9999
b_int = 0
a_int = (0.25 * XX1) ** 2
print("a_int: " + str(a_int) + "   dtype:" + str(a_int.dtype) + "   type:" + str(type(a_int)))
print("b_int: " + str(b_int) + "   type:" + str(type(b_int)))
print("XX1: " + str(XX1) + "   dtype:" + str(XX1.dtype) + "   type:" + str(type(XX1)))
w_numba = dp_formula_with_numba(a_int, b_int, XX1)
w = dp_formula(a_int, b_int, XX1)
print("")
print('With numba: ' + str(w_numba))
print('With numpy: ' + str(w))

Results:

a_int: [3.90625000e-07 3.90625000e-03 1.56250000e-02 3.51562500e-02 6.24875006e-02]   dtype:float64   type:<class 'numpy.ndarray'>
b_int: 0   type:<class 'int'>
XX1: [0.0025 0.25   0.5    0.75   0.9999]   dtype:float64   type:<class 'numpy.ndarray'>

With numba: [7.07150889 2.4470088         nan        nan 0.0185825 ]
With numpy: [7.07150889 2.4470088  1.6399837  1.0224987  0.0185825 ]

I did an even simpler test:

import numpy as np
from numba import njit


@njit
def testing_with_numba(a: np.ndarray, b: float):
    """ formula dp """
    q = a**.5
    print((b - 1) / q)
    print((b - 1) / (a**.5))


a_int = np.arange(4)+1
b_int = 0
print("a_int: " + str(a_int) + "   dtype:" + str(a_int.dtype) + "   type:" + str(type(a_int)))
print("b_int: " + str(b_int) + "   type:" + str(type(b_int)))

testing_with_numba(a_int, b_int)

and this is what gets printed:

a_int: [1 2 3 4]   dtype:int32   type:<class 'numpy.ndarray'>
b_int: 0   type:<class 'int'>
[-1.         -0.70710678 -0.57735027 -0.5       ]
[-1.         -0.70710678         nan         nan]

Hi Alberto,

For what it’s worth, I cannot replicate the behavior you describe.

Your first example prints:

a_int: [3.90625000e-07 3.90625000e-03 1.56250000e-02 3.51562500e-02
 6.24875006e-02]   dtype:float64   type:<class 'numpy.ndarray'>
b_int: 0   type:<class 'int'>
XX1: [0.0025 0.25   0.5    0.75   0.9999]   dtype:float64   type:<class 'numpy.ndarray'>

With numba: [7.07150889 2.4470088  1.6399837  1.0224987  0.0185825 ]
With numpy: [7.07150889 2.4470088  1.6399837  1.0224987  0.0185825 ]

And the second simplified one:

a_int: [1 2 3 4]   dtype:int32   type:<class 'numpy.ndarray'>
b_int: 0   type:<class 'int'>
[-1.         -0.70710678 -0.57735027 -0.5       ]
[-1.         -0.70710678 -0.57735027 -0.5       ]

This was done using:

Numba 0.53.1
Numpy 1.20.3
Python 3.9.2

Are you perhaps using an older version of Numba?

Regards,
Rutger

I tried with both python3.6 + numba 0.53.1 + numpy 1.19.5, and python3.8 + numba 0.51.2 + numpy 1.19.2 on Windows 7 and got the same results.

I then ran the same code in the same environment (Anaconda3-2020.11-Windows-x86_64, python3.8 + numba 0.51.2 + numpy 1.19.2 ) but on windows 10 and it works fine…

It runs fine also on this online compiler (LJCFKz - Online IDE & Debugging Tool - Ideone.com)

I still haven’t tried on a different machine with windows 7

After some trials it looks like it’s a numba bug on Windows 7. Can anyone confirm this?

I was also using Windows 10, but have no Windows 7 machine available to test this.