aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2023-12-21 17:05:24 +0000
committergingerBill <bill@gingerbill.org>2023-12-21 17:05:24 +0000
commit55f3e99f6330eb3961b35217823cdcc45b9eceb5 (patch)
treed9a3d7f633a8fdeaad92617e118e4f015f1dfa90 /core
parent509712f771a766959833bbcf8b1dd91bf3582ea2 (diff)
Fix `%g` in `fmt`, and make `%v` default to `%g` for floats
Diffstat (limited to 'core')
-rw-r--r--core/fmt/fmt.odin18
-rw-r--r--core/strconv/generic_float.odin9
2 files changed, 19 insertions, 8 deletions
diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin
index 402f783cf..ef0a4638c 100644
--- a/core/fmt/fmt.odin
+++ b/core/fmt/fmt.odin
@@ -1232,8 +1232,12 @@ _pad :: proc(fi: ^Info, s: string) {
//
// NOTE: Can return "NaN", "+Inf", "-Inf", "+<value>", or "-<value>".
//
-_fmt_float_as :: proc(fi: ^Info, v: f64, bit_size: int, verb: rune, float_fmt: byte) {
- prec := fi.prec if fi.prec_set else 3
+_fmt_float_as :: proc(fi: ^Info, v: f64, bit_size: int, verb: rune, float_fmt: byte, prec: int) {
+ prec := prec
+ if fi.prec_set {
+ prec = fi.prec
+ }
+
buf: [386]byte
// Can return "NaN", "+Inf", "-Inf", "+<value>", "-<value>".
@@ -1242,7 +1246,7 @@ _fmt_float_as :: proc(fi: ^Info, v: f64, bit_size: int, verb: rune, float_fmt: b
if !fi.plus {
// Strip sign from "+<value>" but not "+Inf".
if str[0] == '+' && str[1] != 'I' {
- str = str[1:]
+ str = str[1:]
}
}
@@ -1258,11 +1262,13 @@ _fmt_float_as :: proc(fi: ^Info, v: f64, bit_size: int, verb: rune, float_fmt: b
//
fmt_float :: proc(fi: ^Info, v: f64, bit_size: int, verb: rune) {
switch verb {
- case 'f', 'F', 'g', 'G', 'v':
- _fmt_float_as(fi, v, bit_size, verb, 'f')
+ case 'g', 'G', 'v':
+ _fmt_float_as(fi, v, bit_size, verb, 'g', -1)
+ case 'f', 'F':
+ _fmt_float_as(fi, v, bit_size, verb, 'f', 3)
case 'e', 'E':
// BUG(): "%.3e" returns "3.000e+00"
- _fmt_float_as(fi, v, bit_size, verb, 'e')
+ _fmt_float_as(fi, v, bit_size, verb, 'e', 6)
case 'h', 'H':
prev_fi := fi^
diff --git a/core/strconv/generic_float.odin b/core/strconv/generic_float.odin
index 70febf832..bbbf98523 100644
--- a/core/strconv/generic_float.odin
+++ b/core/strconv/generic_float.odin
@@ -2,6 +2,8 @@ package strconv
import "decimal"
+import "core:runtime"
+
Decimal_Slice :: struct {
digits: []byte,
count: int,
@@ -103,8 +105,11 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, precision, bit_size: int)
}
} else {
switch fmt {
- case 'e', 'E': decimal.round(d, prec+1)
- case 'f', 'F': decimal.round(d, d.decimal_point+prec)
+ case 'e', 'E':
+ prec += 1
+ decimal.round(d, prec)
+ case 'f', 'F':
+ decimal.round(d, d.decimal_point+prec)
case 'g', 'G':
if prec == 0 {
prec = 1