Skip to content

Commit e060521

Browse files
committed
Report signaling and -nan
This is a pretty niche problem to have, but when you have it it's really nice to know.
1 parent 13e9404 commit e060521

5 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎hypothesis-python/RELEASE.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RELEASE_TYPE: minor
2+
3+
Did you know that of the 2\ :superscript:`64` possible floating-point numbers,
4+
2\ :superscript:`53` of them are ``nan`` - and Python prints them all the same way?
5+
6+
While nans *usually* have all zeros in the sign bit and mantissa, this
7+
`isn't always true <https://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations>`__,
8+
and :wikipedia:`'signaling' nans might trap or error <https://en.wikipedia.org/wiki/NaN#Signaling_NaN>`.
9+
To help distinguish such errors in e.g. CI logs, Hypothesis now prints ``-nan`` for
10+
negative nans, and adds a comment like ``# Saw 3 signaling NaNs`` if applicable.

‎hypothesis-python/src/hypothesis/strategies/_internal/numbers.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def integers(
147147
return IntegersStrategy(min_value, max_value)
148148

149149

150+
SIGNALING_NAN = int_to_float(0x7FF8_0000_0000_0001) # nonzero mantissa
151+
assert math.isnan(SIGNALING_NAN) and math.copysign(1, SIGNALING_NAN) == 1
152+
150153
NASTY_FLOATS = sorted(
151154
[
152155
0.0,
@@ -170,7 +173,8 @@ def integers(
170173
]
171174
+ [2.0 ** -n for n in (24, 14, 149, 126)] # minimum (sub)normals for float16,32
172175
+ [float_info.min / n for n in (2, 10, 1000, 100_000)] # subnormal in float64
173-
+ [math.inf, math.nan] * 5,
176+
+ [math.inf, math.nan] * 5
177+
+ [SIGNALING_NAN],
174178
key=flt.float_to_lex,
175179
)
176180
NASTY_FLOATS = list(map(float, NASTY_FLOATS))

‎hypothesis-python/src/hypothesis/vendor/pretty.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ def _repr_pretty_(self, p, cycle):
7070
import datetime
7171
import platform
7272
import re
73+
import struct
7374
import types
7475
from collections import deque
7576
from contextlib import contextmanager
7677
from io import StringIO
78+
from math import copysign, isnan
7779

7880
__all__ = [
7981
"pretty",
@@ -161,6 +163,8 @@ def __init__(
161163
self.group_queue = GroupQueue(root_group)
162164
self.indentation = 0
163165

166+
self.snans = 0
167+
164168
def _break_outer_groups(self):
165169
while self.max_width < self.output_width + self.buffer_width:
166170
group = self.group_queue.deq()
@@ -266,6 +270,12 @@ def end_group(self, dedent=0, close=""):
266270

267271
def flush(self):
268272
"""Flush data that is left in the buffer."""
273+
if self.snans:
274+
# Reset self.snans *before* calling breakable(), which might flush()
275+
snans = self.snans
276+
self.snans = 0
277+
self.breakable(" ")
278+
self.text(f"# Saw {snans} signaling NaN" + "s" * (snans > 1))
269279
for data in self.buffer:
270280
self.output_width += data.output(self.output, self.output_width)
271281
self.buffer.clear()
@@ -735,10 +745,20 @@ def _exception_pprint(obj, p, cycle):
735745
p.end_group(step, ")")
736746

737747

748+
def _repr_float_counting_nans(obj, p, cycle):
749+
if isnan(obj) and hasattr(p, "snans"):
750+
if struct.pack("!d", abs(obj)) != struct.pack("!d", float("nan")):
751+
p.snans += 1
752+
if copysign(1.0, obj) == -1.0:
753+
p.text("-nan")
754+
return
755+
p.text(repr(obj))
756+
757+
738758
#: printers for builtin types
739759
_type_pprinters = {
740760
int: _repr_pprint,
741-
float: _repr_pprint,
761+
float: _repr_float_counting_nans,
742762
str: _repr_pprint,
743763
tuple: _seq_pprinter_factory("(", ")", tuple),
744764
list: _seq_pprinter_factory("[", "]", list),

‎hypothesis-python/tests/cover/test_pretty.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import pytest
6060

6161
from hypothesis.internal.compat import PYPY
62+
from hypothesis.strategies._internal.numbers import SIGNALING_NAN
6263
from hypothesis.vendor import pretty
6364

6465

@@ -622,3 +623,17 @@ def test_empty_printer():
622623

623624
def test_breakable_at_group_boundary():
624625
assert "\n" in pretty.pretty([[], "000000"], max_width=5)
626+
627+
628+
@pytest.mark.parametrize(
629+
"obj, rep",
630+
[
631+
(float("nan"), "nan"),
632+
(-float("nan"), "-nan"),
633+
(SIGNALING_NAN, "nan # Saw 1 signaling NaN"),
634+
(-SIGNALING_NAN, "-nan # Saw 1 signaling NaN"),
635+
((SIGNALING_NAN, SIGNALING_NAN), "(nan, nan) # Saw 2 signaling NaNs"),
636+
],
637+
)
638+
def test_nan_reprs(obj, rep):
639+
assert pretty.pretty(obj) == rep

‎hypothesis-python/tests/nocover/test_floating.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
import pytest
2222

2323
from hypothesis import HealthCheck, assume, given, settings
24-
from hypothesis.internal.floats import next_down
24+
from hypothesis.internal.floats import float_to_int, next_down
2525
from hypothesis.strategies import data, floats, lists
2626

27+
from tests.common.debug import find_any
2728
from tests.common.utils import fails
2829

2930
TRY_HARDER = settings(
@@ -161,3 +162,16 @@ def test_floats_are_in_range(x, y, data):
161162

162163
t = data.draw(floats(x, y))
163164
assert x <= t <= y
165+
166+
167+
@pytest.mark.parametrize("neg", [False, True])
168+
@pytest.mark.parametrize("snan", [False, True])
169+
def test_can_find_negative_and_signaling_nans(neg, snan):
170+
find_any(
171+
floats().filter(math.isnan),
172+
lambda x: (
173+
snan is (float_to_int(abs(x)) != float_to_int(float("nan")))
174+
and neg is (math.copysign(1, x) == -1)
175+
),
176+
settings=TRY_HARDER,
177+
)

0 commit comments

Comments
 (0)