Skip to content
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

Generate subnormal floats #3152

Merged
merged 1 commit into from Nov 18, 2021
Merged

Conversation

Zac-HD
Copy link
Member

@Zac-HD Zac-HD commented Nov 16, 2021

It turns out that we can generate subnormal values; it's just that we do so with negligible probability because they only occur for one-in-2^11 possible exponents. The obvious workaround is to just add a handful of special values to our NASTY_FLOATS table, and so here's a PR to do just that. Fixes #2976.

@Zac-HD Zac-HD added the bug something is clearly wrong here label Nov 16, 2021
@Zac-HD Zac-HD force-pushed the subnormal-floats branch 2 times, most recently from bee92a0 to b3d4370 Compare November 16, 2021 12:07
@honno
Copy link
Member

honno commented Nov 17, 2021

Noting here that extra.array_api is one use case where we don't want to generate subnormal floats as it's not in scope. Interestingly CuPy handles subnormals differently to builtin Python/NumPy/etc..

Could there be a kwarg like allow_subnormal which defaults to True? Otherwise a recipe in the docs might be helpful, although even then maybe it really is just extra.array_api that cares not to generate these.

st.floats().filter(lambda n: abs(n) >= float_info.min)

@Zac-HD
Copy link
Member Author

Zac-HD commented Nov 17, 2021

Noting here that extra.array_api is one use case where we don't want to generate subnormal floats as it's not in scope. Interestingly CuPy handles subnormals differently to builtin Python/NumPy/etc..

I think this is incorrect - the spec currently says that float dtypes follow IEEE 754, which would require support for subnormals.

Evidently CuPy (/Nvidia hardware) doesn't strictly support IEEE 754, and perhaps the array API standard will be altered to make subnormals optional, but as it stands I consider this a bug in CuPy rather than Hypothesis.

Regardless, I'm open to a new allow_subnormal parameter, but in a different PR to this one since they're already supported.

Copy link
Member

@honno honno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite familiar with the existing sub-normal tests now, so LGTM.

@@ -114,11 +112,6 @@ def test_is_in_exact_int_range(x):
REALLY_SMALL_FLOAT = sys.float_info.min * 2
Copy link
Member

@honno honno Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is obvious to somebody who didn't just learn about the concept of sub-normals today (me), but I couldn't figure this out. My guess is that you could use an interpreter built in one architecture but run this in the other architecture, so sys.float_info could be incorrect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, it'll be if the interpreter was built with various unsafe compiler flags which flush denormals to zero (e.g. -funsafe-math-optimizations). Which is terrible, but here we are.

(I'll clarify the test and comment, and make this a skipif)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

Successfully merging this pull request may close these issues.

floats() does not generate subnormal floating-point values
2 participants