lib/strconv: add f64tos
This commit is contained in:
@@ -105,3 +105,84 @@ export fn stou64(s: str) (u64 | invalid | overflow) = {
|
||||
};
|
||||
return v;
|
||||
};
|
||||
|
||||
// f64tos — write `v` in decimal into `buf` and return the byte count.
|
||||
// Hare name; this is the buffer-in Plan 9 subset of Hare's
|
||||
// `f64tos(n) const str`. Today's surface:
|
||||
//
|
||||
// - finite values only. NaN/±Inf detection needs an f64→u64 bit
|
||||
// reinterpret cast that the cgen doesn't expose yet.
|
||||
// - fixed-point only, up to 6 fractional digits. Trailing zeros
|
||||
// after the decimal point are trimmed. Trailing '.' is dropped.
|
||||
// - magnitudes ≥ 9e18 (overflows i64 in the integer-part cast)
|
||||
// fall back to the literal token "huge". Hare would print these
|
||||
// in scientific notation via Ryū; we will graduate when the
|
||||
// compiler grows the bit-reinterpret cast.
|
||||
//
|
||||
// Round-trip is therefore lossy past 6 fractional digits; callers
|
||||
// that need bit-exact recovery should not use this until the
|
||||
// graduate-to-Ryū step lands. `f64tos(buf, 1.0)` writes "1" (no
|
||||
// decimal point), `f64tos(buf, 1.5)` writes "1.5", `f64tos(buf,
|
||||
// 0.1)` writes "0.1".
|
||||
//
|
||||
// No float literals in the body — 990's wwdump diff requires this
|
||||
// file's TK_FLOAT count to match between C and ww front-ends, and
|
||||
// the ww-side wwdump currently skips TK_FLOAT.fval while the C side
|
||||
// %g-formats it. Same trick lib/ww/lex/lex.ww's parsef64 uses:
|
||||
// build f64 constants via int-to-f64 casts.
|
||||
export fn f64tos(buf: []u8, v: f64) i32 = {
|
||||
let out: i32 = 0;
|
||||
let f: f64 = v;
|
||||
let zero: f64 = 0: f64;
|
||||
if (f < zero) {
|
||||
buf[out] = 45u8; // '-'
|
||||
out += 1;
|
||||
f = -f;
|
||||
};
|
||||
// 9e18 is comfortably under I64_MAX (9.22e18). Past this the
|
||||
// `f: i64` cast wraps and the integer part comes back as garbage.
|
||||
let cap: f64 = 9000000000000000000i64: f64;
|
||||
if (f >= cap) {
|
||||
let s: str = "huge";
|
||||
let k: i32 = 0;
|
||||
for (k < s.len) { buf[out] = s[k]; out += 1; k += 1; };
|
||||
return out;
|
||||
};
|
||||
let ip: i64 = f: i64;
|
||||
// Fractional part scaled to 6 decimal digits, with round-to-
|
||||
// nearest via +0.5. (f64 compound assigns mis-lower in cgen —
|
||||
// use the explicit form, as the rest of lib does.)
|
||||
let frac: f64 = f - (ip: f64);
|
||||
let scale: f64 = 1000000: f64;
|
||||
frac = frac * scale;
|
||||
let half: f64 = (1: f64) / (2: f64);
|
||||
let fp: i64 = (frac + half): i64;
|
||||
// Carry: e.g. 0.9999996 rounds fp up to 1000000 and the integer
|
||||
// part needs to advance.
|
||||
if (fp >= 1000000) {
|
||||
ip += 1;
|
||||
fp = 0;
|
||||
};
|
||||
let itmp: [32]u8;
|
||||
let in: i32 = i64tos(itmp[0:32], ip);
|
||||
let k: i32 = 0;
|
||||
for (k < in) { buf[out] = itmp[k]; out += 1; k += 1; };
|
||||
if (fp == 0) { return out; };
|
||||
buf[out] = 46u8; // '.'
|
||||
out += 1;
|
||||
let ftmp: [16]u8;
|
||||
let m: i32 = u64tos(ftmp[0:16], fp: u64);
|
||||
// Pad fractional to 6 digits with leading zeros (e.g. 0.05 →
|
||||
// fp=50000, m=5, pad one '0' before "50000").
|
||||
let z: i32 = 6 - m;
|
||||
for (z > 0) { buf[out] = 48u8; out += 1; z -= 1; };
|
||||
k = 0;
|
||||
for (k < m) { buf[out] = ftmp[k]; out += 1; k += 1; };
|
||||
// Trim trailing zeros in the fractional part (we know fp != 0,
|
||||
// so the loop stops before erasing the dot).
|
||||
for (out > 0) {
|
||||
if (buf[out - 1] != 48u8) { break; };
|
||||
out -= 1;
|
||||
};
|
||||
return out;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user