# [Numpy-discussion] numpy.spacing question

Nathaniel Smith njs at pobox.com
Thu Dec 4 20:16:16 EST 2014

```It looks to me like spacing is calculating the 1ulp precision for each
of your numbers, while x*eps is suffering from a tidge of rounding
error and giving you 1-or-2 ulp precisions. Notice that the x*eps
values are either equal to or twice the values returned by spacing.

-n

On Fri, Dec 5, 2014 at 12:25 AM, Ryan Nelson <rnelsonchem at gmail.com> wrote:
> Hello everyone,
>
> I was working through the example usage for the test function
> `assert_array_almost_equal_nulp`, and it brought up a question regarding the
> function `spacing`. Here's some example code:
>
> ####
> import numpy as np
> from numpy.testing import assert_array_almost_equal_nulp
> np.set_printoptions(precision=50)
>
> x = np.array([1., 1e-10, 1e-20])
> eps = np.finfo(x.dtype).eps
> y = x*eps + x # y must be larger than x
> ####
>
> [In]: np.abs(x-y) <= np.spacing(y)
> [Out]: array([ True, False,  True], dtype=bool)
>
> [In]: np.spacing(y)
> [Out]: array([  2.22044604925031308084726333618164062500000000000000e-16,
>          1.29246970711410574198657608135931695869658142328262e-26,
>          1.50463276905252801019998276764447446760789191266827e-36])
>
> [In]: np.abs(x-y)
> [Out]: array([  2.22044604925031308084726333618164062500000000000000e-16,
>          2.58493941422821148397315216271863391739316284656525e-26,
>          1.50463276905252801019998276764447446760789191266827e-36])
>
> ####
>
> I guess I'm a little confused about how the spacing values are calculated.
> My expectation is that the first logical test should give an output array
> where all of the results are the same. But it is also very likely that I
> don't have any idea what's going on. Can someone provide some clarification?
>
> Thanks
>
> Ryan
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

--
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org

```