~/bend-docscommunity

math.bend source

math.bend on the hub · documented module

# bend-float64's math functions, NumPy's float64 set, every result correctly rounded (to nearest, ties to even; the# _m versions in any of IEEE 754's five rounding modes, the balls deciding in that mode).# Each function is evaluated on balls (ball.bend: big-integer values with a rigorous error bound) at 128 bits, and# again at 256, 512, 1024, 2048 until every point of the ball rounds to the same float64 (Ziv's strategy). Inputs# whose answer is exact, or exactly halfway between two float64s, never settle that way, so they are found first and# computed exactly: cbrt and hypot always are (integer roots with a remainder), exp2 of an integer, log2 of a power of# two, log10 of 10^k, and every power x^y that is exact. Special values follow C99's Annex F; a NaN result has the# bits NumPy gives on x86-64 (a NaN operand passes through quieted). In a directed mode an answer a hair off a float64# never settles either (tiny arguments, saturation, far-apart legs), so those are found first too and nudged to the# next float64 when the mode says so. Pure.import Baseimport ./wide.bend as Wimport ./big.bend as Bimport ./float64.bend as Fimport ./ball.bend as Limport ./consts.bend as Kimport ./fb.bend as Qimport ./fbconsts.bend as Cimport ./fast.bend as FS# ---- helpers ----def fp(+h: U32, +l: U32) -> F.F64:  F.of_bits(h, l)def is_neg(+x: U32) -> Bool:  U32.is_le(2147483648, x)def neg_nan() -> F.F64:  F.dnan()def pos_nan() -> F.F64:  F.F64{2146959360, 0}def one() -> F.F64:  F.F64{1072693248, 0}def sign_word(+neg: Bool) -> U32:  Bool.pick(U32, neg, 2147483648, 0)# ---- an answer just off a float64: in a directed mode, the next float64 that way ----# whether the rounding of g + d (d nonzero and far below g's last place, positive when up) leaves gdef nudge_moves(rm: F.Rnd, +up: Bool, +away: Bool) -> Bool:  match rm:    case F.RUp{}:      up    case F.RDown{}:      Bool.not(up)    case F.RZero{}:      Bool.not(away)    case _:      False{}# the next float64 from g (finite, nonzero) away from zero, or toward itdef step(+g: F.F64, +away: Bool) -> F.F64:  +b: W.W64 = W.W64{F.hi_bits(g), F.lo_bits(g)}  +r = Bool.pick(W.W64, away, W.add(b, W.w(1)), W.sub(b, W.w(1)))  F.of_bits(W.high(r), W.low(r))# g + d rounded in mode rm, for g finite and nonzero and d nonzero, far below g's last place, positive when updef nudge(+rm: F.Rnd, +g: F.F64, +up: Bool) -> F.F64:  +away = Bool.xor(up, F.signbit(g))  Bool.pick(F.F64, nudge_moves(rm, up, away), step(g, away), g)# |x| < 2^-k, for a k from 1 to 1022 given as the high word of 2^-kdef tiny(+x: F.F64, +hk: U32) -> Bool:  F.lt(F.abs(x), F.F64{hk, 0})# the float64 nearest the ball's midpoint, ignoring its error (for choosing branches, never for answers)def approx(b: L.Ball) -> F.F64:  match b:    case L.Ball{n, m, e, r}:      L.round_big(n, m, e)# |b| < 2^k, judged on the midpointdef below(+b: L.Ball, +k: U32) -> Bool:  L.s_lt(L.top(b), k)def pi_b(+p: U32) -> L.Ball:  L.constant(K.pi(), K.pi_bits(), (p + 8 : U32))def ln2_b(+p: U32) -> L.Ball:  L.constant(K.ln2(), K.ln2_bits(), (p + 8 : U32))def ln10_b(+p: U32) -> L.Ball:  L.constant(K.ln10(), K.ln10_bits(), (p + 8 : U32))# b * k for a signed word kdef mul_i(+b: L.Ball, +k: U32) -> L.Ball:  Bool.pick(L.Ball, is_neg(k), L.negate(L.mul_u(b, (0 - k : U32))), L.mul_u(b, k))def signed(+neg: Bool, +b: L.Ball) -> L.Ball:  Bool.pick(L.Ball, neg, L.negate(b), b)# ---- series: add terms while they matter; the rest is at most twice the first one left out (every series here# shrinks by at least half a term) ----def small(+term: L.Ball, +sum: L.Ball, +p: U32) -> Bool:  L.s_lt(L.top(term), (L.top(sum) - p - 8 : U32))def series_pick(stop: Bool, +sum: L.Ball, +term: L.Ball, more: Unit -> L.Ball) -> L.Ball:  match stop:    case True{}:      L.widen(sum, L.mag_shift(L.mag_of(term), 1))    case False{}:      more(Unit{})# sum + term + term * x / (n + 1) + ...def exp_series(fuel: Nat, +sum: L.Ball, +term: L.Ball, +x: L.Ball, +n: U32, +p: U32) -> L.Ball:  match fuel:    case 0n:      L.widen(sum, L.mag_shift(L.mag_of(term), 1))    case 1n+f:      series_pick(small(term, sum, p), sum, term,        u => exp_series(f, L.add(sum, term, p), L.div_u(L.mul(term, x, p), (n + 1 : U32), p), x, (n + 1 : U32), p))# sum + pw * z2 / (2n + 1) + pw * z2^2 / (2n + 3) + ..., alternating when altdef odd_series(fuel: Nat, +sum: L.Ball, +pw: L.Ball, +z2: L.Ball, +n: U32, +alt: Bool, +p: U32) -> L.Ball:  match fuel:    case 0n:      sum    case 1n+f:      +pw2 = Bool.pick(L.Ball, alt, L.negate(L.mul(pw, z2, p)), L.mul(pw, z2, p))      +term = L.div_u(pw2, (n + n + 1 : U32), p)      series_pick(small(term, sum, p), sum, term, u => odd_series(f, L.add(sum, term, p), pw2, z2, (n + 1 : U32), alt, p))# atanh(z) = z + z^3/3 + z^5/5 + ... (|z| < 0.7)def atanh_series(+z: L.Ball, +p: U32) -> L.Ball:  odd_series(4000n, z, z, L.mul(z, z, p), 1, False{}, p)# atan(z) = z - z^3/3 + z^5/5 - ... (|z| < 0.7)def atan_series(+z: L.Ball, +p: U32) -> L.Ball:  odd_series(4000n, z, z, L.mul(z, z, p), 1, True{}, p)# sum + term * x2 / ((n + 1)(n + 2)) + ...: sin, cos, sinh, cosh by their Taylor series (x2 = +-x^2)def trig_series(fuel: Nat, +sum: L.Ball, +term: L.Ball, +x2: L.Ball, +n: U32, +p: U32) -> L.Ball:  match fuel:    case 0n:      sum    case 1n+f:      +t = L.div_u(L.div_u(L.mul(term, x2, p), (n + 1 : U32), p), (n + 2 : U32), p)      series_pick(small(t, sum, p), sum, t, u => trig_series(f, L.add(sum, t, p), t, x2, (n + 2 : U32), p))# ---- exp and log on balls ----def square_n(n: Nat, +b: L.Ball, +p: U32) -> L.Ball:  match n:    case 0n:      b    case 1n+k:      square_n(k, L.mul(b, b, p), p)# e^a: a = k ln 2 + r, e^r = (e^(r / 256))^256 by its seriesdef exp_go(+a: L.Ball, +p: U32) -> L.Ball:  +k = F.to_i32(F.rint(F.mul(approx(a), fp(1073157447, 1697350398))))  +q = (p + 24 : U32)  +r8 = L.mul_pow2(L.sub(a, mul_i(ln2_b((q + 12 : U32)), k), q), (0 - 8 : U32))  L.mul_pow2(square_n(8n, exp_series(4000n, L.one(), r8, r8, 1, q), q), k)def exp_huge(huge: Bool, +a: L.Ball, +p: U32) -> L.Ball:  match huge:    case True{}:      L.exact(False{}, [1], Bool.pick(U32, L.neg_of(a), (0 - 3000 : U32), 3000))    case False{}:      exp_go(a, p)# e^a; past |a| = 1500 every point overflows or underflows, so a power of two far out stands for them alldef exp_core(+a: L.Ball, +p: U32) -> L.Ball:  exp_huge(F.lt(fp(1083666432, 0), F.abs(approx(a))), a, p)def expm1_go(near: Bool, +a: L.Ball, +p: U32) -> L.Ball:  match near:    case True{}:      exp_series(4000n, a, L.div_u(L.mul(a, a, (p + 16 : U32)), 2, (p + 16 : U32)), a, 2, (p + 16 : U32))    case False{}:      L.sub(exp_core(a, p), L.one(), (p + 16 : U32))# e^a - 1 (exact near 0)def expm1_core(+a: L.Ball, +p: U32) -> L.Ball:  expm1_go(below(a, (0 - 1 : U32)), a, p)def log_mk(+m: L.Ball, +k: U32, +p: U32) -> L.Ball:  +q = (p + 24 : U32)  L.add(L.mul_pow2(atanh_series(L.div(L.sub(m, L.one(), q), L.add(m, L.one(), q), q), q), 1), mul_i(ln2_b((q + 12 : U32)), k), q)# log(a), a > 0: a = 2^k m, m in [1/sqrt 2, sqrt 2], log m = 2 atanh((m - 1) / (m + 1))def log_core(+a: L.Ball, +p: U32) -> L.Ball:  +k0 = (L.top(a) - 1 : U32)  +m0 = L.mul_pow2(a, (0 - k0 : U32))  +big = F.lt(fp(1073127582, 1719614413), approx(m0))  log_mk(Bool.pick(L.Ball, big, L.mul_pow2(m0, (0 - 1 : U32)), m0), (k0 + Bool.to_u32(big) : U32), p)# log(1 + u), exact near 0def log1p_go(near: Bool, +u: L.Ball, +p: U32) -> L.Ball:  match near:    case True{}:      L.mul_pow2(atanh_series(L.div(u, L.add(L.of_u32(2), u, (p + 24 : U32)), (p + 24 : U32)), (p + 24 : U32)), 1)    case False{}:      log_core(L.add(L.one(), u, (p + 24 : U32)), p)def log1p_core(+u: L.Ball, +p: U32) -> L.Ball:  log1p_go(below(u, (0 - 1 : U32)), u, p)# ---- trigonometry on balls ----# atan(t), |t| <= 1: halve the angle twice (t / (1 + sqrt(1 + t^2))), then the seriesdef atan_small(+t: L.Ball, +p: U32) -> L.Ball:  +q = (p + 24 : U32)  +t1 = L.div(t, L.add(L.one(), L.sqrt(L.add(L.one(), L.mul(t, t, q), q), q), q), q)  +t2 = L.div(t1, L.add(L.one(), L.sqrt(L.add(L.one(), L.mul(t1, t1, q), q), q), q), q)  L.mul_pow2(atan_series(t2, q), 2)# atan(t), any t: past 1, pi/2 - atan(1/t)def atan_go(small: Bool, +t: L.Ball, +p: U32) -> L.Ball:  match small:    case True{}:      atan_small(t, p)    case False{}:      signed(L.neg_of(t), L.sub(L.mul_pow2(pi_b((p + 24 : U32)), (0 - 1 : U32)), atan_small(L.div(L.one(), L.abs(t), (p + 24 : U32)), p), (p + 24 : U32)))def atan_any(+t: L.Ball, +p: U32) -> L.Ball:  atan_go(below(t, 1), t, p)# x = (k + f) pi / 2 with |f| <= 1/2: k mod 4, and r = f pi / 2, from a window of 2 / pi's bits (x > 0)type Red is Data:  Red{k: U32, r: L.Ball}def red_frac(+i: List<&2, U32>, +t: List<&2, U32>, +fb: U32, +xm: List<&2, U32>, +ee: U32, +p: U32) -> Red:  +f = B.sub(t, B.shl(i, fb))  +up = B.le(B.shl([1], (fb - 1 : U32)), f)  +k = (U32.and(B.head0(i), 3) + Bool.to_u32(up) : U32)  +fr = L.trunc(L.Ball{up, Bool.pick(List<&2, U32>, up, B.sub(B.shl([1], fb), f), f), (0 - fb : U32), L.mag_big(xm, ee)}, (p + 32 : U32))  Red{U32.and(k, 3), L.mul(fr, L.mul_pow2(pi_b((p + 32 : U32)), (0 - 1 : U32)), (p + 24 : U32))}# words j down to j - n + 1 of floor(2/pi 2^3600) (C.tpi_word), as a big integer (word j - n + 1 the lowest)def tpi_win(n: Nat, +j: U32, +acc: List<&2, U32>) -> List<&2, U32>:  match n:    case 0n:      acc    case 1n+k:      tpi_win(k, (j - 1 : U32), C.tpi_word(j) <> acc)# x = xm 2^ex: x 2/pi mod 4 as a quadrant and a fraction, from a window of 2/pi's bits (Payne and Hanek). The window's# top word holds 2/pi's bit 3601 - ex of floor(2/pi 2^3600) or a higher one, so every bit left out above is worth a# multiple of 4 (whole turns); it runs l words down, about p + 232 bits, ending b = 32 (jt - l) bits above the# constant's last, so the bits left out below are worth under xm 2^(ex - 3600 + b), the ball's error.def reduce(+xm: List<&2, U32>, +ex: U32, +p: U32) -> Red:  +tb = (4676 - ex - 1075 : U32)  +jt = Bool.pick(U32, U32.is_lt(3999, tb), 113, U32.shrn(tb, 5n))  +l0 = (U32.shrn((p + 232 : U32), 5n) + 1 : U32)  +l = Bool.pick(U32, U32.is_lt(jt, l0), jt, l0)  +b = (U32.shln(jt, 5n) - U32.shln(l, 5n) : U32)  +t = B.mul(xm, B.trim(tpi_win(U32.to_nat((l + 1 : U32)), jt, [])))  +fb = (3600 - b - ex : U32)  red_frac(B.shr(t, fb), t, fb, xm, (ex - 3600 + b : U32), p)# the quadrant of a reductiondef red_k(r: Red) -> U32:  match r:    case Red{k, b}:      kdef sin_r(+r: L.Ball, +p: U32) -> L.Ball:  trig_series(4000n, r, r, L.negate(L.mul(r, r, p)), 1, p)def cos_r(+r: L.Ball, +p: U32) -> L.Ball:  trig_series(4000n, L.one(), L.one(), L.negate(L.mul(r, r, p)), 0, p)# ---- the functions on balls, by name ----type Fn is Data:  ZExp{x: F.F64}  ZExp2{k: U32, f: F.F64}  ZExpm1{x: F.F64}  ZLog{x: F.F64}  ZLog2{x: F.F64}  ZLog10{x: F.F64}  ZLog1p{x: F.F64}  ZSin{x: F.F64}  ZCos{x: F.F64}  ZTan{x: F.F64}  ZAsin{x: F.F64}  ZAcos{x: F.F64}  ZAtan{x: F.F64}  ZAtan2{y: F.F64, x: F.F64}  ZSinh{x: F.F64}  ZCosh{x: F.F64}  ZTanh{x: F.F64}  ZAsinh{x: F.F64}  ZAcosh{x: F.F64}  ZAtanh{x: F.F64}  ZPow{neg: Bool, x: F.F64, y: F.F64}  ZHalfPi{neg: Bool}  ZPi{neg: Bool}  ZQuarterPi{neg: Bool, three: Bool}def ball(+x: F.F64) -> L.Ball:  L.of_f64(x)# which of sin r, cos r, sin r / cos r, cos r / sin r a quadrant needs, and its signtype TQ is Data:  TSin{}  TCos{}  TTan{}  TCot{}def tq_ball(t: TQ, +r: L.Ball, +q: U32) -> L.Ball:  match t:    case TSin{}:      sin_r(r, q)    case TCos{}:      cos_r(r, q)    case TTan{}:      L.div(sin_r(r, q), cos_r(r, q), q)    case TCot{}:      L.div(cos_r(r, q), sin_r(r, q), q)# which: 0 sin, 1 cos, 2 tan; quadrant k: sin = (s, c, -s, -c), cos = (c, -s, -c, s), tan = (s/c, -c/s)def trig_q(+which: U32, red: Red, +neg: Bool, +p: U32) -> L.Ball:  match red:    case Red{+k, r}:      +odd = U32.is_eq(U32.and(k, 1), 1)      +t = Bool.pick(TQ, U32.is_eq(which, 2), Bool.pick(TQ, odd, TCot{}, TTan{}), Bool.pick(TQ, Bool.xor(odd, U32.is_eq(which, 1)), TCos{}, TSin{}))      +minus = Bool.pick(Bool, U32.is_eq(which, 2), odd, Bool.pick(Bool, U32.is_eq(which, 0), U32.is_le(2, k), Bool.or(U32.is_eq(k, 1), U32.is_eq(k, 2))))      signed(Bool.xor(neg, minus), tq_ball(t, r, (p + 24 : U32)))# which: 0 sin, 1 cos, 2 tan; x finite and nonzerodef trig_cl(+which: U32, c: F.Cls, +p: U32) -> L.Ball:  match c:    case F.FFin{s, +e, m}:      trig_q(which, reduce(B.of_w64(m), (e - 1075 : U32), p), Bool.and(U32.is_ne(s, 0), U32.is_ne(which, 1)), p)    case _:      L.bad()def trig(+which: U32, x: F.F64, +p: U32) -> L.Ball:  trig_cl(which, F.cls(x), p)# sinh b, b >= 0: the series below 1, (e^b - e^-b) / 2 abovedef sinh_go(near: Bool, +b: L.Ball, +q: U32) -> L.Ball:  match near:    case True{}:      trig_series(4000n, b, b, L.mul(b, b, q), 1, q)    case False{}:      +ex = exp_core(b, q)      L.mul_pow2(L.sub(ex, L.div(L.one(), ex, q), q), (0 - 1 : U32))# asinh b, b >= 0: log1p(b + b^2 / (1 + sqrt(1 + b^2))) below 1, log(b + sqrt(b^2 + 1)) abovedef asinh_go(near: Bool, +b: L.Ball, +p: U32) -> L.Ball:  match near:    case True{}:      +q = (p + 24 : U32)      +b2 = L.mul(b, b, q)      log1p_core(L.add(b, L.div(b2, L.add(L.one(), L.sqrt(L.add(L.one(), b2, q), q), q), q), q), p)    case False{}:      +q = (p + 24 : U32)      log_core(L.add(b, L.sqrt(L.add(L.mul(b, b, q), L.one(), q), q), q), p)# atanh b, 0 <= b < 1: the series below 1/2, log1p(2b / (1 - b)) / 2 abovedef atanh_go(near: Bool, +b: L.Ball, +q: U32) -> L.Ball:  match near:    case True{}:      atanh_series(b, q)    case False{}:      L.mul_pow2(log1p_core(L.div(L.mul_pow2(b, 1), L.sub(L.one(), b, q), q), q), (0 - 1 : U32))def eval(f: Fn, +p: U32) -> L.Ball:  match f:    case ZExp{x}:      exp_core(ball(x), p)    case ZExp2{k, x}:      L.mul_pow2(exp_core(L.mul(ball(x), ln2_b((p + 32 : U32)), (p + 32 : U32)), p), k)    case ZExpm1{x}:      expm1_core(ball(x), p)    case ZLog{x}:      log_core(ball(x), p)    case ZLog2{x}:      L.div(log_core(ball(x), (p + 16 : U32)), ln2_b((p + 32 : U32)), (p + 16 : U32))    case ZLog10{x}:      L.div(log_core(ball(x), (p + 16 : U32)), ln10_b((p + 32 : U32)), (p + 16 : U32))    case ZLog1p{x}:      log1p_core(ball(x), p)    case ZSin{x}:      trig(0, x, p)    case ZCos{x}:      trig(1, x, p)    case ZTan{x}:      trig(2, x, p)    case ZAsin{+x}:      +q = (p + 24 : U32)      +b = ball(x)      atan_any(L.div(b, L.sqrt(L.sub(L.one(), L.mul(b, b, q), q), q), q), p)    case ZAcos{+x}:      +q = (p + 24 : U32)      +b = ball(x)      +a = atan_any(L.div(L.sqrt(L.sub(L.one(), L.mul(b, b, q), q), q), L.abs(b), q), p)      Bool.pick(L.Ball, L.neg_of(b), L.sub(pi_b(q), a, q), a)    case ZAtan{x}:      atan_any(ball(x), p)    case ZAtan2{+y, +x}:      +q = (p + 24 : U32)      +a = atan_any(L.div(L.abs(ball(y)), L.abs(ball(x)), q), p)      signed(F.signbit(y), Bool.pick(L.Ball, F.signbit(x), L.sub(pi_b(q), a, q), a))    case ZSinh{+x}:      signed(F.signbit(x), sinh_go(below(L.abs(ball(x)), 0), L.abs(ball(x)), (p + 24 : U32)))    case ZCosh{x}:      +q = (p + 24 : U32)      +ex = exp_core(L.abs(ball(x)), q)      L.mul_pow2(L.add(ex, L.div(L.one(), ex, q), q), (0 - 1 : U32))    case ZTanh{+x}:      +q = (p + 24 : U32)      +b2 = L.mul_pow2(L.abs(ball(x)), 1)      +u = expm1_core(b2, q)      signed(F.signbit(x), L.div(u, L.add(u, L.of_u32(2), q), q))    case ZAsinh{+x}:      signed(F.signbit(x), asinh_go(below(L.abs(ball(x)), 0), L.abs(ball(x)), p))    case ZAcosh{x}:      +q = (p + 24 : U32)      +t = L.sub(ball(x), L.one(), q)      log1p_core(L.add(t, L.sqrt(L.add(L.mul_pow2(t, 1), L.mul(t, t, q), q), q), q), p)    case ZAtanh{+x}:      signed(F.signbit(x), atanh_go(below(L.abs(ball(x)), (0 - 1 : U32)), L.abs(ball(x)), (p + 24 : U32)))    case ZPow{neg, x, y}:      +q = (p + 40 : U32)      signed(neg, exp_core(L.mul(log_core(ball(x), q), ball(y), q), p))    case ZHalfPi{neg}:      signed(neg, L.mul_pow2(pi_b(p), (0 - 1 : U32)))    case ZPi{neg}:      signed(neg, pi_b(p))    case ZQuarterPi{neg, three}:      signed(neg, Bool.pick(L.Ball, three, L.mul_u(L.mul_pow2(pi_b(p), (0 - 2 : U32)), 3), L.mul_pow2(pi_b(p), (0 - 2 : U32))))def ziv_pick(m: Maybe<&2, F.F64>, more: Unit -> F.F64) -> F.F64:  match m:    case Some{x}:      x    case None{}:      more(Unit{})# evaluate at p bits, doubling p until the ball decides in mode rm (at most n times); a ball that never decides holds# an exact float64 (no other value of these functions comes that close to one), which the nearest rounding findsdef ziv(n: Nat, +rm: F.Rnd, +f: Fn, +p: U32) -> F.F64:  match n:    case 0n:      approx(eval(f, (p / 2 : U32)))    case 1n+k:      ziv_pick(L.decide_m(rm, eval(f, p)), u => ziv(k, rm, f, (p + p : U32)))# ---- the fast path: the same evaluations on 128-bit fast balls (fast.bend) ----def qb(+x: F.F64) -> Q.FB:  Q.of_f64(x)def qsigned(+neg: Bool, +a: Q.FB) -> Q.FB:  Bool.pick(Q.FB, neg, Q.neg(a), a)def q_sinh(small: Bool, +b: Q.FB) -> Q.FB:  match small:    case True{}:      Q.mul(b, C.sinh_inner(Q.sqr(b)))    case False{}:      +ex = FS.exp(b)      Q.mul_pow2(Q.sub(ex, Q.div(Q.one(), ex)), (0 - 1 : U32))def q_asinh(small: Bool, +b: Q.FB) -> Q.FB:  match small:    case True{}:      +b2 = Q.sqr(b)      FS.log1p(Q.add(b, Q.div(b2, Q.add(Q.one(), Q.sqrt(Q.add(Q.one(), b2))))))    case False{}:      FS.log(Q.add(b, Q.sqrt(Q.add(Q.sqr(b), Q.one()))))def q_acos(+neg: Bool, +a: Q.FB) -> Q.FB:  Bool.pick(Q.FB, neg, Q.sub(C.pi(), a), a)def fast_eval(f: Fn) -> Q.FB:  match f:    case ZExp{x}:      FS.exp(qb(x))    case ZExp2{k, x}:      Q.mul_pow2(FS.exp(Q.mul(qb(x), C.ln2())), k)    case ZExpm1{x}:      FS.expm1(qb(x))    case ZLog{x}:      FS.log(qb(x))    case ZLog2{x}:      Q.mul(FS.log(qb(x)), C.inv_ln2())    case ZLog10{x}:      Q.mul(FS.log(qb(x)), C.inv_ln10())    case ZLog1p{x}:      FS.log1p(qb(x))    case ZSin{x}:      FS.trig(0, x)    case ZCos{x}:      FS.trig(1, x)    case ZTan{x}:      FS.trig(2, x)    case ZAsin{+x}:      +b = qb(x)      FS.atan(Q.div(b, Q.sqrt(Q.sub(Q.one(), Q.sqr(b)))))    case ZAcos{+x}:      +b = qb(x)      q_acos(F.signbit(x), FS.atan(Q.div(Q.sqrt(Q.sub(Q.one(), Q.sqr(b))), Q.abs(b))))    case ZAtan{x}:      FS.atan(qb(x))    case ZAtan2{+y, +x}:      qsigned(F.signbit(y), q_acos(F.signbit(x), FS.atan(Q.div(Q.abs(qb(y)), Q.abs(qb(x))))))    case ZSinh{+x}:      +b = Q.abs(qb(x))      qsigned(F.signbit(x), q_sinh(FS.below(b, (0 - 7 : U32)), b))    case ZCosh{x}:      +ex = FS.exp(Q.abs(qb(x)))      Q.mul_pow2(Q.add(ex, Q.div(Q.one(), ex)), (0 - 1 : U32))    case ZTanh{+x}:      +u = FS.expm1(Q.mul_pow2(Q.abs(qb(x)), 1))      qsigned(F.signbit(x), Q.div(u, Q.add(u, Q.of_u32(2))))    case ZAsinh{+x}:      +b = Q.abs(qb(x))      qsigned(F.signbit(x), q_asinh(FS.below(b, 0), b))    case ZAcosh{x}:      +t = Q.sub(qb(x), Q.one())      FS.log1p(Q.add(t, Q.sqrt(Q.add(Q.mul_pow2(t, 1), Q.sqr(t)))))    case ZAtanh{+x}:      +b = Q.abs(qb(x))      qsigned(F.signbit(x), Q.mul_pow2(FS.log1p(Q.div(Q.mul_pow2(b, 1), Q.sub(Q.one(), b))), (0 - 1 : U32)))    case ZPow{neg, x, y}:      qsigned(neg, FS.exp(Q.mul(FS.log(qb(x)), qb(y))))    case ZHalfPi{neg}:      qsigned(neg, C.pi_2())    case ZPi{neg}:      qsigned(neg, C.pi())    case ZQuarterPi{neg, three}:      qsigned(neg, Bool.pick(Q.FB, three, C.pi_3_4(), C.pi_4()))# the fast path's answer if its ball decides in mode rm, else the arbitrary-precision balls from 128 bits updef run(+rm: F.Rnd, +f: Fn) -> F.F64:  ziv_pick(Q.decide_m(rm, fast_eval(f)), u => ziv(5n, rm, f, 128))# ---- exact answers ----def round_top(+rm: F.Rnd, +s: U32, t: B.Top, +sticky: Bool, +e: U32) -> F.F64:  match t:    case B.Top{sig, k}:      F.round_pack_m(rm, s, (1084 + k + e : U32), W.jam_or(sig, sticky))# q * 2^e (plus a little, when sticky) rounded in mode rm, q a nonzero big integerdef round_q(+rm: F.Rnd, +s: U32, +q: List<&2, U32>, +sticky: Bool, +e: U32) -> F.F64:  round_top(rm, s, B.top64(q), sticky, e)def qr_q(d: B.QR) -> List<&2, U32>:  match d:    case B.QR{q, r}:      qdef qr_r(d: B.QR) -> List<&2, U32>:  match d:    case B.QR{q, r}:      rdef div3(+v: U32) -> U32:  Bool.pick(U32, is_neg(v), (0 - U32.div((0 - v : U32), 3) : U32), U32.div(v, 3))def cbrt_go(+rm: F.Rnd, +s: U32, +m: List<&2, U32>, +ex: U32) -> F.F64:  +s0 = (200 - B.bitlen(m) : U32)  +sh = (s0 + U32.mod((ex - s0 + 3000 : U32), 3) : U32)  +n = B.shl(m, sh)  +q = B.icbrt(n)  round_q(rm, s, q, Bool.not(B.is_zero(B.sub(n, B.cube(q)))), div3((ex - sh : U32)))def cbrt_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> F.F64:  match c:    case F.FFin{s, +e, m}:      cbrt_go(rm, s, B.of_w64(m), (e - 1075 : U32))    case F.FNan{}:      F.quiet(x)    case _:      xdef cbrt_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  cbrt_c(F.cls(x), rm, x)def hyp_go(+rm: F.Rnd, +mx: List<&2, U32>, +ex: U32, +my: List<&2, U32>, +ey: U32) -> F.F64:  +e = L.s_min(ex, ey)  +a = B.shl(mx, (ex - e : U32))  +b = B.shl(my, (ey - e : U32))  +ss = B.add(B.mul(a, a), B.mul(b, b))  +s0 = (132 - B.bitlen(ss) : U32)  +s1 = Bool.pick(U32, is_neg(s0), 0, s0)  +sh = (s1 + U32.and(s1, 1) : U32)  +qr = B.isqrt2(B.shl(ss, sh))  round_q(rm, 0, qr_q(qr), Bool.not(B.is_zero(qr_r(qr))), (e - U32.div(sh, 2) : U32))# far apart (exponents more than 64 apart): just above the largerdef hyp_far(far: Bool, +rm: F.Rnd, +x: F.F64, +y: F.F64, +mx: List<&2, U32>, +ex: U32, +my: List<&2, U32>, +ey: U32) -> F.F64:  match far:    case True{}:      nudge(rm, F.maximum(F.abs(x), F.abs(y)), True{})    case False{}:      hyp_go(rm, mx, ex, my, ey)def hyp_c(cx: F.Cls, cy: F.Cls, +rm: F.Rnd, +x: F.F64, +y: F.F64) -> F.F64:  match cx cy:    case F.FFin{sx, +ex, mx} F.FFin{sy, +ey, my}:      +a = (ex - 1075 : U32)      +b = (ey - 1075 : U32)      hyp_far(Bool.or(L.s_lt((a + 64 : U32), b), L.s_lt((b + 64 : U32), a)), rm, x, y, B.of_w64(mx), a, B.of_w64(my), b)    case F.FZero{sx} _:      F.abs(y)    case _ F.FZero{sy}:      F.abs(x)    case _ _:      F.dnan()def is_snan(+x: F.F64) -> Bool:  Bool.and(F.is_nan(x), U32.is_eq(U32.and(F.hi_bits(x), 524288), 0))# sqrt(x^2 + y^2) without overflow, rounded in mode rm (exact: the integer square root of the exact sum of squares). An# infinity wins over a quiet NaN, as C99 says; a signalling NaN does not, as glibc does it.def hypot_m(+rm: F.Rnd, +x: F.F64, +y: F.F64) -> F.F64:  Bool.pick(F.F64, Bool.or(is_snan(x), is_snan(y)), Bool.pick(F.F64, F.is_nan(x), F.quiet(x), F.quiet(y)),    Bool.pick(F.F64, Bool.or(F.is_inf(x), F.is_inf(y)), F.inf(0),      Bool.pick(F.F64, Bool.or(F.is_nan(x), F.is_nan(y)), Bool.pick(F.F64, F.is_nan(x), F.quiet(x), F.quiet(y)),        hyp_c(F.cls(x), F.cls(y), rm, x, y))))# sqrt(x^2 + y^2), to nearestdef hypot(+x: F.F64, +y: F.F64) -> F.F64:  hypot_m(F.RNearEven{}, x, y)# ---- the functions: special values first, each giving a value (Val) or a ball evaluation to run (Go) ----type Out is Data:  Val{x: F.F64}  Go{f: Fn}def out(o: Out, +rm: F.Rnd) -> F.F64:  match o:    case Val{x}:      x    case Go{f}:      run(rm, f)def is_int(+x: F.F64) -> Bool:  Bool.and(F.is_finite(x), F.eq(F.trunc(x), x))# an odd integer (every float64 from 2^53 up is even)def is_odd(+y: F.F64) -> Bool:  Bool.and(is_int(y), F.ne(F.mul(F.trunc(F.mul(y, fp(1071644672, 0))), fp(1073741824, 0)), y))def nanv(+x: F.F64) -> Out:  Val{F.quiet(x)}# |x| < 2^-60: 1 plus a hair of x's signdef exp_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FInf{s}:      Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, F.zero(0))}    case F.FZero{s}:      Val{one()}    case F.FFin{s, e, m}:      Bool.pick(Out, F.lt(fp(1082535936, 0), x), Val{F.overflow(rm, 0)}, Bool.pick(Out, F.lt(x, fp(3230093312, 0)), Val{F.underflow(rm, 0)},        Bool.pick(Out, tiny(x, 1009778688), Val{nudge(rm, one(), Bool.not(F.signbit(x)))}, Go{ZExp{x}})))    case F.FNan{}:      nanv(x)def exp_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(exp_c(F.cls(x), rm, x), rm)def exp2_fin(+rm: F.Rnd, +x: F.F64) -> Out:  +k = F.rint(x)  Bool.pick(Out, F.lt(fp(1083180032, 0), x), Val{F.overflow(rm, 0)}, Bool.pick(Out, F.lt(x, fp(3230715904, 0)), Val{F.underflow(rm, 0)},    Bool.pick(Out, F.eq(k, x), Val{F.round_pack_m(rm, 0, (1084 + F.to_i32(k) : U32), W.w(1))},      Bool.pick(Out, tiny(x, 1009778688), Val{nudge(rm, one(), Bool.not(F.signbit(x)))}, Go{ZExp2{F.to_i32(k), F.sub(x, k)}}))))def exp2_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FInf{s}:      Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, F.zero(0))}    case F.FZero{s}:      Val{one()}    case F.FFin{s, e, m}:      exp2_fin(rm, x)    case F.FNan{}:      nanv(x)def exp2_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(exp2_c(F.cls(x), rm, x), rm)# below -40: -1 plus a hair; |x| < 2^-60: x plus a hair (x^2 / 2)def expm1_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FInf{s}:      Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, F.F64{3220176896, 0})}    case F.FZero{s}:      Val{x}    case F.FFin{s, e, m}:      Bool.pick(Out, F.lt(fp(1082535936, 0), x), Val{F.overflow(rm, 0)}, Bool.pick(Out, F.lt(x, fp(3225681920, 0)), Val{nudge(rm, F.F64{3220176896, 0}, True{})},        Bool.pick(Out, tiny(x, 1009778688), Val{nudge(rm, x, True{})}, Go{ZExpm1{x}})))    case F.FNan{}:      nanv(x)def expm1_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(expm1_c(F.cls(x), rm, x), rm)# k when x = 2^kdef pow2_c(c: F.Cls) -> Maybe<&2, U32>:  match c:    case F.FFin{s, +e, +m}:      Bool.pick(Maybe<&2, U32>, W.is_zero(W.and(m, W.sub(m, W.w(1)))), Some{(e - 1075 + 63 - W.clz(m) : U32)}, None{})    case _:      None{}def pow10_f(+k: U32) -> F.F64:  round_q(F.RNearEven{}, 0, B.pow10(k), False{}, 0)# k when x = 10^k exactly, 0 <= k <= 22def pow10_k(n: Nat, +x: F.F64, +k: U32) -> Maybe<&2, U32>:  match n:    case 0n:      None{}    case 1n+j:      Bool.pick(Maybe<&2, U32>, F.eq(x, pow10_f(k)), Some{k}, pow10_k(j, x, (k + 1 : U32)))def log_exact(m: Maybe<&2, U32>, f: Fn) -> Out:  match m:    case Some{k}:      Val{F.from_i32(k)}    case None{}:      Go{f}def log10_of(int: Bool, +x: F.F64) -> Out:  match int:    case True{}:      log_exact(pow10_k(23n, x, 0), ZLog10{x})    case False{}:      Go{ZLog10{x}}type LK is Data:  KLog{}  KLog2{}  KLog10{}# x finite, positive, not 1def log_fin(k: LK, +x: F.F64) -> Out:  match k:    case KLog{}:      Go{ZLog{x}}    case KLog2{}:      log_exact(pow2_c(F.cls(x)), ZLog2{x})    case KLog10{}:      log10_of(Bool.and(is_int(x), F.lt(x, F.F64{1152724226, 3353430774})), x)def invalid_of(k: LK) -> F.F64:  match k:    case KLog10{}:      pos_nan()    case _:      neg_nan()def log_c(c: F.Cls, +k: LK, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, invalid_of(k))}    case F.FZero{s}:      Val{F.inf(2147483648)}    case F.FFin{s, e, m}:      Bool.pick(Out, U32.is_ne(s, 0), Val{invalid_of(k)}, Bool.pick(Out, F.eq(x, one()), Val{F.zero(0)}, log_fin(k, x)))def log_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(log_c(F.cls(x), KLog{}, x), rm)def log2_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(log_c(F.cls(x), KLog2{}, x), rm)def log10_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(log_c(F.cls(x), KLog10{}, x), rm)# |x| < 2^-60: x minus a hair (x^2 / 2)def log1p_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, neg_nan())}    case F.FZero{s}:      Val{x}    case F.FFin{s, e, m}:      Bool.pick(Out, F.eq(x, F.F64{3220176896, 0}), Val{F.inf(2147483648)}, Bool.pick(Out, F.lt(x, F.F64{3220176896, 0}), Val{neg_nan()},        Bool.pick(Out, tiny(x, 1009778688), Val{nudge(rm, x, False{})}, Go{ZLog1p{x}})))def log1p_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(log1p_c(F.cls(x), rm, x), rm)# the answer for |x| < 2^-40: sin is x a hair toward zero, cos 1 a hair below, tan x a hair away from zerodef trig_tiny(+rm: F.Rnd, +which: U32, +x: F.F64) -> F.F64:  Bool.pick(F.F64, U32.is_eq(which, 1), nudge(rm, one(), False{}), nudge(rm, x, Bool.xor(U32.is_eq(which, 2), F.signbit(x))))def trig_c(c: F.Cls, +rm: F.Rnd, +which: U32, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{neg_nan()}    case F.FZero{s}:      Val{Bool.pick(F.F64, U32.is_eq(which, 1), one(), x)}    case F.FFin{s, e, m}:      Bool.pick(Out, tiny(x, 1030750208), Val{trig_tiny(rm, which, x)},        Go{Bool.pick(Fn, U32.is_eq(which, 0), ZSin{x}, Bool.pick(Fn, U32.is_eq(which, 1), ZCos{x}, ZTan{x}))})def sin_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(trig_c(F.cls(x), rm, 0, x), rm)def cos_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(trig_c(F.cls(x), rm, 1, x), rm)def tan_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(trig_c(F.cls(x), rm, 2, x), rm)def above_one(+x: F.F64) -> Bool:  F.lt(one(), F.abs(x))# |x| < 2^-40: x a hair away from zerodef asin_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{pos_nan()}    case F.FZero{s}:      Val{x}    case F.FFin{s, e, m}:      Bool.pick(Out, above_one(x), Val{pos_nan()}, Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, x, Bool.not(F.signbit(x)))},        Go{Bool.pick(Fn, F.eq(F.abs(x), one()), ZHalfPi{F.signbit(x)}, ZAsin{x})}))def arcsin_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(asin_c(F.cls(x), rm, x), rm)def acos_c(c: F.Cls, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{pos_nan()}    case F.FZero{s}:      Go{ZHalfPi{False{}}}    case F.FFin{s, e, m}:      Bool.pick(Out, above_one(x), Val{pos_nan()}, Bool.pick(Out, F.eq(x, one()), Val{F.zero(0)},        Go{Bool.pick(Fn, F.eq(x, F.F64{3220176896, 0}), ZPi{False{}}, ZAcos{x})}))def arccos_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(acos_c(F.cls(x), x), rm)# |x| < 2^-40: x a hair toward zerodef atan_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Go{ZHalfPi{U32.is_ne(s, 0)}}    case F.FZero{s}:      Val{x}    case F.FFin{s, e, m}:      Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, x, F.signbit(x))}, Go{ZAtan{x}})def arctan_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(atan_c(F.cls(x), rm, x), rm)# q x = y exactly: q x - y is a multiple of about |y| 2^-105 when nonzero, so it cannot round to zero once y is# scaled (both scaled by 2^600 for |y| < 2^-960; an x too big to scale has y / x below every float64, so q = 0)def exact_ratio(+q: F.F64, +y: F.F64, +x: F.F64) -> Bool:  +sc = Bool.pick(F.F64, tiny(y, 66060288), F.F64{1701838848, 0}, one())  F.eq(F.fma(q, F.mul(x, sc), F.neg(F.mul(y, sc))), F.zero(0))# y / x exactly a float64 q, nonzero, below 2^-40, x > 0: atan(q) is q a hair toward zerodef atan2_q(+rm: F.Rnd, +q: F.F64, +y: F.F64, +x: F.F64) -> Out:  Bool.pick(Out, Bool.and(Bool.and(Bool.not(F.signbit(x)), tiny(q, 1030750208)), Bool.and(Bool.not(F.eq(q, F.zero(0))), exact_ratio(q, y, x))),    Val{nudge(rm, q, F.signbit(q))}, Go{ZAtan2{y, x}})def atan2_c(cy: F.Cls, cx: F.Cls, +rm: F.Rnd, +y: F.F64, +x: F.F64) -> Out:  match cy cx:    case F.FZero{sy} _:      Bool.pick(Out, F.signbit(x), Go{ZPi{U32.is_ne(sy, 0)}}, Val{y})    case F.FInf{sy} F.FInf{sx}:      Go{ZQuarterPi{U32.is_ne(sy, 0), U32.is_ne(sx, 0)}}    case F.FInf{sy} _:      Go{ZHalfPi{U32.is_ne(sy, 0)}}    case _ F.FZero{sx}:      Go{ZHalfPi{F.signbit(y)}}    case _ F.FInf{sx}:      Bool.pick(Out, U32.is_eq(sx, 0), Val{F.zero(F.sign_of(F.hi_bits(y)))}, Go{ZPi{F.signbit(y)}})    case _ _:      atan2_q(rm, F.div(y, x), y, x)def atan2_n(nx: Bool, ny: Bool, +rm: F.Rnd, +y: F.F64, +x: F.F64) -> Out:  match nx ny:    case True{} _:      nanv(x)    case False{} True{}:      nanv(y)    case False{} False{}:      atan2_c(F.cls(y), F.cls(x), rm, y, x)# atan(y / x) in the quadrant of (x, y), in [-pi, pi], rounded in mode rm (C99's signed zeros and infinities; of two# NaNs, x's)def arctan2_m(+rm: F.Rnd, +y: F.F64, +x: F.F64) -> F.F64:  out(atan2_n(F.is_nan(x), F.is_nan(y), rm, y, x), rm)# atan(y / x) in the quadrant of (x, y), to nearestdef arctan2(+y: F.F64, +x: F.F64) -> F.F64:  arctan2_m(F.RNearEven{}, y, x)def hyp_big(+x: F.F64, +k: U32) -> Bool:  F.lt(F.F64{k, 0}, F.abs(x))# |x| < 2^-40: x a hair away from zerodef sinh_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FFin{+s, e, m}:      Bool.pick(Out, hyp_big(x, 1082537984), Val{F.overflow(rm, s)}, Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, x, U32.is_eq(s, 0))}, Go{ZSinh{x}}))    case _:      Val{x}def sinh_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(sinh_c(F.cls(x), rm, x), rm)# |x| < 2^-40: 1 and a hairdef cosh_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{F.inf(0)}    case F.FZero{s}:      Val{one()}    case F.FFin{s, e, m}:      Bool.pick(Out, hyp_big(x, 1082537984), Val{F.overflow(rm, 0)}, Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, one(), True{})}, Go{ZCosh{x}}))def cosh_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(cosh_c(F.cls(x), rm, x), rm)# past 22: +-1 a hair toward zero; |x| < 2^-40: x a hair toward zerodef tanh_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      Val{F.F64{2146959360, 0}}    case F.FInf{s}:      Val{F.F64{U32.or(s, 1072693248), 0}}    case F.FZero{s}:      Val{x}    case F.FFin{+s, e, m}:      Bool.pick(Out, hyp_big(x, 1077280768), Val{nudge(rm, F.F64{U32.or(s, 1072693248), 0}, U32.is_ne(s, 0))},        Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, x, U32.is_ne(s, 0))}, Go{ZTanh{x}}))# the hyperbolic tangent (a NaN gives the plain quiet NaN, as NumPy's own tanh does)def tanh_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(tanh_c(F.cls(x), rm, x), rm)# |x| < 2^-40: x a hair toward zerodef asinh_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FFin{s, e, m}:      Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, x, F.signbit(x))}, Go{ZAsinh{x}})    case _:      Val{x}def arcsinh_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(asinh_c(F.cls(x), rm, x), rm)def acosh_c(c: F.Cls, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{Bool.pick(F.F64, U32.is_eq(s, 0), x, neg_nan())}    case F.FZero{s}:      Val{neg_nan()}    case F.FFin{s, e, m}:      Bool.pick(Out, F.lt(x, one()), Val{neg_nan()}, Bool.pick(Out, F.eq(x, one()), Val{F.zero(0)}, Go{ZAcosh{x}}))def arccosh_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(acosh_c(F.cls(x), x), rm)# |x| < 2^-40: x a hair away from zerodef atanh_c(c: F.Cls, +rm: F.Rnd, +x: F.F64) -> Out:  match c:    case F.FNan{}:      nanv(x)    case F.FInf{s}:      Val{neg_nan()}    case F.FZero{s}:      Val{x}    case F.FFin{+s, e, m}:      Bool.pick(Out, above_one(x), Val{neg_nan()}, Bool.pick(Out, F.eq(F.abs(x), one()), Val{F.inf(s)},        Bool.pick(Out, tiny(x, 1030750208), Val{nudge(rm, x, U32.is_eq(s, 0))}, Go{ZAtanh{x}})))def arctanh_body(+rm: F.Rnd, +x: F.F64) -> F.F64:  out(atanh_c(F.cls(x), rm, x), rm)# ---- power ----def ctz_w(+m: W.W64) -> U32:  (63 - W.clz(W.and(m, W.sub(W.w(0), m))) : U32)# v / 2^j for a signed v that 2^j divides (j below 32)def sdiv2j(+v: U32, +j: U32) -> U32:  Bool.pick(U32, is_neg(v), (0 - W.shr32((0 - v : U32), j) : U32), W.shr32(v, j))# x = w 2^g, w odd; |y| = Y 2^f, Y oddtype PX is Data:  PX{w: List<&2, U32>, g: U32, yo: W.W64, f: U32}def px_of(cx: F.Cls, cy: F.Cls) -> PX:  match cx cy:    case F.FFin{sx, +ex, +mx} F.FFin{sy, +ey, +my}:      +tx = ctz_w(mx)      +ty = ctz_w(my)      PX{B.of_w64(W.shr(mx, tx)), (ex - 1075 + tx : U32), W.shr(my, ty), (ey - 1075 + ty : U32)}    case _ _:      PX{[1], 0, W.w(1), 0}# (w 2^g)^n exactly, 0 < |n| <= 64def pow_int(+rm: F.Rnd, +s: U32, +w: List<&2, U32>, +g: U32, +n: U32) -> F.F64:  +a = Bool.pick(U32, is_neg(n), (0 - n : U32), n)  +pw = B.pow(w, a)  +sh = (B.bitlen(pw) + 70 : U32)  +qr = B.divq(B.shl([1], sh), pw)  Bool.pick(F.F64, is_neg(n), round_q(rm, s, qr_q(qr), Bool.not(B.is_zero(qr_r(qr))), (0 - sh - g * a : U32)), round_q(rm, s, pw, False{}, (g * a : U32)))# 2^r for a signed r, or past the range on r's side when hugedef two_to(+rm: F.Rnd, +s: U32, +huge: Bool, +up: Bool, +r: U32) -> F.F64:  Bool.pick(F.F64, huge, Bool.pick(F.F64, up, F.overflow(rm, s), F.underflow(rm, s)), F.round_pack_m(rm, s, (1084 + r : U32), W.w(1)))# x = 2^g: x^y = 2^(g y), exact when g y is an integer (x = 1 here only as |-1|: the answer is +-1)def pow_two(+rm: F.Rnd, +s: U32, +g: U32, +yo: W.W64, +f: U32, +yneg: Bool, +neg: Bool, +x: F.F64, +y: F.F64) -> Out:  +big = Bool.or(Bool.not(W.lt(yo, W.w(4096))), Bool.and(Bool.not(is_neg(f)), U32.is_lt(12, f)))  +up = Bool.not(Bool.xor(yneg, is_neg(g)))  +yv = Bool.pick(U32, yneg, (0 - W.low(yo) : U32), W.low(yo))  +j = (0 - f : U32)  Bool.pick(Out, U32.is_eq(g, 0), Val{F.F64{U32.or(s, 1072693248), 0}}, Bool.pick(Out, Bool.not(is_neg(f)), Val{two_to(rm, s, big, up, (g * yv * W.shl32(1, f) : U32))},    Bool.pick(Out, Bool.and(U32.is_le(j, 11), U32.is_eq(U32.and(g, (W.shl32(1, j) - 1 : U32)), 0)), Val{two_to(rm, s, big, up, (sdiv2j(g, j) * yv : U32))},      Go{ZPow{neg, x, y}})))# the 2^j-th root of w, when w is a perfect 2^j-th powerdef root2j(n: Nat, +w: List<&2, U32>) -> Maybe<&2, List<&2, U32>>:  match n:    case 0n:      Some{w}    case 1n+k:      +qr = B.isqrt2(w)      Bool.pick(Maybe<&2, List<&2, U32>>, B.is_zero(qr_r(qr)), root2j(k, qr_q(qr)), None{})# y = Y / 2^j > 0: when w = t^(2^j) and 2^j divides g Y, x^y = t^Y 2^(g Y / 2^j) exactlydef pow_root(r: Maybe<&2, List<&2, U32>>, +rm: F.Rnd, +s: U32, +g: U32, +ya: U32, +j: U32, +neg: Bool, +x: F.F64, +y: F.F64) -> Out:  match r:    case Some{t}:      Bool.pick(Out, U32.is_eq(U32.and((g * ya : U32), (W.shl32(1, j) - 1 : U32)), 0), Val{round_q(rm, s, B.pow(t, ya), False{}, sdiv2j((g * ya : U32), j))}, Go{ZPow{neg, x, y}})    case None{}:      Go{ZPow{neg, x, y}}# which way x^y is computed: x a power of two, y a small integer, y a small odd multiple of 2^-j (j <= 5), or ballstype PK is Data:  KTwo{}  KInt{}  KRoot{}  KBall{}def pk_of(+w: List<&2, U32>, +f: U32, +y: F.F64, +yo: W.W64) -> PK:  Bool.pick(PK, B.lt(w, [2]), KTwo{},    Bool.pick(PK, Bool.and(Bool.not(is_neg(f)), F.le(F.abs(y), F.F64{1078984704, 0})), KInt{},      Bool.pick(PK, Bool.and(Bool.and(is_neg(f), U32.is_le((0 - f : U32), 5)), Bool.and(Bool.not(F.signbit(y)), W.lt(yo, W.w(65)))), KRoot{}, KBall{})))def pow_k(k: PK, +rm: F.Rnd, +w: List<&2, U32>, +g: U32, +yo: W.W64, +f: U32, +neg: Bool, +x: F.F64, +y: F.F64) -> Out:  match k:    case KTwo{}:      pow_two(rm, sign_word(neg), g, yo, f, F.signbit(y), neg, x, y)    case KInt{}:      Val{pow_int(rm, sign_word(neg), w, g, F.to_i32(y))}    case KRoot{}:      pow_root(root2j(U32.to_nat((0 - f : U32)), w), rm, sign_word(neg), g, W.low(yo), (0 - f : U32), neg, x, y)    case KBall{}:      Go{ZPow{neg, x, y}}def pow_px(p: PX, +rm: F.Rnd, +neg: Bool, +x: F.F64, +y: F.F64) -> Out:  match p:    case PX{+w, +g, +yo, +f}:      pow_k(pk_of(w, f, y, yo), rm, w, g, yo, f, neg, F.abs(x), y)# C99's table, in order; x and y not NaN unless saiddef pow_y_inf(+x: F.F64, +y: F.F64) -> Out:  Bool.pick(Out, F.eq(F.abs(x), one()), Val{one()},    Val{Bool.pick(F.F64, Bool.xor(F.lt(F.abs(x), one()), F.signbit(y)), F.zero(0), F.inf(0))})def pow_x_inf(+x: F.F64, +y: F.F64) -> Out:  +odd = Bool.and(F.signbit(x), is_odd(y))  Val{Bool.pick(F.F64, F.signbit(y), F.zero(sign_word(odd)), F.inf(sign_word(odd)))}def pow_x_zero(+x: F.F64, +y: F.F64) -> Out:  +odd = is_odd(y)  Val{Bool.pick(F.F64, F.signbit(y), F.inf(Bool.pick(U32, odd, F.sign_of(F.hi_bits(x)), 0)), Bool.pick(F.F64, odd, x, F.zero(0)))}# x > 0 and |y| < 2^-80: 1 and a hair, above when y and log(x) have one signdef pow_fin(+rm: F.Rnd, +x: F.F64, +y: F.F64) -> Out:  Bool.pick(Out, F.signbit(x), Bool.pick(Out, is_int(y), pow_px(px_of(F.cls(x), F.cls(y)), rm, is_odd(y), x, y), Val{neg_nan()}),    Bool.pick(Out, tiny(y, 988807168), Val{nudge(rm, one(), Bool.not(Bool.xor(F.signbit(y), F.lt(x, one()))))}, pow_px(px_of(F.cls(x), F.cls(y)), rm, False{}, x, y)))type PC is Data:  CY0{}  CX1{}  CXNan{}  CYNan{}  CYInf{}  CXInf{}  CXZero{}  CFin{}def pc_of(+x: F.F64, +y: F.F64) -> PC:  Bool.pick(PC, F.eq(y, F.zero(0)), CY0{}, Bool.pick(PC, F.eq(x, one()), CX1{}, Bool.pick(PC, F.is_nan(x), CXNan{}, Bool.pick(PC, F.is_nan(y), CYNan{},    Bool.pick(PC, F.is_inf(y), CYInf{}, Bool.pick(PC, F.is_inf(x), CXInf{}, Bool.pick(PC, F.eq(x, F.zero(0)), CXZero{}, CFin{})))))))def pow_pc(c: PC, +rm: F.Rnd, +x: F.F64, +y: F.F64) -> Out:  match c:    case CY0{}:      Bool.pick(Out, is_snan(x), nanv(x), Val{one()})    case CX1{}:      Bool.pick(Out, is_snan(y), nanv(y), Val{one()})    case CXNan{}:      Bool.pick(Out, is_odd(y), Val{F.quiet(F.abs(x))}, nanv(x))    case CYNan{}:      nanv(y)    case CYInf{}:      pow_y_inf(x, y)    case CXInf{}:      pow_x_inf(x, y)    case CXZero{}:      pow_x_zero(x, y)    case CFin{}:      pow_fin(rm, x, y)# x^y rounded in mode rm (C99's special cases; exact whenever the answer is exact or halfway, every other answer# correctly rounded). A NaN x passes through quieted, its sign cleared when y is an odd integer, as glibc's pow does.def power_m(+rm: F.Rnd, +x: F.F64, +y: F.F64) -> F.F64:  out(pow_pc(pc_of(x, y), rm, x, y), rm)# x^y, to nearestdef power(+x: F.F64, +y: F.F64) -> F.F64:  power_m(F.RNearEven{}, x, y)# ---- every one-argument function: a NaN first ----type UK is Data:  UExp{}  UExp2{}  UExpm1{}  ULog{}  ULog2{}  ULog10{}  ULog1p{}  USin{}  UCos{}  UTan{}  UArcsin{}  UArccos{}  UArctan{}  USinh{}  UCosh{}  UTanh{}  UArcsinh{}  UArccosh{}  UArctanh{}  UCbrt{}def nan_of(k: UK, +x: F.F64) -> F.F64:  match k:    case UTanh{}:      F.F64{2146959360, 0}    case _:      F.quiet(x)def unary_go(k: UK, +rm: F.Rnd, +x: F.F64) -> F.F64:  match k:    case UExp{}:      exp_body(rm, x)    case UExp2{}:      exp2_body(rm, x)    case UExpm1{}:      expm1_body(rm, x)    case ULog{}:      log_body(rm, x)    case ULog2{}:      log2_body(rm, x)    case ULog10{}:      log10_body(rm, x)    case ULog1p{}:      log1p_body(rm, x)    case USin{}:      sin_body(rm, x)    case UCos{}:      cos_body(rm, x)    case UTan{}:      tan_body(rm, x)    case UArcsin{}:      arcsin_body(rm, x)    case UArccos{}:      arccos_body(rm, x)    case UArctan{}:      arctan_body(rm, x)    case USinh{}:      sinh_body(rm, x)    case UCosh{}:      cosh_body(rm, x)    case UTanh{}:      tanh_body(rm, x)    case UArcsinh{}:      arcsinh_body(rm, x)    case UArccosh{}:      arccosh_body(rm, x)    case UArctanh{}:      arctanh_body(rm, x)    case UCbrt{}:      cbrt_body(rm, x)# k's function at x rounded in mode rm, a NaN passing through quieted (tanh's gives the plain quiet NaN, as NumPy's own tanh does)def unary(na: Bool, k: UK, +rm: F.Rnd, +x: F.F64) -> F.F64:  match na:    case True{}:      nan_of(k, x)    case False{}:      unary_go(k, rm, x)# e^x, rounded in mode rmdef exp_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UExp{}, rm, x)# e^xdef exp(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UExp{}, F.RNearEven{}, x)# 2^x (exact for integers), rounded in mode rmdef exp2_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UExp2{}, rm, x)# 2^x (exact for integers)def exp2(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UExp2{}, F.RNearEven{}, x)# e^x - 1, exact near 0, rounded in mode rmdef expm1_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UExpm1{}, rm, x)# e^x - 1, exact near 0def expm1(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UExpm1{}, F.RNearEven{}, x)# the natural logarithm, rounded in mode rmdef log_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog{}, rm, x)# the natural logarithmdef log(+x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog{}, F.RNearEven{}, x)# the base-2 logarithm (exact for powers of two), rounded in mode rmdef log2_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog2{}, rm, x)# the base-2 logarithm (exact for powers of two)def log2(+x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog2{}, F.RNearEven{}, x)# the base-10 logarithm (exact for 10^k), rounded in mode rmdef log10_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog10{}, rm, x)# the base-10 logarithm (exact for 10^k)def log10(+x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog10{}, F.RNearEven{}, x)# log(1 + x), exact near 0, rounded in mode rmdef log1p_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog1p{}, rm, x)# log(1 + x), exact near 0def log1p(+x: F.F64) -> F.F64:  unary(F.is_nan(x), ULog1p{}, F.RNearEven{}, x)# the sine (Payne and Hanek's reduction with 2/pi to 3600 bits, so right for any x), rounded in mode rmdef sin_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), USin{}, rm, x)# the sine (Payne and Hanek's reduction with 2/pi to 3600 bits, so right for any x)def sin(+x: F.F64) -> F.F64:  unary(F.is_nan(x), USin{}, F.RNearEven{}, x)# the cosine, rounded in mode rmdef cos_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UCos{}, rm, x)# the cosinedef cos(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UCos{}, F.RNearEven{}, x)# the tangent, rounded in mode rmdef tan_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UTan{}, rm, x)# the tangentdef tan(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UTan{}, F.RNearEven{}, x)# the arcsine, in [-pi/2, pi/2], rounded in mode rmdef arcsin_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UArcsin{}, rm, x)# the arcsine, in [-pi/2, pi/2]def arcsin(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UArcsin{}, F.RNearEven{}, x)# the arccosine, in [0, pi], rounded in mode rmdef arccos_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UArccos{}, rm, x)# the arccosine, in [0, pi]def arccos(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UArccos{}, F.RNearEven{}, x)# the arctangent, in [-pi/2, pi/2], rounded in mode rmdef arctan_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UArctan{}, rm, x)# the arctangent, in [-pi/2, pi/2]def arctan(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UArctan{}, F.RNearEven{}, x)# the hyperbolic sine, rounded in mode rmdef sinh_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), USinh{}, rm, x)# the hyperbolic sinedef sinh(+x: F.F64) -> F.F64:  unary(F.is_nan(x), USinh{}, F.RNearEven{}, x)# the hyperbolic cosine, rounded in mode rmdef cosh_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UCosh{}, rm, x)# the hyperbolic cosinedef cosh(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UCosh{}, F.RNearEven{}, x)# the hyperbolic tangent, rounded in mode rmdef tanh_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UTanh{}, rm, x)# the hyperbolic tangentdef tanh(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UTanh{}, F.RNearEven{}, x)# the inverse hyperbolic sine, rounded in mode rmdef arcsinh_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UArcsinh{}, rm, x)# the inverse hyperbolic sinedef arcsinh(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UArcsinh{}, F.RNearEven{}, x)# the inverse hyperbolic cosine, x >= 1, rounded in mode rmdef arccosh_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UArccosh{}, rm, x)# the inverse hyperbolic cosine, x >= 1def arccosh(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UArccosh{}, F.RNearEven{}, x)# the inverse hyperbolic tangent, |x| <= 1, rounded in mode rmdef arctanh_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UArctanh{}, rm, x)# the inverse hyperbolic tangent, |x| <= 1def arctanh(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UArctanh{}, F.RNearEven{}, x)# the cube root (exact: an integer cube root with its remainder), rounded in mode rmdef cbrt_m(+rm: F.Rnd, +x: F.F64) -> F.F64:  unary(F.is_nan(x), UCbrt{}, rm, x)# the cube root (exact: an integer cube root with its remainder)def cbrt(+x: F.F64) -> F.F64:  unary(F.is_nan(x), UCbrt{}, F.RNearEven{}, x)