omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / qualification / om2 / fixtures / oracle / kiv.py
| 1 | # Independent cross-check of kiv.out (bc): exact integers (Newton) and decimal at 120 digits. |
| 2 | import sys |
| 3 | from decimal import Decimal, getcontext, ROUND_FLOOR |
| 4 | getcontext().prec = 120 |
| 5 | def primes(n): |
| 6 | out, p = [], 2 |
| 7 | while len(out) < n: |
| 8 | if all(p % d for d in range(2, int(p**0.5) + 1)): out.append(p) |
| 9 | p += 1 |
| 10 | return out |
| 11 | def iroot(n, k): |
| 12 | x = 1 << ((n.bit_length() + k - 1) // k) |
| 13 | while True: |
| 14 | y = ((k - 1) * x + n // x ** (k - 1)) // k |
| 15 | if y >= x: return x |
| 16 | x = y |
| 17 | P = primes(64) |
| 18 | K = [iroot(p << 96, 3) % 2**32 for p in P] |
| 19 | H = [iroot(p << 64, 2) % 2**32 for p in P[:8]] |
| 20 | def dfrac(p, k): |
| 21 | r = Decimal(p) ** (Decimal(1) / Decimal(k)) |
| 22 | f = r - r.to_integral_value(rounding=ROUND_FLOOR) |
| 23 | return int((f * (Decimal(2) ** 32)).to_integral_value(rounding=ROUND_FLOOR)) |
| 24 | assert K == [dfrac(p, 3) for p in P] and H == [dfrac(p, 2) for p in P[:8]] |
| 25 | got = [l.split() for l in open(sys.argv[1])] |
| 26 | assert [int(g[3]) for g in got if g[0] == 'K'] == K and [int(g[3]) for g in got if g[0] == 'H'] == H |
| 27 | assert [int(g[2]) for g in got if g[0] == 'K'] == P |
| 28 | print("PASS bc K/H equal python exact-integer and decimal(120) derivations") |