Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upFloat strategy generates -0.0 for max_value=0.0 when exclude_max=True #2201
Comments
This comment has been minimized.
This comment has been minimized.
FWIW this was intentional as it exposes the underlying - and I agree unintuitive - behaviour of IEEE754 floats regarding signed zeros. I can see that it might be a better compromise to ensure that excluding either zero excludes both though! |
This comment has been minimized.
This comment has been minimized.
I think regardless of what we decide as the API our documentation of the floats strategy needs to clearly signal it - right now it's pretty non-obvious from the documentation that this is the behaviour! |
This comment has been minimized.
This comment has been minimized.
Was there any reason in particular to distinguish between signed/unsigned zeros in next_up? In any case, should this still be the desired behaviour, then I definitely agree that the docs should be made clearer! But I do think that the API should exclude both zeros in exclude_min/max. |
This comment has been minimized.
This comment has been minimized.
Yes - they are in fact distinct values, and code - including |
This comment has been minimized.
This comment has been minimized.
I understand that they are distinct values and that treating -0.0 as 0.0, as was done before, would break the property you describe. My point was just that other properties one would intuitively expect are Is it more important to guarantee that Anyway, thanks for the helpful responses! |
This comment has been minimized.
This comment has been minimized.
Huh, it turns out that the IEEE754 standard prefers the opposite convention to us: " Fortunately our I'm more confident now that changing the public-facing behaviour is correct, though! |
As a user, I'd expect values drawn from a strategy with
exclude_max=True
to have the invariantvalue < max_value
. However, I discovered that a floats strategy with max_value set to 0.0 can generate -0.0 even ifexclude_max=True
. This is unexpected since 0.0 == -0.0.Example program (hypothesis 4.43.6):
Example output (excluding traceback):
The issue seems to stem from changes to
floats.py
in 36d641d. The version of prior to this commit matches the referenced Stack Overflow solution and would not cause this particular issue.If I understood it correctly, PR #1860 solved the issue of
floats(min_value=0.0, max_value=-0.0)
and excluding endpoints at infinity ( #1859).Perhaps this was an unintended side-effect?
For now, I can avoid this with, for instance:
but this should not really be necessary. Would it be possible to consolidate solutions to both issues?