8000 ENH: Make numpy floor_divide and remainder agree with Python `//` and `%`. by charris · Pull Request #7258 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Make numpy floor_divide and remainder agree with Python // and %. #7258

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

Merged
merged 6 commits into from
Feb 21, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations 8000
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: Make numpy ufuncs compatible with Python divmod.
The following numpy ufuncs are reimplemented using the npy_divmod
function.

- remainder ('%')
- floor_divide ('//')

Example of differences

Currently

    In [1]: a = np.array(78 * 6e-8)

    In [2]: b = np.array(6e-8)

    In [3]: a // b
    Out[3]: 77.0

    In [4]: a % b
    Out[4]: 5.9999999999999651e-08

Previously

    In [1]: a = np.array(78 * 6e-8)

    In [2]: b = np.array(6e-8)

    In [3]: a // b
    Out[3]: 78.0

    In [4]: a % b
    Out[4]: 0.0

The first agrees with the Python values for the same operation and is a
bit more accurate for the input values as represented internally.

Closes #7224.
  • Loading branch information
charris committed Feb 19, 2016
commit 735174b88768999a2247f960669185978587408a
25 changes: 10 additions & 15 deletions numpy/core/src/umath/loops.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,8 @@ NPY_NO_EXPORT void
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = npy_floor@c@(in1/in2);
@type@ mod;
*((@type@ *)op1) = npy_divmod@c@(in1, in2, &mod);
}
}

Expand All @@ -1706,8 +1707,7 @@ NPY_NO_EXPORT void
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
const @type@ div = in1/in2;
*((@type@ *)op1) = in2*(div - npy_floor@c@(div));
npy_divmod@c@(in1, in2, (@type@ *)op1);
}
}

Expand Down Expand Up @@ -2011,25 +2011,20 @@ NPY_NO_EXPORT void
HALF_floor_divide(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
BINARY_LOOP {
const float in1 = npy_half_to_float(*(npy_half *)ip1);
const float in2 = npy_half_to_float(*(npy_half *)ip2);
*((npy_half *)op1) = npy_float_to_half(npy_floorf(in1/in2));
const npy_half in1 = *(npy_half *)ip1;
const npy_half in2 = *(npy_half *)ip2;
npy_half mod;
*((npy_half *)op1) = npy_half_divmod(in1, in2, &mod);
}
}

NPY_NO_EXPORT void
HALF_remainder(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
BINARY_LOOP {
const float in1 = npy_half_to_float(*(npy_half *)ip1);
const float in2 = npy_half_to_float(*(npy_half *)ip2);
const float res = npy_fmodf(in1,in2);
if (res && ((in2 < 0) != (res < 0))) {
*((npy_half *)op1) = npy_float_to_half(res + in2);
}
else {
*((npy_half *)op1) = npy_float_to_half(res);
}
const npy_half in1 = *(npy_half *)ip1;
const npy_half in2 = *(npy_half *)ip2;
npy_half_divmod(in1, in2, (npy_half *)op1);
}
}

Expand Down
0