Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: floating point errors with np.cos #23773

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

Closed
neutrinoceros opened this issue May 17, 2023 · 9 comments
Closed

BUG: floating point errors with np.cos #23773

neutrinoceros opened this issue May 17, 2023 · 9 comments
Labels

Comments

@neutrinoceros
Copy link
Contributor

Describe the issue:

#23695 affects the precision of cos (and presumably sin), in a way that may break downstream code: np.cos(0) == 0.9999999999999999 on the dev branch. I've tested this on Linux and MacOS

Here's a real life example https://github.com/SAIL-Labs/AMICAL/blob/97dffe6671babdfc71ac830610c0b7b8c5093f25/amical/analysis/fitting.py#L54-L60

This function relies on np.cos(0) being exactly 1.0 because its job is to invalidate inclination angles that have np.cos(angle) < 1.0.

I'm aware this function is intrinsically sensitive to floating point errors (arguably it shouldn't) and I would be okay with having to fix the regression on the user side, but I thought I would first open a discussion here as I imagine this could affect many downstream packages.

Reproduce the code example:

import numpy as np
assert np.cos(0) == 1.0

Error message:

AssertionError

Runtime information:

1.25.0.dev0+1456.g0200e4a00
3.11.0 (main, Nov 13 2022, 10:50:04) [Clang 14.0.0 (clang-1400.0.29.202)]

Context for the issue:

No response

@seberg
Copy link
Member

seberg commented May 17, 2023

The fun part is, that this was quite for off even before if a machine at AVX512 (typically one less ULP, but that if would be randomly wrong on such a machine in a range where it should round to 1!

The linked PR is the wrong one, that just fixes a small unrelated issue, gh-23399 is the relevant one and has more discussion, also ping @Mousius.

TBH, I would prefer higher precision around these special values, and that includes the old SVML fallback (which not super many users end up with anyway). This is all very hard to make clear cut calls on though, since I also think a check like the one you said is dubious.

(I also am surprised that the functions are non-monotenous, TBH. For these fast vectorized algorithms at least, results fluctuate randomly by 1-2 ULPs in regions where they should be numerically constant.)

@neutrinoceros
Copy link
Contributor Author

The linked PR is the wrong one

For closure, I identified #23695 as the cause using git bisect, which lead to c43ae85

@neutrinoceros
Copy link
Contributor Author

#23399 indeed provides much more context, thank you !
Seeing that astropy and SciPy already adapted to the change and that, even on already released versions of numpy np.sin(np.pi) != 0, I'm all the more tempted to just handle it downstream.

@seberg
Copy link
Member

seberg commented May 17, 2023

I am not sure that np.sin(np.pi) != 0 happened before in any context. However np.sin(np.pi+eps) != 0 did happen for sure, which isn't any better probably :).

@seberg
Copy link
Member

seberg commented May 17, 2023

The question is whether a pattern of a decent fraction of large scientific downstream packages noticing this (most will only test after the release), should worry us...

@neutrinoceros
Copy link
Contributor Author

I am not sure that np.sin(np.pi) != 0 happened before in any context.

I'm getting 1.2246467991473532e-16 with numpy 1.23.4 on macOS. This was also noted in #23399 (comment)

@bb-ssaxena99
Copy link

Is this issue still open? Happy to pick up if not worked on yet

@rkern
Copy link
Member

rkern commented May 31, 2023

@bb-ssaxena99 It's more of a policy discussion, not a work item at this time. Check the sprintable label for issues that are better for new contributors to work on.

@seberg
Copy link
Member

seberg commented Jun 21, 2023

I guess the discussion may not be over, but for this specific issue it is (its precise in 1.25 for the moment and maybe its more likely we go the context manager opt-in route, or maybe not...).

@seberg seberg closed this as completed Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants