From 59f8ade1c313e758a20d3a7f8b89086cd244a214 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:02:24 +0100 Subject: [PATCH] Fmt: short, a real in the fewest digits that read back x, lo and hi, the halves to the F64s around it, are written out exactly in decimal (base-10^9 big numbers, as arkusz's Afmt does), and the two numbers of p digits nearest x are tried for p from 1 until one lies between: no 17-digit rounding first, so the answer is always the shortest and the nearest. With an exponent below 1E-4 and from 1E15 on; -0.0 is -0. test/run/FmtShort checks round trips and that a digit fewer fails, over powers of ten, hard values and 20,000 random bit patterns. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 06740a3..5e7d5a1 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -704,6 +704,7 @@ char(c: U8); bool(b: BOOL); real(r: F64; places: INT); + short(r: F64); end; proc print*(f: []U8; args: []Arg); (* to standard output *) proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) @@ -908,7 +909,13 @@ default rounding, as Pascal's `Round`, where `round` is C's. `Fmt.real(x, places)` writes `x` with `places` digits after the point, at most 9, rounded to the nearest; from 1E15 on, with an exponent, as `1.50E17`; and `nan`, -`inf` and `-inf` as themselves. `hex` writes capitals and `lowHex` +`inf` and `-inf` as themselves. `Fmt.short(x)` writes `x` in the +fewest significant digits that `Math.toReal` reads back as the same +`F64`, and of two such the nearer `x`: `0.1`, not +`0.10000000000000001`; `42`, with no point; `0.0001`; and below 1E-4 +or from 1E15 on with an exponent, as `1.5E17` or `5E-324`. Zero keeps +its sign, `0` or `-0`, and `nan` and `inf` are as `real` writes them. +`hex` writes capitals and `lowHex` lower case, neither with a `0x`. A width between the braces pads an argument to that many bytes: `{2}` with spaces on the left, `{02}` with zeros on the left, after a sign (`{04}` of -7 is `-007`), `{-10}` diff --git a/lib/Fmt.raz b/lib/Fmt.raz index eb18302..940f64b 100644 --- a/lib/Fmt.raz +++ b/lib/Fmt.raz @@ -10,11 +10,28 @@ module Fmt; import Out, Buf, Arena, Rt, Sys; +const + Limb = 1000000000; (* a Big's base, 10^9 *) + LimbDigits = 9; (* the decimal digits of a limb *) + LimbsMax = 90; (* limbs of the largest Big: 769 digits, for the least F64's lo *) + MostDigits = 17; (* digits that always read back as the F64 they came from *) + MantBits = 52; (* an F64's fraction bits *) + ExpBias = 1075; (* its exponent's bias, with the fraction a whole number *) + ExpMask = 7FFH; (* its exponent's bits, shifted down *) + TwoStep = 29; (* doublings at once: 10^9 * 2^29 < 2^63 *) + FiveStep = 13; (* fives multiplied at once: 10^9 * 5^13 < 2^63 *) + type (* An argument, and how it is printed. A real has places digits after the point, at most 9, rounded to the nearest, half away from zero; from 1E15 on, it has the exponent that makes it less - than 10, as 1.50E17; nan and inf are spelled so. *) + than 10, as 1.50E17; nan and inf are spelled so. A short has the + fewest significant digits that Math.toReal reads back as the + same F64, the nearer of two such, and of two as near the one + whose last digit is even; it is written as 0.0001, 0.1, 42 or + 123456789012345, and with an exponent below 1E-4 and from 1E15 + on, as 1.5E17 or 5E-324. Zero is 0 or -0, keeping its sign so + that it too reads back; nan and inf are spelled as real's. *) Arg* = variant int(i: I64); (* in decimal *) uint(u: U64); (* in decimal *) @@ -24,6 +41,7 @@ char(c: U8); (* the one byte *) bool(b: BOOL); (* true or false *) real(r: F64; places: INT); + short(r: F64); end; (* Where the pieces go: to out if slice is set, else to b, or to @@ -43,8 +61,28 @@ Spec = record width: INT; zero, left: BOOL; + end; + + (* A whole number, not negative, in base 10^9, the lowest limb + first. *) + Big = record + v: [LimbsMax]U64; + n: INT; (* the limbs in use *) end; + (* A number above 0, exactly in decimal: 0.d times 10^e, its first + digit not 0. *) + Exact = record + d: [LimbsMax * LimbDigits]U8; (* '0' to '9' *) + n, e: INT; (* how many digits, and the exponent *) + end; + + (* A number above 0 as short writes it, in the same way. *) + Short = record + d: [MostDigits]U8; + n, e: INT; + end; + (* The digits of u in base, at the end of the array: it and where they start. Digits past 9 are letters from letter on, 'A' or 'a'. *) proc digits(u: U64; base: U64; letter: U8): ([24]U8, INT); @@ -127,8 +165,230 @@ put(k, "E"); put(k, d[at..24]); end; +end; + +(* ---- short: the fewest digits that read back ---- + + An F64 x is m times 2^k, m and k whole numbers. Every number between + the halves to the F64s on either side of x reads back as x, so short + looks for the fewest digits that land between those halves, lo and + hi: + + below lo x hi above the F64s around x + |--------|------*------|--------| + + x, lo and hi are written out exactly in decimal, each an Exact, and + compared digit by digit with each try, so nothing is rounded on the + way. For p digits the tries are x cut to p digits and that one more + in its last place: the two numbers of p digits nearest x. The first + p for which one of them lies between lo and hi gives the answer; if + both do, the nearer x is taken. 17 digits are always enough. + + An exact value is made in a Big, a whole number in base 10^9: for k + at least 0, m times 2^k is m doubled k times; for k below 0, it is m + times 5^-k with the point -k places from the right, since 2^-k is + 5^-k / 10^-k. *) + +(* The bits of x. *) +proc bits(x: F64): U64; +var p: ^U64; +begin + p := ^U64(ADR(x)); + return p^; end; +(* b times f, which is at most 2^31. *) +proc bigMul(var b: Big; f: U64); +var i: INT; t, carry: U64; +begin + carry := 0; + for i := 0 to b.n - 1 do + t := b.v[i] * f + carry; + carry := t div Limb; + b.v[i] := t - carry * Limb; + end; + while carry > 0 do + b.v[b.n] := carry mod Limb; + carry := carry div Limb; + INC(b.n); + end; +end; + +(* b times 2^k, or 5^k if five, a few steps at once. *) +proc bigPow(var b: Big; k: INT; five: BOOL); +var step, i: INT; f: U64; +begin + while k > 0 do + if five then step := FiveStep; else step := TwoStep; end; + if k < step then step := k; end; + f := 1; + for i := 1 to step do + if five then f := f * 5; else f := f * 2; end; + end; + bigMul(b, f); + DEC(k, step); + end; +end; + +(* m times 2^k, m not 0, exactly in decimal, into w. *) +proc expand(m: U64; k: INT; var w: Exact); +var b: Big; d: [24]U8; at, i, j, point: INT; v: U64; +begin + b.n := 0; + while m > 0 do + b.v[b.n] := m mod Limb; + m := m div Limb; + INC(b.n); + end; + point := 0; + if k < 0 then point := -k; bigPow(b, -k, true); else bigPow(b, k, false); end; + (* the top limb without leading zeros, then nine digits a limb *) + d, at := digits(b.v[b.n - 1], 10, 'A'); + w.n := 24 - at; + COPY(w.d[0..w.n], d[at..24]); + for i := b.n - 2 to 0 by -1 do + v := b.v[i]; + for j := LimbDigits - 1 to 0 by -1 do + w.d[w.n + j] := U8(v mod 10) + '0'; + v := v div 10; + end; + INC(w.n, LimbDigits); + end; + w.e := w.n - point; +end; + +(* -1, 0 or 1 as 0.d times 10^e, d's first digit not 0, is below, at or + above w. *) +proc compare(d: []U8; e: INT; const w: Exact): INT; +var i, n: INT; a, c: U8; +begin + if e # w.e then + if e < w.e then return -1; end; + return 1; + end; + n := w.n; + if LEN(d) > n then n := LEN(d); end; + for i := 0 to n - 1 do + a := '0'; c := '0'; (* past its end each has zeros *) + if i < LEN(d) then a := d[i]; end; + if i < w.n then c := w.d[i]; end; + if a # c then + if a < c then return -1; end; + return 1; + end; + end; + return 0; +end; + +(* Whether s reads back as the F64 that lo and hi are the halves around: + it lies between them, or on one of them if ends, as the F64 whose m + is even takes a tie. *) +proc between(const s: Short; const lo, hi: Exact; ends: BOOL): BOOL; +var below, above: INT; +begin + below := compare(s.d[0..s.n], s.e, lo); + above := compare(s.d[0..s.n], s.e, hi); + return ((below > 0) or (below = 0) and ends) and ((above < 0) or (above = 0) and ends); +end; + +(* s one more in its last place, carrying, without the zeros that leaves + at the end: 129 becomes 13, and 99 becomes 1 with the point moved. *) +proc up(var s: Short); +var i: INT; +begin + i := s.n - 1; + while (i >= 0) and (s.d[i] = '9') do DEC(i); end; + if i >= 0 then + INC(s.d[i]); + s.n := i + 1; + else + s.d[0] := '1'; + s.n := 1; + INC(s.e); + end; +end; + +(* Whether x, cut where rest begins, is nearer the number one more in + the last place than to the cut one: rest is above a half, or a half + and last, the cut one's last digit, odd. *) +proc nearerUp(rest: []U8; last: U8): BOOL; +var i: INT; +begin + if rest[0] # '5' then return rest[0] > '5'; end; + for i := 1 to LEN(rest) - 1 do + if rest[i] # '0' then return true; end; + end; + return (last - '0') & 1 = 1; +end; + +(* The fewest digits that read back as x, finite and above 0, into s. *) +proc shortest(x: F64; var s: Short); +var u, m: U64; k, p: INT; mid, lo, hi: Exact; ends, downOk, upOk: BOOL; next: Short; +begin + (* x as m times 2^k: the fraction with its hidden 1 but for the + smallest exponent, the F64s below 2^-1022 *) + u := bits(x); + m := u & (U64(1) shl MantBits - 1); + k := INT(u shr MantBits & ExpMask); + if k = 0 then k := 1; else m := m | U64(1) shl MantBits; end; + DEC(k, ExpBias); + (* the halves to the F64s around x; the one below a power of two is + half as far as the one above, but at the smallest exponent *) + expand(m, k, mid); + expand(2 * m + 1, k - 1, hi); + if (m = U64(1) shl MantBits) and (k > 1 - ExpBias) then + expand(4 * m - 1, k - 2, lo); + else + expand(2 * m - 1, k - 1, lo); + end; + ends := m & 1 = 0; + for p := 1 to MostDigits do + s.n := p; + if p >= mid.n then s.n := mid.n; end; (* x itself has no more digits *) + s.e := mid.e; + COPY(s.d[0..s.n], mid.d[0..s.n]); + if s.n = mid.n then return; end; + next := s; + up(next); + downOk := between(s, lo, hi, ends); + upOk := between(next, lo, hi, ends); + if upOk and (not downOk or nearerUp(mid.d[p..mid.n], s.d[p - 1])) then + s := next; + return; + elsif downOk then + return; + end; + end; +end; + +(* x as Arg's short says, but for the minus sign, which arg writes. *) +proc putShort(var k: Sink; x: F64); +var s: Short; d: [24]U8; at, e: INT; +begin + if x # x then put(k, "nan"); return; end; + if x < 0.0 then x := -x; end; + if x > 1.7976931348623157E308 then put(k, "inf"); return; end; + if x = 0.0 then put(k, "0"); return; end; (* -0.0 too *) + shortest(x, s); + if (s.e >= -3) and (s.e <= 15) then (* from 1E-4 to below 1E15 *) + if s.e <= 0 then + put(k, "0."); fill(k, '0', -s.e); put(k, s.d[0..s.n]); + elsif s.e >= s.n then + put(k, s.d[0..s.n]); fill(k, '0', s.e - s.n); + else + put(k, s.d[0..s.e]); put(k, "."); put(k, s.d[s.e..s.n]); + end; + else + put(k, s.d[0..1]); + if s.n > 1 then put(k, "."); put(k, s.d[1..s.n]); end; + put(k, "E"); + e := s.e - 1; + if e < 0 then put(k, "-"); e := -e; end; + d, at := digits(U64(e), 10, 'A'); + put(k, d[at..24]); + end; +end; + (* Whether a is printed with a minus sign. *) proc negative(a: Arg): BOOL; var neg: BOOL; @@ -136,6 +396,7 @@ match a of | int(i): neg := i < 0; | real(r, _places): neg := r < 0.0; + | short(r): neg := (r = r) and (bits(r) shr 63 = 1); (* -0.0 too, but no nan *) | else: neg := false; end; return neg; @@ -160,6 +421,7 @@ | char(c): one[0] := c; put(k, one); | bool(t): if t then put(k, "true"); else put(k, "false"); end; | real(r, places): putReal(k, r, places); + | short(r): putShort(k, r); end; end; @@ -317,6 +579,20 @@ Buf.reset(b); write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]); if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end; + Buf.reset(b); + write(b, "{} {} {} {} {} {} {} {}", [short(0.1), short(1.0 / 3.0), short(-2.5), short(100.0), short(42.0E3), + short(0.0001), short(0.00012), short(123456789012345.0)]); + if Buf.bytes(b) # "0.1 0.3333333333333333 -2.5 100 42000 0.0001 0.00012 123456789012345" then Rt.stop("Fmt.short"); end; + Buf.reset(b); + write(b, "{} {} {} {} {} {} {}", [short(1.0E15), short(1.5E17), short(9.9E-5), short(5.0E-324), + short(1.7976931348623157E308), short(2.2250738585072014E-308), short(9007199254740992.0)]); + if Buf.bytes(b) # "1E15 1.5E17 9.9E-5 5E-324 1.7976931348623157E308 2.2250738585072014E-308 9.007199254740992E15" then + Rt.stop("Fmt.short: an exponent"); + end; + Buf.reset(b); + write(b, "{} {} {} {} {} {6}|{-6}|{06}", [short(zero), short(-zero), short(1.0 / zero), short(-1.0 / zero), + short(zero / zero), short(0.5), short(0.5), short(-0.5)]); + if Buf.bytes(b) # "0 -0 inf -inf nan 0.5|0.5 |-000.5" then Rt.stop("Fmt.short: zero, inf, nan and a width"); end; n, whole := into(small, "{}:{02}", [int(-3), int(7)]); if (small[0..n] # "-3:07") or not whole then Rt.stop("Fmt.into"); end; n, whole := into(small, "line {}, {}", [int(12), str("x")]); diff --git a/test/run/FmtShort.expect b/test/run/FmtShort.expect new file mode 100644 index 0000000..5e7f8ba --- /dev/null +++ b/test/run/FmtShort.expect @@ -0,0 +1 @@ +21266 values tried, 0 bad diff --git a/test/run/FmtShort.raz b/test/run/FmtShort.raz new file mode 100644 index 0000000..d0d4b26 --- /dev/null +++ b/test/run/FmtShort.raz @@ -0,0 +1,102 @@ +(* Fmt.short against Math.toReal: what it writes reads back as the + same F64, and one digit fewer does not. Tried on powers of ten, a + few numbers known to be hard, and random bit patterns. *) +module FmtShort; +import Fmt, Math, Buf, Heap; + +var + text, fewer: Buf.Buf; + tried, bad: INT; + +(* What toReal reads s as; a nan if it reads nothing. *) +proc read(s: []U8): F64; +var x: F64; ok: BOOL; +begin + x, ok := Math.toReal(s); + if not ok then x := Math.fromBits(7FF8000000000000H); end; + return x; +end; + +(* Whether y is x, -0.0 not being 0.0. *) +proc same(x, y: F64): BOOL; +begin + return Math.bits(x) = Math.bits(y); +end; + +(* The significant digits of s, as short writes it, as a whole number, + and the power of ten it is to be taken to: 1.5E17 is 15 and 16. *) +proc parse(s: []U8): (U64, INT); +var d: U64; e, i, ex: INT; point, neg: BOOL; +begin + while (i < LEN(s)) and (s[i] # 'E') do + if s[i] = '.' then + point := true; + elsif s[i] # '-' then + d := d * 10 + U64(s[i] - '0'); + if point then DEC(e); end; + end; + INC(i); + end; + if i < LEN(s) then + INC(i); + if s[i] = '-' then neg := true; INC(i); end; + while i < LEN(s) do ex := ex * 10 + INT(s[i] - '0'); INC(i); end; + if neg then ex := -ex; end; + end; + while (d > 0) and (d mod 10 = 0) do d := d div 10; INC(e); end; + return d, e + ex; +end; + +(* Whether d times 10^e reads back as x. *) +proc readsBack(d: U64; e: INT; x: F64): BOOL; +begin + Buf.reset(fewer); + Fmt.write(fewer, "{}E{}", [Fmt.uint(d), Fmt.int(e)]); + return same(read(Buf.bytes(fewer)), x); +end; + +(* x, finite and not 0, tried. *) +proc check(x: F64); +var d: U64; e: INT; +begin + INC(tried); + Buf.reset(text); + Fmt.write(text, "{}", [Fmt.short(x)]); + if not same(read(Buf.bytes(text)), x) then + INC(bad); + Fmt.print("{} reads back as another F64\n", [Fmt.str(Buf.bytes(text))]); + return; + end; + (* one digit fewer: the two numbers of that many digits around x *) + if x < 0.0 then x := -x; end; + d, e := parse(Buf.bytes(text)); + if (d >= 10) and (readsBack(d div 10, e + 1, x) or readsBack(d div 10 + 1, e + 1, x)) then + INC(bad); + Fmt.print("{} reads back in fewer digits\n", [Fmt.str(Buf.bytes(text))]); + end; +end; + +proc main*; +var x: F64; u: U64; i: INT; +begin + Buf.init(text, Heap.heap); + Buf.init(fewer, Heap.heap); + x := 1.0E-323; + while x < 1.0E308 do check(x); check(-x); x := x * 10.0; end; + x := 1.0; + for i := 0 to 1073 do x := x / 2.0; end; + check(x); (* the least F64, 5E-324 *) + check(0.1); check(0.2); check(0.3); check(1.0 / 3.0); check(2.0 / 3.0); + check(1.7976931348623157E308); check(2.2250738585072014E-308); check(2.2250738585072009E-308); + check(9007199254740992.0); check(9007199254740993.0); check(5.0E-324); check(123456789.0); + (* random bit patterns, nan and inf left out *) + u := 12345; + for i := 1 to 20000 do + u := u * 6364136223846793005 + 1442695040888963407; + x := Math.fromBits(u); + if (x = x) and (x - x = 0.0) and (x # 0.0) then check(x); end; + end; + Fmt.print("{} values tried, {} bad\n", [Fmt.int(tried), Fmt.int(bad)]); +end; + +end FmtShort.