@@ -308,6 +308,39 @@ def test_string_roundtrip():
308308 )
309309
310310
311+ def test_string_roundtrip_all_powers_of_two ():
312+ """Every exact power of two from the smallest subnormal up to overflow must
313+ round-trip through str() and repr(). Powers of two are the only values whose
314+ rounding interval is asymmetric, so they are the sole trigger for Dragon4
315+ margin bugs and are otherwise unreachable by random or decimal fuzzing."""
316+ two = QuadPrecision ("2.0" , backend = "sleef" )
317+ maxv = numpy_quaddtype .max_value
318+ p = numpy_quaddtype .smallest_subnormal
319+
320+ str_fails = []
321+ repr_fails = []
322+ tested = 0
323+ while True :
324+ tested += 1
325+ if QuadPrecision (str (p ), backend = "sleef" ) != p :
326+ str_fails .append (str (p ))
327+ repr_inner = repr (p ).split ("'" )[1 ]
328+ if QuadPrecision (repr_inner , backend = "sleef" ) != p :
329+ repr_fails .append (repr (p ))
330+ nxt = p * two
331+ if not (abs (nxt ) <= maxv ):
332+ break
333+ p = nxt
334+
335+ assert tested > 30000 , f"expected the full power-of-two sweep, only tested { tested } "
336+ assert not str_fails , (
337+ f"{ len (str_fails )} powers of two failed str() round-trip, e.g. { str_fails [:5 ]} "
338+ )
339+ assert not repr_fails , (
340+ f"{ len (repr_fails )} powers of two failed repr() round-trip, e.g. { repr_fails [:5 ]} "
341+ )
342+
343+
311344class TestBytesSupport :
312345 """Test suite for QuadPrecision bytes input support."""
313346
0 commit comments