Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Support integer dtype inputs in rounding functions #26766

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 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/release/upcoming_changes/26766.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* `numpy.floor`, `numpy.ceil`, and `numpy.trunc` now won't perform casting
to a floating dtype for integer and boolean dtype input arrays.
3 changes: 3 additions & 0 deletions numpy/_core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ def english_upper(s):
Ufunc(1, 1, None,
docstrings.get('numpy._core.umath.ceil'),
None,
TD(bints),
TD('e', f='ceil', astype={'e': 'f'}),
TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]),
TD('fdg', f='ceil'),
Expand All @@ -962,6 +963,7 @@ def english_upper(s):
Ufunc(1, 1, None,
docstrings.get('numpy._core.umath.trunc'),
None,
TD(bints),
TD('e', f='trunc', astype={'e': 'f'}),
TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]),
TD('fdg', f='trunc'),
Expand All @@ -978,6 +980,7 @@ def english_upper(s):
Ufunc(1, 1, None,
docstrings.get('numpy._core.umath.floor'),
None,
TD(bints),
TD('e', f='floor', astype={'e': 'f'}),
TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]),
TD('fdg', f='floor'),
Expand Down
8 changes: 7 additions & 1 deletion numpy/_core/src/umath/loops.h.src
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ BOOL_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void
#include "loops_autovec.dispatch.h"
#endif
/**begin repeat
* #kind = isnan, isinf, isfinite#
* #kind = isnan, isinf, isfinite, floor, ceil, trunc#
*/
NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT void BOOL_@kind@,
(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)))
Expand Down Expand Up @@ -179,6 +179,12 @@ NPY_NO_EXPORT void
NPY_NO_EXPORT void
@S@@TYPE@_positive(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func));

/**begin repeat2
* #kind = floor, ceil, trunc#
*/
#define @S@@TYPE@_@kind@ @S@@TYPE@_positive
/**end repeat2**/

/**begin repeat2
* Arithmetic
* #kind = add, subtract, multiply, bitwise_and, bitwise_or, bitwise_xor,
Expand Down
11 changes: 11 additions & 0 deletions numpy/_core/src/umath/loops_autovec.dispatch.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(BOOL_@kind@)
}
/**end repeat**/

/**begin repeat
* Identity
* #kind = floor, ceil, trunc#
*/
NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(BOOL_@kind@)
(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func))
{
UNARY_LOOP_FAST(npy_bool, npy_bool, *out = in);
}
/**end repeat**/

/*
*****************************************************************************
** HALF-FLOAT LOOPS **
Expand Down
9 changes: 9 additions & 0 deletions numpy/_core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4174,6 +4174,15 @@ def test_fraction(self):
assert_equal(np.ceil(f), -1)
assert_equal(np.trunc(f), -1)

@pytest.mark.parametrize('func', [np.floor, np.ceil, np.trunc])
@pytest.mark.parametrize('dtype', [np.bool, np.float64, np.float32,
np.int64, np.uint32])
def test_output_dtype(self, func, dtype):
arr = np.array([-2, 0, 4, 8]).astype(dtype)
result = func(arr)
assert_equal(arr, result)
assert result.dtype == dtype


class TestComplexFunctions:
funcs = [np.arcsin, np.arccos, np.arctan, np.arcsinh, np.arccosh,
Expand Down
5 changes: 0 additions & 5 deletions tools/ci/array-api-skips.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# finfo return type misalignment
array_api_tests/test_data_type_functions.py::test_finfo[float32]

# for int inputs out.dtype=float32, but should be int
array_api_tests/test_operators_and_elementwise_functions.py::test_ceil
array_api_tests/test_operators_and_elementwise_functions.py::test_floor
array_api_tests/test_operators_and_elementwise_functions.py::test_trunc

# 'shape' arg is present. 'newshape' is retained for backward compat.
array_api_tests/test_signatures.py::test_func_signature[reshape]

Expand Down
Loading