question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.spacing documentation inaccuracies

See original GitHub issue

The current numpy.spacing documentation seems inaccurate. At the top, it says:

Return the distance between x and the nearest adjacent number.

But this isn’t right for powers of 2: for example, if x = 1.0, the nearest representable float to x is 1.0 - 2**-53, so the distance would be 2.**-53. But in this case, spacing gives 2.0**-52.

It’s also not immediately clear from the description what the expected sign of the result is. From experimentation, it looks as though np.spacing(-x) is -np.spacing(x), except in the case of zeros, where the result is the same for both negative and positive zeros.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
mdickinsoncommented, Jan 15, 2020

On the sign, having the sign of the result match the sign of the input seems a perfectly reasonable choice; I think it’s potentially valuable that a + np.spacing(a) gives the next-away-from-zero value from a. I’m not suggesting any behaviour change here - even without backwards compatibility concerns it doesn’t seem a clear choice either way (and as you say, with backwards compatibility concerns, it is clear that this shouldn’t be changed).

The -0.0 corner case is a bit surprising, in that it’s the only case where the sign of the result doesn’t match the sign of the input. OTOH, there’s an argument for having -0.0 and 0.0 behave identically in almost all numeric contexts, with a few well-known exceptions. Both behaviours seem reasonable, but again it would be good to document.

For the np.finfo(np.float64).max case, I guess I find this surprising because I don’t think of the output as being a difference so much as being the value of the least significant place in the given float. OTOH, the “spacing” name clearly indicates that this should be thought of as a difference.

But I agree that all the current choices seem reasonable, and that this is really just a documentation issue.

For context, I was looking at this mostly because CPython just implemented math.ulp, with a similar purpose but a slightly different set of choices: https://bugs.python.org/issue39310.

1reaction
mattipcommented, Jan 16, 2020

Are the new math.nextafter and math.ulp going to cause us a whole new set of edge-case incompatibilities with the numpy definitions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.spacing — NumPy v1.24 Manual
For other keyword-only arguments, see the ufunc docs. Returns: outndarray or scalar. The spacing of values of x. This is a scalar if...
Read more >
non-uniform spacing with numpy.gradient - Stack Overflow
I'm wondering if this is a problem on my end. I can't get the same answers as the numpy.gradient documentation, following their examples....
Read more >
Style guide — numpydoc v1.6.0rc1.dev0 Manual
This document describes the current community consensus for such a standard. If you have suggestions for improvements, post them on the numpy-discussion list....
Read more >
np.linspace(): Create Evenly or Non-Evenly Spaced Arrays
In this tutorial, you'll learn how to use NumPy's np.linspace() ... In the example above, you create a linear space with 25 values...
Read more >
What is the numpy.spacing() function in NumPy? - Educative.io
The numpy.spacing() function takes the following parameter values. x : This represents the input value whose spacing is to be returned and is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found