Skip to content

Commit 6c91567

Browse files
committed
ENH: Support integer & bool dtype inputs in rounding functions.
1 parent 0acdad6 commit 6c91567

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* `numpy.floor`, `numpy.ceil`, and `numpy.trunc` now won't perform casting
2+
to a floating dtype for integer and boolean dtype input arrays.

numpy/_core/code_generators/generate_umath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ def english_upper(s):
953953
Ufunc(1, 1, None,
954954
docstrings.get('numpy._core.umath.ceil'),
955955
None,
956+
TD(bints),
956957
TD('e', f='ceil', astype={'e': 'f'}),
957958
TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]),
958959
TD('fdg', f='ceil'),
@@ -962,6 +963,7 @@ def english_upper(s):
962963
Ufunc(1, 1, None,
963964
docstrings.get('numpy._core.umath.trunc'),
964965
None,
966+
TD(bints),
965967
TD('e', f='trunc', astype={'e': 'f'}),
966968
TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]),
967969
TD('fdg', f='trunc'),
@@ -978,6 +980,7 @@ def english_upper(s):
978980
Ufunc(1, 1, None,
979981
docstrings.get('numpy._core.umath.floor'),
980982
None,
983+
TD(bints),
981984
TD('e', f='floor', astype={'e': 'f'}),
982985
TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]),
983986
TD('fdg', f='floor'),

numpy/_core/src/umath/loops.h.src

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ BOOL_@kind@(char **args, npy_intp const *dimensions, npy_intp const *steps, void
7777
#include "loops_autovec.dispatch.h"
7878
#endif
7979
/**begin repeat
80-
* #kind = isnan, isinf, isfinite#
80+
* #kind = isnan, isinf, isfinite, floor, ceil, trunc#
8181
*/
8282
NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT void BOOL_@kind@,
8383
(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)))
@@ -179,6 +179,12 @@ NPY_NO_EXPORT void
179179
NPY_NO_EXPORT void
180180
@S@@TYPE@_positive(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func));
181181

182+
/**begin repeat2
183+
* #kind = floor, ceil, trunc#
184+
*/
185+
#define @S@@TYPE@_@kind@ @S@@TYPE@_positive
186+
/**end repeat2**/
187+
182188
/**begin repeat2
183189
* Arithmetic
184190
* #kind = add, subtract, multiply, bitwise_and, bitwise_or, bitwise_xor,

numpy/_core/src/umath/loops_autovec.dispatch.c.src

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(BOOL_@kind@)
264264
}
265265
/**end repeat**/
266266

267+
/**begin repeat
268+
* Identity
269+
* #kind = floor, ceil, trunc#
270+
*/
271+
NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(BOOL_@kind@)
272+
(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func))
273+
{
274+
UNARY_LOOP_FAST(npy_bool, npy_bool, *out = in);
275+
}
276+
/**end repeat**/
277+
267278
/*
268279
*****************************************************************************
269280
** HALF-FLOAT LOOPS **

numpy/_core/tests/test_umath.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,6 +4174,15 @@ def test_fraction(self):
41744174
assert_equal(np.ceil(f), -1)
41754175
assert_equal(np.trunc(f), -1)
41764176

4177+
@pytest.mark.parametrize('func', [np.floor, np.ceil, np.trunc])
4178+
@pytest.mark.parametrize('dtype', [np.bool, np.float64, np.float32,
4179+
np.int64, np.uint32])
4180+
def test_output_dtype(self, func, dtype):
4181+
arr = np.array([-2, 0, 4, 8]).astype(dtype)
4182+
result = func(arr)
4183+
assert_equal(arr, result)
4184+
assert result.dtype == dtype
4185+
41774186

41784187
class TestComplexFunctions:
41794188
funcs = [np.arcsin, np.arccos, np.arctan, np.arcsinh, np.arccosh,

tools/ci/array-api-skips.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# finfo return type misalignment
22
array_api_tests/test_data_type_functions.py::test_finfo[float32]
33

4-
# for int inputs out.dtype=float32, but should be int
5-
array_api_tests/test_operators_and_elementwise_functions.py::test_ceil
6-
array_api_tests/test_operators_and_elementwise_functions.py::test_floor
7-
array_api_tests/test_operators_and_elementwise_functions.py::test_trunc
8-
94
# 'shape' arg is present. 'newshape' is retained for backward compat.
105
array_api_tests/test_signatures.py::test_func_signature[reshape]
116

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy