[Numpy-discussion] numpy.spacing question

Ryan Nelson rnelsonchem at gmail.com
Thu Dec 4 19:25:01 EST 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141204/cfc0dd97/attachment.html>


More information about the NumPy-Discussion mailing list