()
| 9 | |
| 10 | |
| 11 | def test_short_halfway_cases(): |
| 12 | # exact halfway cases with a small number of significant digits |
| 13 | for k in 0, 5, 10, 15, 20: |
| 14 | # upper = smallest integer >= 2**54/5**k |
| 15 | upper = -(-2**54//5**k) |
| 16 | # lower = smallest odd number >= 2**53/5**k |
| 17 | lower = -(-2**53//5**k) |
| 18 | if lower % 2 == 0: |
| 19 | lower += 1 |
| 20 | for i in range(10 * TEST_SIZE): |
| 21 | # Select a random odd n in [2**53/5**k, |
| 22 | # 2**54/5**k). Then n * 10**k gives a halfway case |
| 23 | # with small number of significant digits. |
| 24 | n, e = random.randrange(lower, upper, 2), k |
| 25 | |
| 26 | # Remove any additional powers of 5. |
| 27 | while n % 5 == 0: |
| 28 | n, e = n // 5, e + 1 |
| 29 | assert n % 10 in (1, 3, 7, 9) |
| 30 | |
| 31 | # Try numbers of the form n * 2**p2 * 10**e, p2 >= 0, |
| 32 | # until n * 2**p2 has more than 20 significant digits. |
| 33 | digits, exponent = n, e |
| 34 | while digits < 10**20: |
| 35 | s = '{}e{}'.format(digits, exponent) |
| 36 | yield s |
| 37 | # Same again, but with extra trailing zeros. |
| 38 | s = '{}e{}'.format(digits * 10**40, exponent - 40) |
| 39 | yield s |
| 40 | digits *= 2 |
| 41 | |
| 42 | # Try numbers of the form n * 5**p2 * 10**(e - p5), p5 |
| 43 | # >= 0, with n * 5**p5 < 10**20. |
| 44 | digits, exponent = n, e |
| 45 | while digits < 10**20: |
| 46 | s = '{}e{}'.format(digits, exponent) |
| 47 | yield s |
| 48 | # Same again, but with extra trailing zeros. |
| 49 | s = '{}e{}'.format(digits * 10**40, exponent - 40) |
| 50 | yield s |
| 51 | digits *= 5 |
| 52 | exponent -= 1 |
| 53 | |
| 54 | def test_halfway_cases(): |
| 55 | # test halfway cases for the round-half-to-even rule |
no test coverage detected
searching dependent graphs…