MCPcopy Index your code
hub / github.com/numpy/numpy / nulp_diff

Function nulp_diff

numpy/testing/_private/utils.py:1899–1958  ·  view source on GitHub ↗

For each item in x and y, return the number of representable floating points between them. Parameters ---------- x : array_like first input array y : array_like second input array dtype : dtype, optional Data-type to convert `x` and `y` to if given. D

(x, y, dtype=None)

Source from the content-addressed store, hash-verified

1897
1898
1899def nulp_diff(x, y, dtype=None):
1900 """For each item in x and y, return the number of representable floating
1901 points between them.
1902
1903 Parameters
1904 ----------
1905 x : array_like
1906 first input array
1907 y : array_like
1908 second input array
1909 dtype : dtype, optional
1910 Data-type to convert `x` and `y` to if given. Default is None.
1911
1912 Returns
1913 -------
1914 nulp : array_like
1915 number of representable floating point numbers between each item in x
1916 and y.
1917
1918 Notes
1919 -----
1920 For computing the ULP difference, this API does not differentiate between
1921 various representations of NAN (ULP difference between 0x7fc00000 and 0xffc00000
1922 is zero).
1923
1924 Examples
1925 --------
1926 # By definition, epsilon is the smallest number such as 1 + eps != 1, so
1927 # there should be exactly one ULP between 1 and 1 + eps
1928 >>> nulp_diff(1, 1 + np.finfo(x.dtype).eps)
1929 1.0
1930 """
1931 import numpy as np
1932 if dtype:
1933 x = np.asarray(x, dtype=dtype)
1934 y = np.asarray(y, dtype=dtype)
1935 else:
1936 x = np.asarray(x)
1937 y = np.asarray(y)
1938
1939 t = np.common_type(x, y)
1940 if np.iscomplexobj(x) or np.iscomplexobj(y):
1941 raise NotImplementedError("_nulp not implemented for complex array")
1942
1943 x = np.array([x], dtype=t)
1944 y = np.array([y], dtype=t)
1945
1946 x[np.isnan(x)] = np.nan
1947 y[np.isnan(y)] = np.nan
1948
1949 if not x.shape == y.shape:
1950 raise ValueError(f"Arrays do not have the same shape: {x.shape} - {y.shape}")
1951
1952 def _diff(rx, ry, vdt):
1953 diff = np.asarray(rx - ry, dtype=vdt)
1954 return np.abs(diff)
1955
1956 rx = integer_repr(x)

Callers 2

assert_array_max_ulpFunction · 0.85

Calls 2

integer_reprFunction · 0.85
_diffFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…