I’m playing with Interval Arithmetic (e.g. see Validated Numerics by Tucker).
Naturally wanted to use numba for speed.
Vanilla Python function does what expected. First print:
(0.09999999999999999, 0.1)
second print with numba.njit produces something unexpected:
(0.09999999999999999, 0.09999999999999999)
adding mathematically insignificant + 1 - 1
restores expected behavior. (see comment in code)
It looks like somewhere in the numba/llvm belly repeating `x/y’ gets optimized away into a single computation.
Which for my purpose breaks intended sequence of execution. Is there a way to control this sort of optimization in numba.njit?
Thanks!
from numba import njit
from ctypes import cdll, c_int
fesetround = cdll.ucrtbased.fesetround
fesetround.restype = c_int
fesetround.argtypes = [c_int]
FE_DOWN = 0x0100
FE_UP = 0x0200
FE_NEAR = 0x0
def test_div(x, y):
fesetround(FE_DOWN)
a = x/ y
fesetround(FE_UP)
# un-comment to get expected behavior
b = x/ y # + 1 - 1
fesetround(FE_NEAR)
return a, b
print("py:", test_div(1, 10))
print("nb:", njit(test_div)(1, 10))