diff options
| author | gingerBill <bill@gingerbill.org> | 2021-09-29 16:39:02 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-09-29 16:39:02 +0100 |
| commit | c6ff88e85afc9e13dd71a32d5ab28551cfd284e3 (patch) | |
| tree | 88abd5d7900467f4d3cf2465d17bea3cf5d9b039 /core/io | |
| parent | f99bea12c7fc0138547785a70d5c2f2878b18922 (diff) | |
Add `io.write_f16` `io.write_f32` `io.write_f64`
Diffstat (limited to 'core/io')
| -rw-r--r-- | core/io/util.odin | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/core/io/util.odin b/core/io/util.odin index ff033e3df..538e1a077 100644 --- a/core/io/util.odin +++ b/core/io/util.odin @@ -47,6 +47,54 @@ write_i128 :: proc(w: Writer, i: i128, base: int = 10, n_written: ^int = nil) -> s := strconv.append_bits_128(buf[:], u128(i), base, true, 128, strconv.digits, nil) return write_string(w, s, n_written) } +write_f16 :: proc(w: Writer, val: f16, n_written: ^int = nil) -> (n: int, err: Error) { + buf: [386]byte + + str := strconv.append_float(buf[1:], f64(val), 'f', 2*size_of(val), 8*size_of(val)) + s := buf[:len(str)+1] + if s[1] == '+' || s[1] == '-' { + s = s[1:] + } else { + s[0] = '+' + } + if s[0] == '+' { + s = s[1:] + } + + return write_string(w, string(s), n_written) +} +write_f32 :: proc(w: Writer, val: f32, n_written: ^int = nil) -> (n: int, err: Error) { + buf: [386]byte + + str := strconv.append_float(buf[1:], f64(val), 'f', 2*size_of(val), 8*size_of(val)) + s := buf[:len(str)+1] + if s[1] == '+' || s[1] == '-' { + s = s[1:] + } else { + s[0] = '+' + } + if s[0] == '+' { + s = s[1:] + } + + return write_string(w, string(s), n_written) +} +write_f64 :: proc(w: Writer, val: f64, n_written: ^int = nil) -> (n: int, err: Error) { + buf: [386]byte + + str := strconv.append_float(buf[1:], val, 'f', 2*size_of(val), 8*size_of(val)) + s := buf[:len(str)+1] + if s[1] == '+' || s[1] == '-' { + s = s[1:] + } else { + s[0] = '+' + } + if s[0] == '+' { + s = s[1:] + } + + return write_string(w, string(s), n_written) +} |