| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent eba13d8 commit ce1093b
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -365,7 +365,7 @@ def test_p_code(self): | |||
| 365 | 365 | (got,) = struct.unpack(code, got) | |
| 366 | 366 | self.assertEqual(got, expectedback) | |
| 367 | 367 | ||
| 368 | - def check_705836(self, format, reverse_format): | ||
| 368 | + def test_705836(self): | ||
| 369 | 369 | # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry | |
| 370 | 370 | # from the low-order discarded bits could propagate into the exponent | |
| 371 | 371 | # field, causing the result to be wrong by a factor of 2. | |
@@ -376,33 +376,42 @@ def check_705836(self, format, reverse_format): | |||
| 376 | 376 | delta /= 2.0 | |
| 377 | 377 | smaller = base - delta | |
| 378 | 378 | # Packing this rounds away a solid string of trailing 1 bits. | |
| 379 | - packed = struct.pack(format, smaller) | ||
| 380 | - unpacked = struct.unpack(format, packed)[0] | ||
| 379 | + packed = struct.pack("<f", smaller) | ||
| 380 | + unpacked = struct.unpack("<f", packed)[0] | ||
| 381 | 381 | # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and | |
| 382 | 382 | # 16, respectively. | |
| 383 | 383 | self.assertEqual(base, unpacked) | |
| 384 | - | ||
| 385 | - bigpacked = struct.pack(reverse_format, smaller) | ||
| 384 | + bigpacked = struct.pack(">f", smaller) | ||
| 386 | 385 | self.assertEqual(bigpacked, string_reverse(packed)) | |
| 387 | - unpacked = struct.unpack(reverse_format, bigpacked)[0] | ||
| 386 | + unpacked = struct.unpack(">f", bigpacked)[0] | ||
| 388 | 387 | self.assertEqual(base, unpacked) | |
| 389 | 388 | ||
| 390 | 389 | # Largest finite IEEE single. | |
| 391 | 390 | big = (1 << 24) - 1 | |
| 392 | 391 | big = math.ldexp(big, 127 - 23) | |
| 393 | - packed = struct.pack(format, big) | ||
| 394 | - unpacked = struct.unpack(format, packed)[0] | ||
| 392 | + packed = struct.pack(">f", big) | ||
| 393 | + unpacked = struct.unpack(">f", packed)[0] | ||
| 395 | 394 | self.assertEqual(big, unpacked) | |
| 396 | 395 | ||
| 397 | 396 | # The same, but tack on a 1 bit so it rounds up to infinity. | |
| 398 | 397 | big = (1 << 25) - 1 | |
| 399 | 398 | big = math.ldexp(big, 127 - 24) | |
| 400 | - self.assertRaises(OverflowError, struct.pack, format, big) | ||
| 401 | - | ||
| 402 | - def test_705836(self): | ||
| 403 | - self.check_705836("<f", ">f") | ||
| 404 | - self.check_705836(">f", "<f") | ||
| 405 | - self.check_705836("f", "<f" if sys.byteorder == "big" else ">f") | ||
| 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) | ||
| 406 | 415 | ||
| 407 | 416 | def test_1530559(self): | |
| 408 | 417 | for code, byteorder in iter_integer_formats(): | |
@@ -1207,20 +1216,6 @@ def test_half_float(self): | |||
| 1207 | 1216 | for formatcode, bits, f in format_bits_float__doubleRoundingError_list: | |
| 1208 | 1217 | self.assertEqual(bits, struct.pack(formatcode, f)) | |
| 1209 | 1218 | ||
| 1210 | - def test_float_round_trip(self): | ||
| 1211 | - for format in ("f", "<f", ">f", "d", "<d", ">d"): | ||
| 1212 | - with self.subTest(format=format): | ||
| 1213 | - f = struct.unpack(format, struct.pack(format, 1.5))[0] | ||
| 1214 | - self.assertEqual(f, 1.5) | ||
| 1215 | - f = struct.unpack(format, struct.pack(format, NAN))[0] | ||
| 1216 | - self.assertTrue(math.isnan(f), f) | ||
| 1217 | - f = struct.unpack(format, struct.pack(format, INF))[0] | ||
| 1218 | - self.assertTrue(math.isinf(f), f) | ||
| 1219 | - self.assertEqual(math.copysign(1.0, f), 1.0) | ||
| 1220 | - f = struct.unpack(format, struct.pack(format, -INF))[0] | ||
| 1221 | - self.assertTrue(math.isinf(f), f) | ||
| 1222 | - self.assertEqual(math.copysign(1.0, f), -1.0) | ||
| 1223 | - | ||
| 1224 | 1219 | ||
| 1225 | 1220 | if __name__ == '__main__': | |
| 1226 | 1221 | unittest.main() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments