[Numpy-discussion] numpy.spacing question
Ryan Nelson
rnelsonchem at gmail.com
Fri Dec 5 08:29:50 EST 2014
Alok Singhal <alok <at> edgestreamlp.com> writes:
>
> On Thu, Dec 4, 2014 at 4:25 PM, Ryan Nelson <rnelsonchem <at>
gmail.com> wrote:
> >
> > I guess I'm a little confused about how the spacing values are
calculated.
>
> np.spacing(x) is basically the same as np.nextafter(x, np.inf) - x,
> i.e., it returns the minimum positive number that can be added to x to
> get a number that's different from x.
>
> > 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?
>
> For 1e-10, np.spacing() is 1.2924697071141057e-26. 1e-10 * eps is
> 2.2204460492503132e-26, which, when added to 1e-10 rounds to the
> closest number that can be represented in a 64-bit floating-point
> representation. That happens to be 2*np.spacing(1e-10), and not
> 1*np.spacing(1e-10).
>
Thanks Nathaniel and Alok. Your explanations were very helpful. I was
expecting that all of those logical tests would come out True. It might
have been the example in the doc string for
`assert_array_almost_equal_nulp` that was throwing me off a little bit.
The precision test in that function is `np.abs(x-y) <= ref`, where `ref`
is the spacing for the largest values in the two arrays (which is `y` in
my case). In the doc string, this function is run comparing x to (x*eps
+ x), which seems like it shouldn't throw an error given the logical
test in the function. For example, if you change the following `x =
np.array([1., 1e-9, 1e-20])`, then the assert test function does not
throw an error for that example.
Anyway, I guess that is the problem with working at the last unit of
precision in these numbers... Pesky floating point values...
More information about the NumPy-Discussion
mailing list