From 2d7786bfc25494bce1651352719f30b78d73e0a5 Mon Sep 17 00:00:00 2001 From: bardrop Date: Fri, 20 Nov 2026 07:00:20 +0530 Subject: [PATCH] fix: round decimals half to even on their decimal value marshalDecimal rounded d/0.001 in binary, so a tie like 0.5015 (stored as 0.50149999...) went down and 2.0005 (stored as 2.00050000...) went up. RFC 9651 section 4.1.5 rounds the decimal number, and the official test suite now has cases for this. Round the shortest decimal form of the float instead. Also fail on NaN and infinities (NaN was written out as "NaN") and drop the sign when a negative value rounds to zero. Bump structured-field-tests to 10781e4 (same as #27), which has the new tie-to-even cases. --- decimal.go | 47 +++++++++++++++++++++++++++++++++++------- decimal_test.go | 13 ++++++++++++ structured-field-tests | 2 +- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/decimal.go b/decimal.go index 954a747..608c2b1 100644 --- a/decimal.go +++ b/decimal.go @@ -16,25 +16,56 @@ var ErrInvalidDecimal = errors.New("the integer portion is larger than 12 digits // marshalDecimal serializes as defined in // https://httpwg.org/specs/rfc9651.html#ser-decimal. // +// Rounding is done on the shortest decimal form of d, not on its binary +// value: 0.5015 is stored as 0.50149999..., but it must serialize as 0.502. +// // TODO(dunglas): add support for decimal float type when one will be available // (https://github.com/golang/go/issues/19787) func marshalDecimal(b io.StringWriter, d float64) error { - const TH = 0.001 + if math.IsNaN(d) || math.IsInf(d, 0) { + return ErrInvalidDecimal + } + + s := strconv.FormatFloat(math.Abs(d), 'f', -1, 64) + intPart, frac, _ := strings.Cut(s, ".") + + if len(frac) > maxDecDigit { + digits := []byte(intPart + frac[:maxDecDigit]) + rest := frac[maxDecDigit:] - rounded := math.RoundToEven(d/TH) * TH - i, _ := math.Modf(rounded) + // Round half to even. + if rest[0] > '5' || (rest[0] == '5' && (strings.TrimRight(rest[1:], "0") != "" || (digits[len(digits)-1]-'0')%2 == 1)) { + i := len(digits) - 1 + for ; i >= 0 && digits[i] == '9'; i-- { + digits[i] = '0' + } - if i < -999999999999 || i > 999999999999 { + if i < 0 { + digits = append([]byte{'1'}, digits...) + } else { + digits[i]++ + } + } + + intPart = string(digits[:len(digits)-maxDecDigit]) + frac = string(digits[len(digits)-maxDecDigit:]) + } + + if len(intPart) > 12 { return ErrInvalidDecimal } - s := strings.TrimRight(strconv.FormatFloat(rounded, 'f', 3, 64), "0") + frac = strings.TrimRight(frac, "0") // Ensure at least one digit after the decimal point. - if strings.HasSuffix(s, ".") { - s += "0" + if frac == "" { + frac = "0" + } + + if d < 0 && (intPart != "0" || frac != "0") { + intPart = "-" + intPart } - _, err := b.WriteString(s) + _, err := b.WriteString(intPart + "." + frac) return err } diff --git a/decimal_test.go b/decimal_test.go index c079c6d..7a8b525 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -1,6 +1,7 @@ package httpsfv import ( + "math" "strings" "testing" ) @@ -24,6 +25,18 @@ func TestMarshalDecimal(t *testing.T) { {-9999999999999.0, "", false}, {9999999999999.0, "", false}, {1.9, "1.9", true}, + // Ties are decided on the decimal value, not on the binary one. + {0.5015, "0.502", true}, + {-0.5015, "-0.502", true}, + {0.5025, "0.502", true}, + {2.0005, "2.0", true}, + {1.0025, "1.002", true}, + {0.9995, "1.0", true}, + {999999999999.9995, "", false}, + {-0.0001, "0.0", true}, + {math.NaN(), "", false}, + {math.Inf(1), "", false}, + {math.Inf(-1), "", false}, } var b strings.Builder diff --git a/structured-field-tests b/structured-field-tests index 1e280c3..10781e4 160000 --- a/structured-field-tests +++ b/structured-field-tests @@ -1 +1 @@ -Subproject commit 1e280c3ed9ffe0ca5fdb1d97219dddc389007677 +Subproject commit 10781e4a59c387853c900cc06cdd8a3fd7ae4134