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

Float division is no longer IEEE 754 after njit #1256

Closed
astrojuanlu opened this issue Jun 23, 2015 · 3 comments
Closed

Float division is no longer IEEE 754 after njit #1256

astrojuanlu opened this issue Jun 23, 2015 · 3 comments

Comments

@astrojuanlu
Copy link
Contributor

After applying numba.njit to a function, float division behavior is no longer that of NumPy, but the one of Python:

# coding: utf-8
import numpy as np
from numba import njit

def func_ok(a, b):
    c = np.sin(a * b)
    return np.divide(a - b, c)

numba_func_ok = njit(func_ok)


def func_fail(a, b):
    c = np.sin(a * b)
    return (a - b) / c

numba_func_fail = njit(func_fail)

if __name__ == '__main__':
    print(func_ok(0., 0.))  # nan
    print(numba_func_ok(0., 0.))  # nan
    print(func_fail(0., 0.))  # nan
    print(numba_func_fail(0., 0.))  # ZeroDivisionError

My workaround is to use numpy.divide, which luckily is supported in nopython mode.

@astrojuanlu astrojuanlu changed the title Float division is no longer IEEE754 after njit Float division is no longer IEEE 754 after njit Jun 23, 2015
@pitrou
Copy link
Contributor

pitrou commented Jun 23, 2015

Unfortunately we can't implement both semantics at the same time. We chose to implement the Python semantics over scalars and the Numpy semantics over Numpy arrays.

@seibert
Copy link
Contributor

seibert commented Jun 23, 2015

We could provide a mechanism to disable raising zero division errors. This would also have a performance benefit for those who are willing to deal with the NaN.

@seibert
Copy link
Contributor

seibert commented Nov 3, 2017

this is now available with the error_model="python" or error_model="numpy" argument to @jit

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

No branches or pull requests

3 participants