| | @@ -10,11 +10,28 @@ |
| 10 | 10 | module Fmt; |
| 11 | 11 | import Out, Buf, Arena, Rt, Sys; |
| 12 | 12 | |
| 13 | +const |
| 14 | + Limb = 1000000000; (* a Big's base, 10^9 *) |
| 15 | + LimbDigits = 9; (* the decimal digits of a limb *) |
| 16 | + LimbsMax = 90; (* limbs of the largest Big: 769 digits, for the least F64's lo *) |
| 17 | + MostDigits = 17; (* digits that always read back as the F64 they came from *) |
| 18 | + MantBits = 52; (* an F64's fraction bits *) |
| 19 | + ExpBias = 1075; (* its exponent's bias, with the fraction a whole number *) |
| 20 | + ExpMask = 7FFH; (* its exponent's bits, shifted down *) |
| 21 | + TwoStep = 29; (* doublings at once: 10^9 * 2^29 < 2^63 *) |
| 22 | + FiveStep = 13; (* fives multiplied at once: 10^9 * 5^13 < 2^63 *) |
| 23 | + |
| 13 | 24 | type |
| 14 | 25 | (* An argument, and how it is printed. A real has places digits |
| 15 | 26 | after the point, at most 9, rounded to the nearest, half away |
| 16 | 27 | from zero; from 1E15 on, it has the exponent that makes it less |
| 17 | | - than 10, as 1.50E17; nan and inf are spelled so. *) |
| 28 | + than 10, as 1.50E17; nan and inf are spelled so. A short has the |
| 29 | + fewest significant digits that Math.toReal reads back as the |
| 30 | + same F64, the nearer of two such, and of two as near the one |
| 31 | + whose last digit is even; it is written as 0.0001, 0.1, 42 or |
| 32 | + 123456789012345, and with an exponent below 1E-4 and from 1E15 |
| 33 | + on, as 1.5E17 or 5E-324. Zero is 0 or -0, keeping its sign so |
| 34 | + that it too reads back; nan and inf are spelled as real's. *) |
| 18 | 35 | Arg* = variant |
| 19 | 36 | int(i: I64); (* in decimal *) |
| 20 | 37 | uint(u: U64); (* in decimal *) |
| | @@ -24,6 +41,7 @@ |
| 24 | 41 | char(c: U8); (* the one byte *) |
| 25 | 42 | bool(b: BOOL); (* true or false *) |
| 26 | 43 | real(r: F64; places: INT); |
| 44 | + short(r: F64); |
| 27 | 45 | end; |
| 28 | 46 | |
| 29 | 47 | (* Where the pieces go: to out if slice is set, else to b, or to |
| | @@ -43,8 +61,28 @@ |
| 43 | 61 | Spec = record |
| 44 | 62 | width: INT; |
| 45 | 63 | zero, left: BOOL; |
| 64 | + end; |
| 65 | + |
| 66 | + (* A whole number, not negative, in base 10^9, the lowest limb |
| 67 | + first. *) |
| 68 | + Big = record |
| 69 | + v: [LimbsMax]U64; |
| 70 | + n: INT; (* the limbs in use *) |
| 46 | 71 | end; |
| 47 | 72 | |
| 73 | + (* A number above 0, exactly in decimal: 0.d times 10^e, its first |
| 74 | + digit not 0. *) |
| 75 | + Exact = record |
| 76 | + d: [LimbsMax * LimbDigits]U8; (* '0' to '9' *) |
| 77 | + n, e: INT; (* how many digits, and the exponent *) |
| 78 | + end; |
| 79 | + |
| 80 | + (* A number above 0 as short writes it, in the same way. *) |
| 81 | + Short = record |
| 82 | + d: [MostDigits]U8; |
| 83 | + n, e: INT; |
| 84 | + end; |
| 85 | + |
| 48 | 86 | (* The digits of u in base, at the end of the array: it and where they |
| 49 | 87 | start. Digits past 9 are letters from letter on, 'A' or 'a'. *) |
| 50 | 88 | proc digits(u: U64; base: U64; letter: U8): ([24]U8, INT); |
| | @@ -127,8 +165,230 @@ |
| 127 | 165 | put(k, "E"); |
| 128 | 166 | put(k, d[at..24]); |
| 129 | 167 | end; |
| 168 | +end; |
| 169 | + |
| 170 | +(* ---- short: the fewest digits that read back ---- |
| 171 | + |
| 172 | + An F64 x is m times 2^k, m and k whole numbers. Every number between |
| 173 | + the halves to the F64s on either side of x reads back as x, so short |
| 174 | + looks for the fewest digits that land between those halves, lo and |
| 175 | + hi: |
| 176 | + |
| 177 | + below lo x hi above the F64s around x |
| 178 | + |--------|------*------|--------| |
| 179 | + |
| 180 | + x, lo and hi are written out exactly in decimal, each an Exact, and |
| 181 | + compared digit by digit with each try, so nothing is rounded on the |
| 182 | + way. For p digits the tries are x cut to p digits and that one more |
| 183 | + in its last place: the two numbers of p digits nearest x. The first |
| 184 | + p for which one of them lies between lo and hi gives the answer; if |
| 185 | + both do, the nearer x is taken. 17 digits are always enough. |
| 186 | + |
| 187 | + An exact value is made in a Big, a whole number in base 10^9: for k |
| 188 | + at least 0, m times 2^k is m doubled k times; for k below 0, it is m |
| 189 | + times 5^-k with the point -k places from the right, since 2^-k is |
| 190 | + 5^-k / 10^-k. *) |
| 191 | + |
| 192 | +(* The bits of x. *) |
| 193 | +proc bits(x: F64): U64; |
| 194 | +var p: ^U64; |
| 195 | +begin |
| 196 | + p := ^U64(ADR(x)); |
| 197 | + return p^; |
| 130 | 198 | end; |
| 131 | 199 | |
| 200 | +(* b times f, which is at most 2^31. *) |
| 201 | +proc bigMul(var b: Big; f: U64); |
| 202 | +var i: INT; t, carry: U64; |
| 203 | +begin |
| 204 | + carry := 0; |
| 205 | + for i := 0 to b.n - 1 do |
| 206 | + t := b.v[i] * f + carry; |
| 207 | + carry := t div Limb; |
| 208 | + b.v[i] := t - carry * Limb; |
| 209 | + end; |
| 210 | + while carry > 0 do |
| 211 | + b.v[b.n] := carry mod Limb; |
| 212 | + carry := carry div Limb; |
| 213 | + INC(b.n); |
| 214 | + end; |
| 215 | +end; |
| 216 | + |
| 217 | +(* b times 2^k, or 5^k if five, a few steps at once. *) |
| 218 | +proc bigPow(var b: Big; k: INT; five: BOOL); |
| 219 | +var step, i: INT; f: U64; |
| 220 | +begin |
| 221 | + while k > 0 do |
| 222 | + if five then step := FiveStep; else step := TwoStep; end; |
| 223 | + if k < step then step := k; end; |
| 224 | + f := 1; |
| 225 | + for i := 1 to step do |
| 226 | + if five then f := f * 5; else f := f * 2; end; |
| 227 | + end; |
| 228 | + bigMul(b, f); |
| 229 | + DEC(k, step); |
| 230 | + end; |
| 231 | +end; |
| 232 | + |
| 233 | +(* m times 2^k, m not 0, exactly in decimal, into w. *) |
| 234 | +proc expand(m: U64; k: INT; var w: Exact); |
| 235 | +var b: Big; d: [24]U8; at, i, j, point: INT; v: U64; |
| 236 | +begin |
| 237 | + b.n := 0; |
| 238 | + while m > 0 do |
| 239 | + b.v[b.n] := m mod Limb; |
| 240 | + m := m div Limb; |
| 241 | + INC(b.n); |
| 242 | + end; |
| 243 | + point := 0; |
| 244 | + if k < 0 then point := -k; bigPow(b, -k, true); else bigPow(b, k, false); end; |
| 245 | + (* the top limb without leading zeros, then nine digits a limb *) |
| 246 | + d, at := digits(b.v[b.n - 1], 10, 'A'); |
| 247 | + w.n := 24 - at; |
| 248 | + COPY(w.d[0..w.n], d[at..24]); |
| 249 | + for i := b.n - 2 to 0 by -1 do |
| 250 | + v := b.v[i]; |
| 251 | + for j := LimbDigits - 1 to 0 by -1 do |
| 252 | + w.d[w.n + j] := U8(v mod 10) + '0'; |
| 253 | + v := v div 10; |
| 254 | + end; |
| 255 | + INC(w.n, LimbDigits); |
| 256 | + end; |
| 257 | + w.e := w.n - point; |
| 258 | +end; |
| 259 | + |
| 260 | +(* -1, 0 or 1 as 0.d times 10^e, d's first digit not 0, is below, at or |
| 261 | + above w. *) |
| 262 | +proc compare(d: []U8; e: INT; const w: Exact): INT; |
| 263 | +var i, n: INT; a, c: U8; |
| 264 | +begin |
| 265 | + if e # w.e then |
| 266 | + if e < w.e then return -1; end; |
| 267 | + return 1; |
| 268 | + end; |
| 269 | + n := w.n; |
| 270 | + if LEN(d) > n then n := LEN(d); end; |
| 271 | + for i := 0 to n - 1 do |
| 272 | + a := '0'; c := '0'; (* past its end each has zeros *) |
| 273 | + if i < LEN(d) then a := d[i]; end; |
| 274 | + if i < w.n then c := w.d[i]; end; |
| 275 | + if a # c then |
| 276 | + if a < c then return -1; end; |
| 277 | + return 1; |
| 278 | + end; |
| 279 | + end; |
| 280 | + return 0; |
| 281 | +end; |
| 282 | + |
| 283 | +(* Whether s reads back as the F64 that lo and hi are the halves around: |
| 284 | + it lies between them, or on one of them if ends, as the F64 whose m |
| 285 | + is even takes a tie. *) |
| 286 | +proc between(const s: Short; const lo, hi: Exact; ends: BOOL): BOOL; |
| 287 | +var below, above: INT; |
| 288 | +begin |
| 289 | + below := compare(s.d[0..s.n], s.e, lo); |
| 290 | + above := compare(s.d[0..s.n], s.e, hi); |
| 291 | + return ((below > 0) or (below = 0) and ends) and ((above < 0) or (above = 0) and ends); |
| 292 | +end; |
| 293 | + |
| 294 | +(* s one more in its last place, carrying, without the zeros that leaves |
| 295 | + at the end: 129 becomes 13, and 99 becomes 1 with the point moved. *) |
| 296 | +proc up(var s: Short); |
| 297 | +var i: INT; |
| 298 | +begin |
| 299 | + i := s.n - 1; |
| 300 | + while (i >= 0) and (s.d[i] = '9') do DEC(i); end; |
| 301 | + if i >= 0 then |
| 302 | + INC(s.d[i]); |
| 303 | + s.n := i + 1; |
| 304 | + else |
| 305 | + s.d[0] := '1'; |
| 306 | + s.n := 1; |
| 307 | + INC(s.e); |
| 308 | + end; |
| 309 | +end; |
| 310 | + |
| 311 | +(* Whether x, cut where rest begins, is nearer the number one more in |
| 312 | + the last place than to the cut one: rest is above a half, or a half |
| 313 | + and last, the cut one's last digit, odd. *) |
| 314 | +proc nearerUp(rest: []U8; last: U8): BOOL; |
| 315 | +var i: INT; |
| 316 | +begin |
| 317 | + if rest[0] # '5' then return rest[0] > '5'; end; |
| 318 | + for i := 1 to LEN(rest) - 1 do |
| 319 | + if rest[i] # '0' then return true; end; |
| 320 | + end; |
| 321 | + return (last - '0') & 1 = 1; |
| 322 | +end; |
| 323 | + |
| 324 | +(* The fewest digits that read back as x, finite and above 0, into s. *) |
| 325 | +proc shortest(x: F64; var s: Short); |
| 326 | +var u, m: U64; k, p: INT; mid, lo, hi: Exact; ends, downOk, upOk: BOOL; next: Short; |
| 327 | +begin |
| 328 | + (* x as m times 2^k: the fraction with its hidden 1 but for the |
| 329 | + smallest exponent, the F64s below 2^-1022 *) |
| 330 | + u := bits(x); |
| 331 | + m := u & (U64(1) shl MantBits - 1); |
| 332 | + k := INT(u shr MantBits & ExpMask); |
| 333 | + if k = 0 then k := 1; else m := m | U64(1) shl MantBits; end; |
| 334 | + DEC(k, ExpBias); |
| 335 | + (* the halves to the F64s around x; the one below a power of two is |
| 336 | + half as far as the one above, but at the smallest exponent *) |
| 337 | + expand(m, k, mid); |
| 338 | + expand(2 * m + 1, k - 1, hi); |
| 339 | + if (m = U64(1) shl MantBits) and (k > 1 - ExpBias) then |
| 340 | + expand(4 * m - 1, k - 2, lo); |
| 341 | + else |
| 342 | + expand(2 * m - 1, k - 1, lo); |
| 343 | + end; |
| 344 | + ends := m & 1 = 0; |
| 345 | + for p := 1 to MostDigits do |
| 346 | + s.n := p; |
| 347 | + if p >= mid.n then s.n := mid.n; end; (* x itself has no more digits *) |
| 348 | + s.e := mid.e; |
| 349 | + COPY(s.d[0..s.n], mid.d[0..s.n]); |
| 350 | + if s.n = mid.n then return; end; |
| 351 | + next := s; |
| 352 | + up(next); |
| 353 | + downOk := between(s, lo, hi, ends); |
| 354 | + upOk := between(next, lo, hi, ends); |
| 355 | + if upOk and (not downOk or nearerUp(mid.d[p..mid.n], s.d[p - 1])) then |
| 356 | + s := next; |
| 357 | + return; |
| 358 | + elsif downOk then |
| 359 | + return; |
| 360 | + end; |
| 361 | + end; |
| 362 | +end; |
| 363 | + |
| 364 | +(* x as Arg's short says, but for the minus sign, which arg writes. *) |
| 365 | +proc putShort(var k: Sink; x: F64); |
| 366 | +var s: Short; d: [24]U8; at, e: INT; |
| 367 | +begin |
| 368 | + if x # x then put(k, "nan"); return; end; |
| 369 | + if x < 0.0 then x := -x; end; |
| 370 | + if x > 1.7976931348623157E308 then put(k, "inf"); return; end; |
| 371 | + if x = 0.0 then put(k, "0"); return; end; (* -0.0 too *) |
| 372 | + shortest(x, s); |
| 373 | + if (s.e >= -3) and (s.e <= 15) then (* from 1E-4 to below 1E15 *) |
| 374 | + if s.e <= 0 then |
| 375 | + put(k, "0."); fill(k, '0', -s.e); put(k, s.d[0..s.n]); |
| 376 | + elsif s.e >= s.n then |
| 377 | + put(k, s.d[0..s.n]); fill(k, '0', s.e - s.n); |
| 378 | + else |
| 379 | + put(k, s.d[0..s.e]); put(k, "."); put(k, s.d[s.e..s.n]); |
| 380 | + end; |
| 381 | + else |
| 382 | + put(k, s.d[0..1]); |
| 383 | + if s.n > 1 then put(k, "."); put(k, s.d[1..s.n]); end; |
| 384 | + put(k, "E"); |
| 385 | + e := s.e - 1; |
| 386 | + if e < 0 then put(k, "-"); e := -e; end; |
| 387 | + d, at := digits(U64(e), 10, 'A'); |
| 388 | + put(k, d[at..24]); |
| 389 | + end; |
| 390 | +end; |
| 391 | + |
| 132 | 392 | (* Whether a is printed with a minus sign. *) |
| 133 | 393 | proc negative(a: Arg): BOOL; |
| 134 | 394 | var neg: BOOL; |
| | @@ -136,6 +396,7 @@ |
| 136 | 396 | match a of |
| 137 | 397 | | int(i): neg := i < 0; |
| 138 | 398 | | real(r, _places): neg := r < 0.0; |
| 399 | + | short(r): neg := (r = r) and (bits(r) shr 63 = 1); (* -0.0 too, but no nan *) |
| 139 | 400 | | else: neg := false; |
| 140 | 401 | end; |
| 141 | 402 | return neg; |
| | @@ -160,6 +421,7 @@ |
| 160 | 421 | | char(c): one[0] := c; put(k, one); |
| 161 | 422 | | bool(t): if t then put(k, "true"); else put(k, "false"); end; |
| 162 | 423 | | real(r, places): putReal(k, r, places); |
| 424 | + | short(r): putShort(k, r); |
| 163 | 425 | end; |
| 164 | 426 | end; |
| 165 | 427 | |
| | @@ -317,6 +579,20 @@ |
| 317 | 579 | Buf.reset(b); |
| 318 | 580 | write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]); |
| 319 | 581 | if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end; |
| 582 | + Buf.reset(b); |
| 583 | + write(b, "{} {} {} {} {} {} {} {}", [short(0.1), short(1.0 / 3.0), short(-2.5), short(100.0), short(42.0E3), |
| 584 | + short(0.0001), short(0.00012), short(123456789012345.0)]); |
| 585 | + if Buf.bytes(b) # "0.1 0.3333333333333333 -2.5 100 42000 0.0001 0.00012 123456789012345" then Rt.stop("Fmt.short"); end; |
| 586 | + Buf.reset(b); |
| 587 | + write(b, "{} {} {} {} {} {} {}", [short(1.0E15), short(1.5E17), short(9.9E-5), short(5.0E-324), |
| 588 | + short(1.7976931348623157E308), short(2.2250738585072014E-308), short(9007199254740992.0)]); |
| 589 | + if Buf.bytes(b) # "1E15 1.5E17 9.9E-5 5E-324 1.7976931348623157E308 2.2250738585072014E-308 9.007199254740992E15" then |
| 590 | + Rt.stop("Fmt.short: an exponent"); |
| 591 | + end; |
| 592 | + Buf.reset(b); |
| 593 | + write(b, "{} {} {} {} {} {6}|{-6}|{06}", [short(zero), short(-zero), short(1.0 / zero), short(-1.0 / zero), |
| 594 | + short(zero / zero), short(0.5), short(0.5), short(-0.5)]); |
| 595 | + 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; |
| 320 | 596 | n, whole := into(small, "{}:{02}", [int(-3), int(7)]); |
| 321 | 597 | if (small[0..n] # "-3:07") or not whole then Rt.stop("Fmt.into"); end; |
| 322 | 598 | n, whole := into(small, "line {}, {}", [int(12), str("x")]); |