Skip to content

Commit 80d7649

Browse files
committed
Add more tests to cover different use cases
1 parent fcb4efb commit 80d7649

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

dpnp/backend/kernels/elementwise_functions/spacing.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ struct SpacingFunctor
4646
if (sycl::isnan(x)) {
4747
return x;
4848
}
49-
else if (sycl::isinf(x)) {
49+
50+
if (sycl::isinf(x)) {
5051
return std::numeric_limits<resT>::quiet_NaN();
5152
}
52-
return sycl::nextafter(x, x + 1) - x;
53+
54+
const argT y = sycl::copysign(std::numeric_limits<argT>::infinity(), x);
55+
return sycl::nextafter(x, y) - x;
5356
}
5457
};
5558
} // namespace dpnp::kernels::spacing

dpnp/dpnp_iface_mathematical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3799,7 +3799,7 @@ def real_if_close(a, tol=100):
37993799
"""
38003800

38013801
spacing = DPNPUnaryFunc(
3802-
"fabs",
3802+
"spacing",
38033803
ufi._spacing_result_type,
38043804
ufi._spacing,
38053805
_SPACING_DOCSTRING,

tests/test_mathematical.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,75 @@ def test_wrong_tol_type(self, xp, tol_val):
18971897
assert_raises(TypeError, xp.real_if_close, a, tol=tol_val)
18981898

18991899

1900+
class TestSpacing:
1901+
@pytest.mark.parametrize("dt", get_float_dtypes())
1902+
def test_basic(self, dt):
1903+
a = numpy.array(
1904+
[1, numpy.nan, numpy.inf, 0.0, 1e10, 1e-5, 1000, 10500], dtype=dt
1905+
)
1906+
ia = dpnp.array(a)
1907+
1908+
result = dpnp.spacing(ia)
1909+
expected = numpy.spacing(a)
1910+
assert_equal(result, expected)
1911+
1912+
# switch to negatives
1913+
result = dpnp.spacing(-ia)
1914+
expected = numpy.spacing(-a)
1915+
assert_equal(result, expected)
1916+
1917+
@pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False))
1918+
@pytest.mark.parametrize("val", [1, 1e-5, 1000])
1919+
@pytest.mark.parametrize("xp", [numpy, dpnp])
1920+
def test_vs_nextafter(self, val, dt, xp):
1921+
a = xp.array(val, dtype=dt)
1922+
a1 = xp.array(val + 1, dtype=dt)
1923+
assert (xp.nextafter(a, a1) - a) == xp.spacing(a)
1924+
1925+
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
1926+
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
1927+
def test_fp16(self):
1928+
a = numpy.arange(0x7C00, dtype=numpy.uint16)
1929+
1930+
# all values are non-negative finites
1931+
b = a.view(dtype=numpy.float16)
1932+
ib = dpnp.array(b)
1933+
1934+
result = dpnp.spacing(ib)
1935+
expected = numpy.spacing(b)
1936+
assert_equal(result, expected)
1937+
1938+
# switch to negatives
1939+
a |= 0x8000
1940+
ib = dpnp.array(b)
1941+
1942+
result = dpnp.spacing(ib)
1943+
expected = numpy.spacing(b)
1944+
assert_equal(result, expected)
1945+
1946+
@pytest.mark.parametrize("dt", get_integer_dtypes())
1947+
def test_integer(self, dt):
1948+
a = numpy.array([1, 0, -3], dtype=dt)
1949+
ia = dpnp.array(a)
1950+
1951+
result = dpnp.spacing(ia)
1952+
expected = numpy.spacing(a)
1953+
assert_dtype_allclose(result, expected)
1954+
1955+
def test_bool(self):
1956+
a = numpy.array([True, False])
1957+
ia = dpnp.array(a)
1958+
1959+
result = dpnp.spacing(ia)
1960+
expected = numpy.spacing(a)
1961+
assert_dtype_allclose(result, expected)
1962+
1963+
@pytest.mark.parametrize("xp", [numpy, dpnp])
1964+
def test_complex(self, xp):
1965+
a = xp.array([2.1 + 4e-14j, 5.2 + 3e-15j])
1966+
assert_raises((TypeError, ValueError), xp.spacing, a)
1967+
1968+
19001969
class TestTrapezoid:
19011970
def get_numpy_func(self):
19021971
if numpy.lib.NumpyVersion(numpy.__version__) < "2.0.0":

0 commit comments

Comments
 (0)