format.bend source
format.bend on the hub · documented module
# bend-float64's fixed-digit formatting, as Python's % operator does it: fixed(x, n) is '%.nf', sci(x, n) is# '%.ne', general(x, n) is '%.ng'. Any number of digits; the digits are those of the exact binary value, rounded half# to even, computed with big integers (no approximation). Pure.import Baseimport ./wide.bend as Wimport ./big.bend as Bimport ./float64.bend as Fimport ./text.bend as Tdef is_neg(+x: U32) -> Bool: U32.is_le(2147483648, x)# ---- the exact value times a power of ten, rounded half to even ----# (num / 2^k) rounded half to even, k > 0def round_shift(+num: List<&2, U32>, +k: U32) -> List<&2, U32>: +q = B.shr(num, k) +half = B.low_nonzero(B.shr(num, (k - 1 : U32)), 1) +below = B.low_nonzero(num, (k - 1 : U32)) Bool.pick(List<&2, U32>, Bool.and(half, Bool.or(below, B.low_nonzero(q, 1))), B.add(q, [1]), q)def round_qr(r: B.QR, +den: List<&2, U32>) -> List<&2, U32>: match r: case B.QR{+q, rem}: +c = B.cmp(B.shl(rem, 1), den) Bool.pick(List<&2, U32>, Bool.or(Cmp.is_gt(c), Bool.and(Cmp.is_eq(c), B.low_nonzero(q, 1))), B.add(q, [1]), q)# (num / den) rounded half to even, den > 1def round_div(+num: List<&2, U32>, +den: List<&2, U32>) -> List<&2, U32>: round_qr(B.divq(num, den), den)type Way is Data: WExact{} WShift{} WDiv{}def scaled_way(w: Way, +m: List<&2, U32>, +q: U32, +s: U32) -> List<&2, U32>: match w: case WExact{}: B.shl(B.mul_pow10(m, s), q) case WShift{}: round_shift(B.mul_pow10(m, s), (0 - q : U32)) case WDiv{}: round_div(B.shl(B.mul_pow10(m, Bool.pick(U32, is_neg(s), 0, s)), Bool.pick(U32, is_neg(q), 0, q)), B.shl(B.pow10(Bool.pick(U32, is_neg(s), (0 - s : U32), 0)), Bool.pick(U32, is_neg(q), (0 - q : U32), 0)))# m 2^q 10^s rounded half to even to an integer (q, s signed)def scaled(+m: List<&2, U32>, +q: U32, +s: U32) -> List<&2, U32>: scaled_way(Bool.pick(Way, is_neg(s), WDiv{}, Bool.pick(Way, is_neg(q), WShift{}, WExact{})), m, q, s)# ---- decimal digits of a big integer ----def ds_q(d: B.DS) -> List<&2, U32>: match d: case B.DS{q, r}: qdef ds_r(d: B.DS) -> U32: match d: case B.DS{q, r}: rdef four(+r: U32) -> List<&2, U32>: [U32.div(r, 1000), U32.mod(U32.div(r, 100), 10), U32.mod(U32.div(r, 10), 10), U32.mod(r, 10)]# xs's digits, four at a time from the bottom, most significant first (with leading zeros)def chunks(n: Nat, +xs: List<&2, U32>, +acc: List<&2, U32>) -> List<&2, U32>: match n: case 0n: acc case 1n+k: +d = B.div_small(xs, 10000) Bool.pick(List<&2, U32>, B.is_zero(xs), acc, chunks(k, ds_q(d), List.append(&2, U32, four(ds_r(d)), acc)))# the decimal digits of xs, most significant first, no leading zeros ([0] for 0)def nonempty(ds: List<&2, U32>) -> List<&2, U32>: match ds: case []: [0] case d <> t: d <> tdef digits(+xs: List<&2, U32>) -> List<&2, U32>: nonempty(T.drop_lz(chunks(U32.to_nat((U32.div(B.bitlen(xs), 13) + 2 : U32)), xs, [])))# n zeros then dsdef pad(n: Nat, ds: List<&2, U32>) -> List<&2, U32>: match n: case 0n: ds case 1n+k: 0 <> pad(k, ds)# ---- the three layouts ----def sign_str(+neg: Bool) -> String: Bool.pick(String, neg, "-", "")def zeros(+n: U32) -> String: T.zero_chars(U32.to_nat(n), "")def dstr(ds: List<&2, U32>) -> String: T.digit_chars(ds, "")# ds (at least n + 1 digits) with a point before the last ndef point_at(+ds: List<&2, U32>, +n: U32) -> String: +k = (T.len_d(ds) - n : U32) dstr(T.take_d(U32.to_nat(k), ds)) ++ Bool.pick(String, U32.is_eq(n, 0), "", "." ++ dstr(T.drop_d(U32.to_nat(k), ds)))# the digits of R, padded with zeros on the left to n + 1 at leastdef pad_go(enough: Bool, +ds: List<&2, U32>, +n: U32, +l: U32) -> List<&2, U32>: match enough: case True{}: ds case False{}: pad(U32.to_nat((n + 1 - l : U32)), ds)def padded(+r: List<&2, U32>, +n: U32) -> List<&2, U32>: +ds = digits(r) +l = T.len_d(ds) pad_go(U32.is_lt(n, l), ds, n, l)# n + 1 significant digits of m 2^q, and the decimal exponent x of the first: the exponent estimate x0 (from the# shortest digits; it can be one off near a power of ten) is corrected while the digit count is wrongtype Sig is Data: Sig{x: U32, r: List<&2, U32>}def sig_pick(c: Cmp, +x: U32, +r: List<&2, U32>, more: U32 -> Sig) -> Sig: match c: case LT{}: more((x - 1 : U32)) case GT{}: more((x + 1 : U32)) case EQ{}: Sig{x, r}def sig_go(fuel: Nat, +m: List<&2, U32>, +q: U32, +n: U32, +x: U32) -> Sig: match fuel: case 0n: Sig{x, scaled(m, q, (n - x : U32))} case 1n+f: +r = scaled(m, q, (n - x : U32)) sig_pick(U32.cmp(T.len_d(digits(r)), (n + 1 : U32)), x, r, y => sig_go(f, m, q, n, y))def decpt(d: T.Digits) -> U32: match d: case T.Digits{ds, k}: kdef sig(+e: U32, +m: W.W64, +n: U32) -> Sig: sig_go(3n, B.of_w64(m), (e - 1075 : U32), n, (decpt(T.shortest(e, m)) - 1 : U32))def exp_part(+x: U32) -> String: "e" ++ T.exp_text(x)def sci_sig(s: Sig, +n: U32) -> String: match s: case Sig{x, r}: point_at(digits(r), n) ++ exp_part(x)# without trailing zeros after the point, nor the point if nothing is left after itdef strip_frac(+ds: List<&2, U32>) -> List<&2, U32>: List.reverse(&2, U32, T.drop_lz(List.reverse(&2, U32, ds)))def g_point(+ds: List<&2, U32>, +k: U32) -> String: +ip = T.take_d(U32.to_nat(k), ds) +fp = strip_frac(T.drop_d(U32.to_nat(k), ds)) dstr(ip) ++ Bool.pick(String, U32.is_eq(T.len_d(fp), 0), "", "." ++ dstr(fp))# %g's three shapes: 0.000ddd (x from -4 to -1), ddd.ddd (x from 0 to p - 1), d.ddde+xxtype GL is Data: GSmall{} GFixed{} GSci{}def g_lay(g: GL, +ds: List<&2, U32>, +x: U32) -> String: match g: case GSmall{}: "0." ++ zeros((0 - x - 1 : U32)) ++ dstr(strip_frac(ds)) case GFixed{}: g_point(ds, (x + 1 : U32)) case GSci{}: g_point(ds, 1) ++ exp_part(x)# %g with p significant digits: fixed when -4 <= x < p, else scientific; trailing zeros droppeddef gen_sig(s: Sig, +p: U32) -> String: match s: case Sig{+x, r}: g_lay(Bool.pick(GL, Bool.and(Bool.not(F.s_lt(x, (0 - 4 : U32))), F.s_lt(x, p)), Bool.pick(GL, is_neg(x), GSmall{}, GFixed{}), GSci{}), digits(r), x)# ---- the public functions ----type Fmt is Data: KFixed{} KSci{} KGen{}def zero_text(k: Fmt, +neg: Bool, +n: U32) -> String: match k: case KFixed{}: sign_str(neg) ++ "0" ++ Bool.pick(String, U32.is_eq(n, 0), "", "." ++ zeros(n)) case KSci{}: sign_str(neg) ++ "0" ++ Bool.pick(String, U32.is_eq(n, 0), "", "." ++ zeros(n)) ++ "e+00" case KGen{}: sign_str(neg) ++ "0"def fin_text(k: Fmt, +neg: Bool, +e: U32, +m: W.W64, +n: U32) -> String: match k: case KFixed{}: sign_str(neg) ++ point_at(padded(scaled(B.of_w64(m), (e - 1075 : U32), n), n), n) case KSci{}: sign_str(neg) ++ sci_sig(sig(e, m, n), n) case KGen{}: +p = Bool.pick(U32, U32.is_eq(n, 0), 1, n) sign_str(neg) ++ gen_sig(sig(e, m, (p - 1 : U32)), p)def text_c(c: F.Cls, +k: Fmt, +n: U32) -> String: match c: case F.FNan{}: "nan" case F.FInf{s}: Bool.pick(String, U32.is_eq(s, 0), "inf", "-inf") case F.FZero{s}: zero_text(k, U32.is_ne(s, 0), n) case F.FFin{s, e, m}: fin_text(k, U32.is_ne(s, 0), e, m, n)# Python's '%.nf' % x: n digits after the point (none, and no point, for n = 0), rounded half to evendef fixed(x: F.F64, +n: U32) -> String: text_c(F.cls(x), KFixed{}, n)# Python's '%.ne' % x: one digit, a point and n more (no point for n = 0), then e, a sign and at least two exponent# digitsdef sci(x: F.F64, +n: U32) -> String: text_c(F.cls(x), KSci{}, n)# Python's '%.ng' % x: n significant digits (1 for n = 0), fixed when the exponent is from -4 up to below n, else# scientific, with trailing zeros (and a bare point) droppeddef general(x: F.F64, +n: U32) -> String: text_c(F.cls(x), KGen{}, n)