Repository: ivy-llc/ivy

Issue:
spacing



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/numpy/__init__.py b/ivy/functional/frontends/numpy/__init__.py
--- a/ivy/functional/frontends/numpy/__init__.py
+++ b/ivy/functional/frontends/numpy/__init__.py
@@ -628,7 +628,7 @@ def promote_types_of_numpy_inputs(
 )
 
 from ivy.functional.frontends.numpy.mathematical_functions.floating_point_routines import (  # noqa
-    _nextafter,
+    _nextafter, _spacing,
 )
 
 _frontend_array = array
@@ -712,6 +712,7 @@ def promote_types_of_numpy_inputs(
 conjugate = ufunc("_conj")
 lcm = ufunc("_lcm")
 gcd = ufunc("_gcd")
+spacing = ufunc("_spacing")
 clip = ufunc("_clip")
 
 # setting to specific version #
diff --git a/ivy/functional/frontends/numpy/mathematical_functions/floating_point_routines.py b/ivy/functional/frontends/numpy/mathematical_functions/floating_point_routines.py
--- a/ivy/functional/frontends/numpy/mathematical_functions/floating_point_routines.py
+++ b/ivy/functional/frontends/numpy/mathematical_functions/floating_point_routines.py
@@ -10,7 +10,6 @@
     handle_numpy_casting,
 )
 
-
 @handle_numpy_out
 @handle_numpy_dtype
 @to_ivy_arrays_and_back
@@ -29,3 +28,31 @@ def _nextafter(
     subok=True,
 ):
     return ivy.nextafter(x1, x2, out=out)
+
+
+
+
+@handle_numpy_out
+@handle_numpy_dtype
+@to_ivy_arrays_and_back
+@handle_numpy_casting
+@from_zero_dim_arrays_to_scalar
+def _spacing(
+    x,
+    /,
+    out=None,
+    *,
+    where=True,
+    casting="same_kind",
+    order="K",
+    dtype=None,
+    subok=True,
+):
+    # Implement the frontend function using Ivy compositions
+    if dtype is None:
+        dtype = ivy.dtype(x)
+    y = ivy.floor(ivy.log2(ivy.abs(x + 1)))
+    spacing = ivy.multiply(ivy.finfo(dtype).eps, ivy.pow(2, y))
+    if dtype != "float16":
+        spacing = ivy.sign(x) * spacing
+    return spacing
\ No newline at end of file


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_floating_point_routines.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_floating_point_routines.py
--- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_floating_point_routines.py
+++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_floating_point_routines.py
@@ -3,7 +3,7 @@
 from ivy_tests.test_ivy.helpers import handle_frontend_test
 import ivy_tests.test_ivy.test_frontends.test_numpy.helpers as np_frontend_helpers
 
-
+# nextafter
 @handle_frontend_test(
     fn_tree="numpy.nextafter",
     dtypes_values_casting=np_frontend_helpers.dtypes_values_casting_dtype(
@@ -49,3 +49,51 @@ def test_numpy_nextafter(
         dtype=dtype,
         subok=True,
     )
+
+
+# spacing
+@handle_frontend_test(
+    fn_tree="numpy.spacing",
+    dtypes_values_casting=np_frontend_helpers.dtypes_values_casting_dtype(
+        arr_func=[
+            lambda: helpers.dtype_and_values(
+                available_dtypes=helpers.get_dtypes("float"),
+                num_arrays=1,
+            )
+        ],
+    ),
+    where=np_frontend_helpers.where(),
+    number_positional_args=np_frontend_helpers.get_num_positional_args_ufunc(
+        fn_name="spacing"
+    ),
+)
+def test_numpy_spacing(
+    dtypes_values_casting,
+    where,
+    frontend,
+    test_flags,
+    fn_tree,
+    on_device,
+):
+    input_dtypes, xs, casting, dtype = dtypes_values_casting
+    where, input_dtypes, test_flags = np_frontend_helpers.handle_where_and_array_bools(
+        where=where,
+        input_dtype=input_dtypes,
+        test_flags=test_flags,
+    )
+    np_frontend_helpers.test_frontend_function(
+        input_dtypes=input_dtypes,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        rtol=1e-03,
+        atol=1e-03,
+        x=xs[0],
+        out=None,
+        where=where,
+        casting=casting,
+        order="K",
+        dtype=dtype,
+        subok=True,
+    )
\ No newline at end of file