| 1 | """Regenerate the independent mixed-number comparison oracle; V tests need no Python.""" |
| 2 | |
| 3 | from fractions import Fraction |
| 4 | from pathlib import Path |
| 5 | import math |
| 6 | import random |
| 7 | import struct |
| 8 | |
| 9 | |
| 10 | def generate() -> str: |
| 11 | rng = random.Random(0x52355253434D50) |
| 12 | cases = [] |
| 13 | |
| 14 | def add(n: int, d: int, x: float) -> None: |
| 15 | if not math.isfinite(x): |
| 16 | return |
| 17 | exact = Fraction(n, d) |
| 18 | binary = Fraction.from_float(x) |
| 19 | order = (exact > binary) - (exact < binary) |
| 20 | bits = struct.unpack(">Q", struct.pack(">d", x))[0] |
| 21 | nearest = struct.unpack(">Q", struct.pack(">d", float(exact)))[0] |
| 22 | cases.append(f"{n}\t{d}\t{bits:016x}\t{order}\t{nearest:016x}") |
| 23 | |
| 24 | boundaries = [ |
| 25 | (0, 1), (1, 1), (-1, 1), (1, 10), (-1, 10), (1, 3), (-1, 3), |
| 26 | (2**53 + 1, 1), (-(2**53 + 1), 1), (2**63 - 1, 1), (-2**63, 1), |
| 27 | (1, 2**63 - 1), (-1, 2**63 - 1), (2**63 - 1, 2**63 - 2), |
| 28 | ] |
| 29 | for n, d in boundaries: |
| 30 | x = float(Fraction(n, d)) |
| 31 | for y in [x, math.nextafter(x, math.inf), math.nextafter(x, -math.inf), |
| 32 | 0.0, -0.0, 5e-324, -5e-324, 1e300, -1e300]: |
| 33 | add(n, d, y) |
| 34 | for _ in range(400): |
| 35 | n = rng.randrange(-2**63, 2**63) |
| 36 | d = rng.randrange(1, 2**63) |
| 37 | x = float(Fraction(n, d)) |
| 38 | for y in [x, math.nextafter(x, math.inf), math.nextafter(x, -math.inf)]: |
| 39 | add(n, d, y) |
| 40 | for _ in range(256): |
| 41 | n = rng.randrange(-2**63, 2**63) |
| 42 | d = rng.randrange(1, 2**63) |
| 43 | x = struct.unpack(">d", struct.pack(">Q", rng.getrandbits(64)))[0] |
| 44 | add(n, d, x) |
| 45 | return "# numerator\tdenominator\tIEEE754-bits\texact-order\tnearest-bits\n" + "\n".join(cases) + "\n" |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
| 49 | Path(__file__).with_name("mixed_numbers.tsv").write_text(generate()) |
| 50 | |