| | @@ -21,17 +21,13 @@ |
| 21 | 21 | TwoStep = 29; (* doublings at once: 10^9 * 2^29 < 2^63 *) |
| 22 | 22 | FiveStep = 13; (* fives multiplied at once: 10^9 * 5^13 < 2^63 *) |
| 23 | 23 | |
| 24 | +const ShortMost* = 24; (* the longest short: -2.2250738585072014E-308 *) |
| 25 | + |
| 24 | 26 | type |
| 25 | 27 | (* An argument, and how it is printed. A real has places digits |
| 26 | 28 | after the point, at most 9, rounded to the nearest, half away |
| 27 | 29 | from zero; from 1E15 on, it has the exponent that makes it less |
| 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. *) |
| 30 | + than 10, as 1.50E17; nan and inf are spelled so. *) |
| 35 | 31 | Arg* = variant |
| 36 | 32 | int(i: I64); (* in decimal *) |
| 37 | 33 | uint(u: U64); (* in decimal *) |
| | @@ -41,7 +37,6 @@ |
| 41 | 37 | char(c: U8); (* the one byte *) |
| 42 | 38 | bool(b: BOOL); (* true or false *) |
| 43 | 39 | real(r: F64; places: INT); |
| 44 | | - short(r: F64); |
| 45 | 40 | end; |
| 46 | 41 | |
| 47 | 42 | (* Where the pieces go: to out if slice is set, else to b, or to |
| | @@ -361,32 +356,52 @@ |
| 361 | 356 | end; |
| 362 | 357 | end; |
| 363 | 358 | |
| 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; |
| 359 | +(* s added to out at n, which moves past it. *) |
| 360 | +proc emit(var out: [ShortMost]U8; var n: INT; s: []U8); |
| 361 | +begin |
| 362 | + COPY(out[n..n + LEN(s)], s); |
| 363 | + INC(n, LEN(s)); |
| 364 | +end; |
| 365 | + |
| 366 | +(* x in the fewest digits Math.toReal reads back as x, the nearer of |
| 367 | + two such, and of two as near the one whose last digit is even, |
| 368 | + written into out from 0: those bytes. From 1E-4 to below 1E15 it is |
| 369 | + plain, as 0.0001, 0.1, 42 or 123456789012345; else a digit, a point |
| 370 | + and the rest, an E and the exponent, as 1.5E17 or 5E-324. Zero is 0 |
| 371 | + or -0, keeping its sign so that it too reads back; nan, inf and |
| 372 | + -inf are spelled so. The longest is ShortMost, 24 bytes, as |
| 373 | + -2.2250738585072014E-308: a sign, 17 digits, a point and E-308; the |
| 374 | + longest plain one, -0.00012345678901234567, has 22. *) |
| 375 | +proc short*(x: F64; var out: [ShortMost]U8): []U8; |
| 376 | +var s: Short; d: [24]U8; n, at, e, z: INT; |
| 367 | 377 | 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 *) |
| 378 | + n := 0; |
| 379 | + if x # x then emit(out, n, "nan"); return out[0..n]; end; |
| 380 | + if bits(x) shr 63 = 1 then emit(out, n, "-"); x := -x; end; (* -0.0 too *) |
| 381 | + if x > 1.7976931348623157E308 then emit(out, n, "inf"); return out[0..n]; end; |
| 382 | + if x = 0.0 then emit(out, n, "0"); return out[0..n]; end; |
| 372 | 383 | shortest(x, s); |
| 373 | 384 | if (s.e >= -3) and (s.e <= 15) then (* from 1E-4 to below 1E15 *) |
| 374 | 385 | if s.e <= 0 then |
| 375 | | - put(k, "0."); fill(k, '0', -s.e); put(k, s.d[0..s.n]); |
| 386 | + emit(out, n, "0."); |
| 387 | + for z := 1 to -s.e do emit(out, n, "0"); end; |
| 388 | + emit(out, n, s.d[0..s.n]); |
| 376 | 389 | elsif s.e >= s.n then |
| 377 | | - put(k, s.d[0..s.n]); fill(k, '0', s.e - s.n); |
| 390 | + emit(out, n, s.d[0..s.n]); |
| 391 | + for z := 1 to s.e - s.n do emit(out, n, "0"); end; |
| 378 | 392 | else |
| 379 | | - put(k, s.d[0..s.e]); put(k, "."); put(k, s.d[s.e..s.n]); |
| 393 | + emit(out, n, s.d[0..s.e]); emit(out, n, "."); emit(out, n, s.d[s.e..s.n]); |
| 380 | 394 | end; |
| 381 | 395 | 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"); |
| 396 | + emit(out, n, s.d[0..1]); |
| 397 | + if s.n > 1 then emit(out, n, "."); emit(out, n, s.d[1..s.n]); end; |
| 398 | + emit(out, n, "E"); |
| 385 | 399 | e := s.e - 1; |
| 386 | | - if e < 0 then put(k, "-"); e := -e; end; |
| 400 | + if e < 0 then emit(out, n, "-"); e := -e; end; |
| 387 | 401 | d, at := digits(U64(e), 10, 'A'); |
| 388 | | - put(k, d[at..24]); |
| 402 | + emit(out, n, d[at..24]); |
| 389 | 403 | end; |
| 404 | + return out[0..n]; |
| 390 | 405 | end; |
| 391 | 406 | |
| 392 | 407 | (* Whether a is printed with a minus sign. *) |
| | @@ -396,7 +411,6 @@ |
| 396 | 411 | match a of |
| 397 | 412 | | int(i): neg := i < 0; |
| 398 | 413 | | 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 *) |
| 400 | 414 | | else: neg := false; |
| 401 | 415 | end; |
| 402 | 416 | return neg; |
| | @@ -421,7 +435,6 @@ |
| 421 | 435 | | char(c): one[0] := c; put(k, one); |
| 422 | 436 | | bool(t): if t then put(k, "true"); else put(k, "false"); end; |
| 423 | 437 | | real(r, places): putReal(k, r, places); |
| 424 | | - | short(r): putShort(k, r); |
| 425 | 438 | end; |
| 426 | 439 | end; |
| 427 | 440 | |
| | @@ -550,9 +563,19 @@ |
| 550 | 563 | return k.n, true; |
| 551 | 564 | end; |
| 552 | 565 | |
| 566 | +(* Each of xs, shorts, with a space between, added to b. *) |
| 567 | +proc shorts(var b: Buf.Buf; xs: []F64); |
| 568 | +var d: [ShortMost]U8; i: INT; |
| 569 | +begin |
| 570 | + for i := 0 to LEN(xs) - 1 do |
| 571 | + if i > 0 then Buf.add(b, ' '); end; |
| 572 | + Buf.str(b, short(xs[i], d)); |
| 573 | + end; |
| 574 | +end; |
| 575 | + |
| 553 | 576 | (* The module's test: formats into a buffer over an arena. *) |
| 554 | 577 | proc main*; |
| 555 | | -var ar: Arena.Arena; b: Buf.Buf; zero: F64; small: [8]U8; n: INT; whole: BOOL; |
| 578 | +var ar: Arena.Arena; b: Buf.Buf; zero: F64; small: [8]U8; d: [ShortMost]U8; n: INT; whole: BOOL; |
| 556 | 579 | begin |
| 557 | 580 | if not Arena.init(ar, 1 shl 16) then Rt.stop("Fmt: no arena"); end; |
| 558 | 581 | Buf.init(b, ADR(ar.base)); |
| | @@ -580,19 +603,19 @@ |
| 580 | 603 | write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]); |
| 581 | 604 | if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end; |
| 582 | 605 | 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)]); |
| 606 | + shorts(b, [0.1, 1.0 / 3.0, -2.5, 100.0, 42.0E3, 0.0001, 0.00012, 123456789012345.0]); |
| 585 | 607 | if Buf.bytes(b) # "0.1 0.3333333333333333 -2.5 100 42000 0.0001 0.00012 123456789012345" then Rt.stop("Fmt.short"); end; |
| 586 | 608 | 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)]); |
| 609 | + shorts(b, [1.0E15, 1.5E17, 9.9E-5, 5.0E-324, 1.7976931348623157E308, 2.2250738585072014E-308, |
| 610 | + 9007199254740992.0]); |
| 589 | 611 | if Buf.bytes(b) # "1E15 1.5E17 9.9E-5 5E-324 1.7976931348623157E308 2.2250738585072014E-308 9.007199254740992E15" then |
| 590 | 612 | Rt.stop("Fmt.short: an exponent"); |
| 591 | 613 | end; |
| 592 | 614 | 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; |
| 615 | + shorts(b, [zero, -zero, 1.0 / zero, -1.0 / zero, zero / zero]); |
| 616 | + write(b, " {6}|{-6}", [str(short(0.5, d)), str(short(0.5, d))]); |
| 617 | + if Buf.bytes(b) # "0 -0 inf -inf nan 0.5|0.5 " then Rt.stop("Fmt.short: zero, inf, nan and a width"); end; |
| 618 | + if LEN(short(-2.2250738585072014E-308, d)) # ShortMost then Rt.stop("Fmt.short: the longest"); end; |
| 596 | 619 | n, whole := into(small, "{}:{02}", [int(-3), int(7)]); |
| 597 | 620 | if (small[0..n] # "-3:07") or not whole then Rt.stop("Fmt.into"); end; |
| 598 | 621 | n, whole := into(small, "line {}, {}", [int(12), str("x")]); |