-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
numpy.spacing documentation inaccuracies #15331
Comments
One more corner case: I'm not sure whether this counts as a documentation issue or an implementation issue (or both, or neither):
It could be argued that the right result here is the distance between that max value and the next float down. (That distance being |
So I guess it actually is the nearest "larger" floating point number (by absolute value)... I agree that "distance" should really be an absolute value, I have never used this function, so I am not sure if there is some use, in that I think the The function is implemented as a bunch of bit-fiddling, could be easily adapted to return the absolute value, but for backward compatibility concerns.... The internal function could also be changed to return your value (it has a flag whether to return the larger or the smaller spacing and it is set to the larger spacing), but I would think the larger is just as well? |
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 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 For the 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 |
Well, you can spell that as |
Are the new |
@mattip I hope not. I'd hope that @eric-wieser I guess my point was that you can't spell that as |
Well, right now we still have a chance to change Python. E.g. if we think that our definition for the largest representable float is more reasonable ( I guess, we could at some point add |
Sorry, nvm. I see you already commented about the existence of |
So just for context, IIRC, I initially implemented those functions to implement some test functions in np.testing, that was the main use case. |
As of writing this comment the documentation is still wrong about the https://numpy.org/devdocs/reference/generated/numpy.spacing.html |
I was recently bitten by this inaccuracy, falsely assuming that If nobody else is working on this, I can open a PR:
A maybe useful invariant for visualizing current behaviour could be
which holds for all finite |
The current
numpy.spacing
documentation seems inaccurate. At the top, it says:But this isn't right for powers of 2: for example, if
x = 1.0
, the nearest representable float tox
is1.0 - 2**-53
, so the distance would be2.**-53
. But in this case,spacing
gives2.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.The text was updated successfully, but these errors were encountered: