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)
| 1621 | |
| 1622 | |
| 1623 | def nulp_diff(x, y, dtype=None): |
| 1624 | """For each item in x and y, return the number of representable floating |
| 1625 | points between them. |
| 1626 | |
| 1627 | Parameters |
| 1628 | ---------- |
| 1629 | x : array_like |
| 1630 | first input array |
| 1631 | y : array_like |
| 1632 | second input array |
| 1633 | dtype : dtype, optional |
| 1634 | Data-type to convert `x` and `y` to if given. Default is None. |
| 1635 | |
| 1636 | Returns |
| 1637 | ------- |
| 1638 | nulp : array_like |
| 1639 | number of representable floating point numbers between each item in x |
| 1640 | and y. |
| 1641 | |
| 1642 | Notes |
| 1643 | ----- |
| 1644 | For computing the ULP difference, this API does not differentiate between |
| 1645 | various representations of NAN (ULP difference between 0x7fc00000 and 0xffc00000 |
| 1646 | is zero). |
| 1647 | |
| 1648 | Examples |
| 1649 | -------- |
| 1650 | # By definition, epsilon is the smallest number such as 1 + eps != 1, so |
| 1651 | # there should be exactly one ULP between 1 and 1 + eps |
| 1652 | >>> nulp_diff(1, 1 + np.finfo(x.dtype).eps) |
| 1653 | 1.0 |
| 1654 | """ |
| 1655 | import numpy as np |
| 1656 | if dtype: |
| 1657 | x = np.asarray(x, dtype=dtype) |
| 1658 | y = np.asarray(y, dtype=dtype) |
| 1659 | else: |
| 1660 | x = np.asarray(x) |
| 1661 | y = np.asarray(y) |
| 1662 | |
| 1663 | t = np.common_type(x, y) |
| 1664 | if np.iscomplexobj(x) or np.iscomplexobj(y): |
| 1665 | raise NotImplementedError("_nulp not implemented for complex array") |
| 1666 | |
| 1667 | x = np.array([x], dtype=t) |
| 1668 | y = np.array([y], dtype=t) |
| 1669 | |
| 1670 | x[np.isnan(x)] = np.nan |
| 1671 | y[np.isnan(y)] = np.nan |
| 1672 | |
| 1673 | if not x.shape == y.shape: |
| 1674 | raise ValueError("x and y do not have the same shape: %s - %s" % |
| 1675 | (x.shape, y.shape)) |
| 1676 | |
| 1677 | def _diff(rx, ry, vdt): |
| 1678 | diff = np.asarray(rx-ry, dtype=vdt) |
| 1679 | return np.abs(diff) |
| 1680 |
no test coverage detected