raz / test / run / FmtShort.raz

103 lines · 3.2 KiB · raw · history · last change a700688d

1(* Fmt.short against Math.toReal: what it writes reads back as the
2 same F64, and one digit fewer does not. Tried on powers of ten, a
3 few numbers known to be hard, and random bit patterns. *)
4module FmtShort;
5import Fmt, Math, Buf, Heap;
6
7var
8 text, fewer: Buf.Buf;
9 digitsOut: [Fmt.ShortMost]U8;
10 tried, bad: INT;
11
12(* What toReal reads s as; a nan if it reads nothing. *)
13proc read(s: []U8): F64;
14var x: F64; ok: BOOL;
15begin
16 x, ok := Math.toReal(s);
17 if not ok then x := Math.fromBits(7FF8000000000000H); end;
18 return x;
19end;
20
21(* Whether y is x, -0.0 not being 0.0. *)
22proc same(x, y: F64): BOOL;
23begin
24 return Math.bits(x) = Math.bits(y);
25end;
26
27(* The significant digits of s, as short writes it, as a whole number,
28 and the power of ten it is to be taken to: 1.5E17 is 15 and 16. *)
29proc parse(s: []U8): (U64, INT);
30var d: U64; e, i, ex: INT; point, neg: BOOL;
31begin
32 while (i < LEN(s)) and (s[i] # 'E') do
33 if s[i] = '.' then
34 point := true;
35 elsif s[i] # '-' then
36 d := d * 10 + U64(s[i] - '0');
37 if point then DEC(e); end;
38 end;
39 INC(i);
40 end;
41 if i < LEN(s) then
42 INC(i);
43 if s[i] = '-' then neg := true; INC(i); end;
44 while i < LEN(s) do ex := ex * 10 + INT(s[i] - '0'); INC(i); end;
45 if neg then ex := -ex; end;
46 end;
47 while (d > 0) and (d mod 10 = 0) do d := d div 10; INC(e); end;
48 return d, e + ex;
49end;
50
51(* Whether d times 10^e reads back as x. *)
52proc readsBack(d: U64; e: INT; x: F64): BOOL;
53begin
54 Buf.reset(fewer);
55 Fmt.write(fewer, "{}E{}", [Fmt.uint(d), Fmt.int(e)]);
56 return same(read(Buf.bytes(fewer)), x);
57end;
58
59(* x, finite and not 0, tried. *)
60proc check(x: F64);
61var d: U64; e: INT;
62begin
63 INC(tried);
64 Buf.reset(text);
65 Fmt.write(text, "{}", [Fmt.str(Fmt.short(x, digitsOut))]);
66 if not same(read(Buf.bytes(text)), x) then
67 INC(bad);
68 Fmt.print("{} reads back as another F64\n", [Fmt.str(Buf.bytes(text))]);
69 return;
70 end;
71 (* one digit fewer: the two numbers of that many digits around x *)
72 if x < 0.0 then x := -x; end;
73 d, e := parse(Buf.bytes(text));
74 if (d >= 10) and (readsBack(d div 10, e + 1, x) or readsBack(d div 10 + 1, e + 1, x)) then
75 INC(bad);
76 Fmt.print("{} reads back in fewer digits\n", [Fmt.str(Buf.bytes(text))]);
77 end;
78end;
79
80proc main*;
81var x: F64; u: U64; i: INT;
82begin
83 Buf.init(text, Heap.heap);
84 Buf.init(fewer, Heap.heap);
85 x := 1.0E-323;
86 while x < 1.0E308 do check(x); check(-x); x := x * 10.0; end;
87 x := 1.0;
88 for i := 0 to 1073 do x := x / 2.0; end;
89 check(x); (* the least F64, 5E-324 *)
90 check(0.1); check(0.2); check(0.3); check(1.0 / 3.0); check(2.0 / 3.0);
91 check(1.7976931348623157E308); check(2.2250738585072014E-308); check(2.2250738585072009E-308);
92 check(9007199254740992.0); check(9007199254740993.0); check(5.0E-324); check(123456789.0);
93 (* random bit patterns, nan and inf left out *)
94 u := 12345;
95 for i := 1 to 20000 do
96 u := u * 6364136223846793005 + 1442695040888963407;
97 x := Math.fromBits(u);
98 if (x = x) and (x - x = 0.0) and (x # 0.0) then check(x); end;
99 end;
100 Fmt.print("{} values tried, {} bad\n", [Fmt.int(tried), Fmt.int(bad)]);
101end;
102
103end FmtShort.