yang
This project has not described itself yet.
Brought by drakestapleton
yang / scripts / ivmath_crosscheck.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Offline cross-check of the elementary functions used by the interval certificates. |
| 3 | |
| 4 | Emulates src/ufbi_math.om (uSin64 15th order, uExp64, uLog64) and the historical pre-dyadic certificate (ivSin64 23rd |
| 5 | order) operation by operation in IEEE binary64 and compares against 50-digit mpmath references. |
| 6 | Evidence only (floating point + sampling); it is not a proof of the error budget. |
| 7 | """ |
| 8 | import math, struct, sys |
| 9 | import mpmath as mp |
| 10 | |
| 11 | mp.mp.dps = 50 |
| 12 | PI = struct.unpack('<d', struct.pack('<Q', 0x400921fb54442d18))[0] |
| 13 | TWOPI = struct.unpack('<d', struct.pack('<Q', 0x401921fb54442d18))[0] |
| 14 | HALFPI = struct.unpack('<d', struct.pack('<Q', 0x3ff921fb54442d18))[0] |
| 15 | LN2 = struct.unpack('<d', struct.pack('<Q', 0x3fe62e42fefa39ef))[0] |
| 16 | INVLN2 = struct.unpack('<d', struct.pack('<Q', 0x3ff71547652b82fe))[0] |
| 17 | |
| 18 | |
| 19 | def _reduce(theta): |
| 20 | k = int(theta / TWOPI) # truncation toward zero, as `long div theta twopi` |
| 21 | x = theta - float(k) * TWOPI |
| 22 | n = 0 |
| 23 | while x > PI and n < 8: |
| 24 | x -= TWOPI; n += 1 |
| 25 | n = 0 |
| 26 | while x < -PI and n < 8: |
| 27 | x += TWOPI; n += 1 |
| 28 | if x > HALFPI: |
| 29 | x = PI - x |
| 30 | if x < -HALFPI: |
| 31 | x = -PI - x |
| 32 | return x |
| 33 | |
| 34 | |
| 35 | def sin_poly(theta, dens): |
| 36 | x = _reduce(theta) |
| 37 | x2 = x * x |
| 38 | s = 1.0 |
| 39 | for d in dens: |
| 40 | s = 1.0 - (x2 * s) / d |
| 41 | return x * s |
| 42 | |
| 43 | |
| 44 | OLD = [210.0, 156.0, 110.0, 72.0, 42.0, 20.0, 6.0] |
| 45 | NEW = [506.0, 420.0, 342.0, 272.0] + OLD |
| 46 | u_sin = lambda t: sin_poly(t, OLD) |
| 47 | u_cos = lambda t: sin_poly(t + HALFPI, OLD) |
| 48 | iv_sin = lambda t: sin_poly(t, NEW) |
| 49 | iv_cos = lambda t: sin_poly(t + HALFPI, NEW) |
| 50 | |
| 51 | |
| 52 | def u_exp(v): |
| 53 | kf = v * INVLN2 |
| 54 | ki = int(kf + 0.5) if kf >= 0.0 else int(kf - 0.5) |
| 55 | r = v - float(ki) * LN2 |
| 56 | p = 1.0 |
| 57 | for d in (12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0): |
| 58 | p = 1.0 + r * (p / d) |
| 59 | p = 1.0 + r * p |
| 60 | return p * math.ldexp(1.0, ki) |
| 61 | |
| 62 | |
| 63 | def u_log(x): |
| 64 | bits = struct.unpack('<Q', struct.pack('<d', x))[0] |
| 65 | e = ((bits >> 52) & 2047) - 1023 |
| 66 | m = struct.unpack('<d', struct.pack('<Q', (bits & 0x000fffffffffffff) | 0x3ff0000000000000))[0] |
| 67 | if m > 1.4142135623730951: |
| 68 | m = m * 0.5; e += 1 |
| 69 | t = (m - 1.0) / (m + 1.0) |
| 70 | t2 = t * t |
| 71 | s = 1.0 / 15.0 |
| 72 | for d in (13.0, 11.0, 9.0, 7.0, 5.0, 3.0): |
| 73 | s = 1.0 / d + t2 * s |
| 74 | s = 1.0 + t2 * s |
| 75 | return float(e) * LN2 + 2.0 * (t * s) |
| 76 | |
| 77 | |
| 78 | def scan(name, f, ref, lo, hi, n, rel=False, log_grid=False): |
| 79 | worst, at = mp.mpf(0), None |
| 80 | for i in range(n + 1): |
| 81 | x = lo + (hi - lo) * i / n |
| 82 | if log_grid: |
| 83 | x = 2.0 ** (math.log2(lo) + (math.log2(hi) - math.log2(lo)) * i / n) |
| 84 | r = ref(mp.mpf(x)) |
| 85 | err = abs(mp.mpf(f(x)) - r) |
| 86 | if rel: |
| 87 | if r != 0: |
| 88 | err /= abs(r) |
| 89 | elif err != 0: |
| 90 | err = mp.inf |
| 91 | if err > worst: |
| 92 | worst, at = err, x |
| 93 | kind = 'rel' if rel else 'abs' |
| 94 | print(f"{name:34s} [{lo}, {hi}] n={n} max_{kind}_err={mp.nstr(worst, 4)} at x={at!r}") |
| 95 | return float(worst) |
| 96 | |
| 97 | |
| 98 | N = 200000 |
| 99 | print("# elementary-function cross-check vs 50-digit mpmath") |
| 100 | a = scan("uSin64 (15th order, OLD)", u_sin, mp.sin, -8.0, 8.0, N) |
| 101 | b = scan("uCos64 (15th order, OLD)", u_cos, mp.cos, -8.0, 8.0, N) |
| 102 | c = scan("ivSin64 (23rd order, NEW)", iv_sin, mp.sin, -8.0, 8.0, N) |
| 103 | d = scan("ivCos64 (23rd order, NEW)", iv_cos, mp.cos, -8.0, 8.0, N) |
| 104 | e = scan("uExp64", u_exp, mp.exp, -100.0, 100.0, N, rel=True) |
| 105 | f = scan("uLog64", u_log, mp.log, 2.0**-60, 2.0**60, N, rel=True, log_grid=True) |
| 106 | g = scan("uLog64 near 1", u_log, mp.log, 0.5, 2.0, N, rel=True) |
| 107 | h = scan("uLog64 near 1 (abs)", u_log, mp.log, 0.5, 2.0, N) |
| 108 | print(f"old cos(0) = {u_cos(0.0)!r} (must be 1; old margin 1e-12*|x|+1e-15)") |
| 109 | print(f"new cos(0) = {iv_cos(0.0)!r}") |
| 110 | print("# sampled regression thresholds") |
| 111 | ok = True |
| 112 | for label, val, bound in (("ivSin64 abs <= 4e-15", c, 4e-15), ("ivCos64 abs <= 5e-15", d, 5e-15), |
| 113 | ("uExp64 rel <= 3e-14", e, 3e-14), ("uLog64 rel <= 1e-13", max(f, g), 1e-13)): |
| 114 | good = val <= bound |
| 115 | ok = ok and good |
| 116 | print(f"{label:26s} observed={val:.3e} {'OK' if good else 'VIOLATED'}") |
| 117 | print("old uSin64/uCos64 exceed the old margin 1e-12:", a > 1e-12 or b > 1e-12) |
| 118 | sys.exit(0 if ok else 1) |