(self)
| 366 | self.assertEqual(got, expectedback) |
| 367 | |
| 368 | def test_705836(self): |
| 369 | # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry |
| 370 | # from the low-order discarded bits could propagate into the exponent |
| 371 | # field, causing the result to be wrong by a factor of 2. |
| 372 | for base in range(1, 33): |
| 373 | # smaller <- largest representable float less than base. |
| 374 | delta = 0.5 |
| 375 | while base - delta / 2.0 != base: |
| 376 | delta /= 2.0 |
| 377 | smaller = base - delta |
| 378 | # Packing this rounds away a solid string of trailing 1 bits. |
| 379 | packed = struct.pack("<f", smaller) |
| 380 | unpacked = struct.unpack("<f", packed)[0] |
| 381 | # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and |
| 382 | # 16, respectively. |
| 383 | self.assertEqual(base, unpacked) |
| 384 | bigpacked = struct.pack(">f", smaller) |
| 385 | self.assertEqual(bigpacked, string_reverse(packed)) |
| 386 | unpacked = struct.unpack(">f", bigpacked)[0] |
| 387 | self.assertEqual(base, unpacked) |
| 388 | |
| 389 | # Largest finite IEEE single. |
| 390 | big = (1 << 24) - 1 |
| 391 | big = math.ldexp(big, 127 - 23) |
| 392 | packed = struct.pack(">f", big) |
| 393 | unpacked = struct.unpack(">f", packed)[0] |
| 394 | self.assertEqual(big, unpacked) |
| 395 | |
| 396 | # The same, but tack on a 1 bit so it rounds up to infinity. |
| 397 | big = (1 << 25) - 1 |
| 398 | big = math.ldexp(big, 127 - 24) |
| 399 | self.assertRaises(OverflowError, struct.pack, ">f", big) |
| 400 | self.assertRaises(OverflowError, struct.pack, "<f", big) |
| 401 | # same for native format, see gh-145633 |
| 402 | self.assertRaises(OverflowError, struct.pack, "f", big) |
| 403 | |
| 404 | # And for half-floats |
| 405 | big = (1 << 11) - 1 |
| 406 | big = math.ldexp(big, 15 - 10) |
| 407 | packed = struct.pack(">e", big) |
| 408 | unpacked = struct.unpack(">e", packed)[0] |
| 409 | self.assertEqual(big, unpacked) |
| 410 | big = (1 << 12) - 1 |
| 411 | big = math.ldexp(big, 15 - 11) |
| 412 | self.assertRaises(OverflowError, struct.pack, ">e", big) |
| 413 | self.assertRaises(OverflowError, struct.pack, "<e", big) |
| 414 | self.assertRaises(OverflowError, struct.pack, "e", big) |
| 415 | |
| 416 | def test_1530559(self): |
| 417 | for code, byteorder in iter_integer_formats(): |
nothing calls this directly
no test coverage detected