Skip to content

fix: follow Go's round-half-even on exact decimal ties in float formatting - #58

Merged
sinkog merged 3 commits into
mainfrom
fix/float-tie-go-parity
Oct 17, 2026
Merged

sinkog merged 3 commits into
mainfrom
fix/float-tie-go-parity

Conversation

@sinkog

@sinkog sinkog commented Oct 17, 2026

Copy link
Copy Markdown
Member

Closes #57. Stacked on #56 (which is stacked on #55); retarget to main as they merge.

The same JSON number produced different canonical bytes — hence different digests — in Rust and Go whenever the double's exact value is a decimal tie: 912968984149762.3 is the double 912968984149762.25; Go prints …762.2 (round half to even on the shortest digit string, strconv/ftoaryu.go), Rust printed …762.3. Found by the generated cross-language corpus in the #54 work.

Fix: canonical::shortest_digits applies Go's tie rule (cheap exact-mode pre-check, tie confirmed with exact rational arithmetic, even neighbour used only if it round-trips); canonical_float and go_float_display (the %v port) both use it.

Measured against real Go, 1,499,849 floats (j·2^-q, large values with eighth fractions, random bit patterns, decimal-looking values): 44,029 mismatches before (~3%) → 0 after, for both the canonical and the %v forms. (One-off scratch comparison, not committed.)

Tests: unit tests with Go-verified values; conformance/canonicalize/float_exact_decimal_tie; a Go test pinning the reference behaviour. Disabling the tie rule fails the unit tests and the corpus vector. fmt, clippy -D warnings, cargo test --workspace, gofmt/vet/go test all green.

🤖 Generated with Claude Code

Gábor Z Sinkó added 2 commits October 17, 2026 13:35
…tting (#57)

When a double's exact value lies exactly halfway between two equally short
decimals, Go's strconv rounds half to even on the shortest digit string
(ftoaryu.go, ryuDigits32); Rust's {:e}/Display could pick the odd
neighbour. 912968984149762.3 is the double 912968984149762.25: Go prints
...762.2, Rust printed ...762.3, so the same document produced different
canonical bytes and digests in the two languages. Found by the generated
cross-language corpus added for #54.

- canonical: shortest_digits applies Go's tie rule. A tie needs the exact
  expansion to be one digit longer than the shortest form and end in 5, so
  one exact-mode format is the cheap pre-check; the tie itself is confirmed
  with exact rational arithmetic, and the even neighbour is used only if it
  parses back to the same double. canonical_float renders from it.
- collection: go_float_display (the %v port) uses the same routine.

Measured against real Go on 1.5M floats (j * 2^-q, large values with eighth
fractions, random bit patterns, decimal-looking values): 44,029 mismatches
before (about 3%), 0 after, for both the canonical form and the %v form.

Tests: unit tests with Go-verified values, a canonicalize corpus vector
(float_exact_decimal_tie), and a Go test pinning the reference behaviour.
Disabling the tie rule fails the unit tests and the corpus vector.

Closes #57

---
[signing-metadata]
key = cic-my-sign-key
signature = vault:v1:MEUCIQCG0PK/2i5B3O8SH5yQmdU/n+2CszFgQA9MyEDdQd7MIAIgFF/+QUYv+cs+sOdD6Tf+AiEWcMVGpfUUwglnGpOsOcE=
hash-algorithm = sha256
digest = vOrwqFncAtxzlHXC2jKAaTdMNvqSOoxBhVloXAxfOC4=

[certificate]
-----BEGIN CERTIFICATE-----
MIICBjCCAaygAwIBAgIUSnRMR6RPnEbg296XWPOqq/u5PCwwCgYIKoZIzj0EAwIw
QzELMAkGA1UEBhMCSFUxGTAXBgNVBAoMEENlbnRyYWxJbmZyYUNvcmUxGTAXBgNV
BAMMEENJQyBEZXZlbG9wZXIgQ0EwHhcNMjYwMzIwMTMyMjU5WhcNMjYxMjMxMTMy
MjU5WjBFMQswCQYDVQQGEwJIVTEZMBcGA1UECgwQQ2VudHJhbEluZnJhQ29yZTEb
MBkGA1UEAwwSR2Fib3IgWm9sdGFuIFNpbmtvMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAEIG2CVmTfmLB9pLLclj7YmP2eedAjklpy4LGrU2ijoiy6Xqpuybv7OgJe
i+ez31s65NEV8+X/ByeX1cstR988z6N8MHowCQYDVR0TBAIwADAdBgNVHQ4EFgQU
yZN6AIX/TNnIJ9GwAa/NRN3ujHAwHwYDVR0jBBgwFoAUXn6CHYzPUqU4JVP8g+OS
WeDYjhcwDgYDVR0PAQH/BAQDAgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEF
BQcDBDAKBggqhkjOPQQDAgNIADBFAiEA+bFzXRoJ4PCQbhAAtpkcMjt0vNj5rEW0
lOMBGDNyaWkCIB1vmM7PcZzv/c9bIrxF5kqv6QXomouhByUfeNUTbpKW
-----END CERTIFICATE-----
---
[signing-metadata]
key = cic-my-sign-key
signature = vault:v1:MEYCIQDWqbAwKjkvzjsD2RFlqcrnLwEbs1/qmImWlpBHu7ch6wIhANm14+usP3HC8e9zOdnnJGlEW8OXL8YOF9Hk5lXFdJDt
hash-algorithm = sha256
digest = 2X9ZfbCwR7J1ys3SJyVYuHNMa/n3dgiN5/hDFoxlPRA=

[certificate]
-----BEGIN CERTIFICATE-----
MIICBjCCAaygAwIBAgIUSnRMR6RPnEbg296XWPOqq/u5PCwwCgYIKoZIzj0EAwIw
QzELMAkGA1UEBhMCSFUxGTAXBgNVBAoMEENlbnRyYWxJbmZyYUNvcmUxGTAXBgNV
BAMMEENJQyBEZXZlbG9wZXIgQ0EwHhcNMjYwMzIwMTMyMjU5WhcNMjYxMjMxMTMy
MjU5WjBFMQswCQYDVQQGEwJIVTEZMBcGA1UECgwQQ2VudHJhbEluZnJhQ29yZTEb
MBkGA1UEAwwSR2Fib3IgWm9sdGFuIFNpbmtvMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAEIG2CVmTfmLB9pLLclj7YmP2eedAjklpy4LGrU2ijoiy6Xqpuybv7OgJe
i+ez31s65NEV8+X/ByeX1cstR988z6N8MHowCQYDVR0TBAIwADAdBgNVHQ4EFgQU
yZN6AIX/TNnIJ9GwAa/NRN3ujHAwHwYDVR0jBBgwFoAUXn6CHYzPUqU4JVP8g+OS
WeDYjhcwDgYDVR0PAQH/BAQDAgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEF
BQcDBDAKBggqhkjOPQQDAgNIADBFAiEA+bFzXRoJ4PCQbhAAtpkcMjt0vNj5rEW0
lOMBGDNyaWkCIB1vmM7PcZzv/c9bIrxF5kqv6QXomouhByUfeNUTbpKW
-----END CERTIFICATE-----
---
[signing-metadata]
key = cic-my-sign-key
signature = vault:v1:MEUCIAvG8zw/6xuy/RJosKyMQrTr9lWKzQYaQEhHuuEARKQFAiEA2UfDlWxgCNxueh6Q1FTe3meqAXz1p5BQvgIODz8jQL0=
hash-algorithm = sha256
digest = ZcJB575dUQCeTbUqvbMlOcD9upa4R3t8a1nSh/t1Hm4=

[certificate]
-----BEGIN CERTIFICATE-----
MIICBjCCAaygAwIBAgIUSnRMR6RPnEbg296XWPOqq/u5PCwwCgYIKoZIzj0EAwIw
QzELMAkGA1UEBhMCSFUxGTAXBgNVBAoMEENlbnRyYWxJbmZyYUNvcmUxGTAXBgNV
BAMMEENJQyBEZXZlbG9wZXIgQ0EwHhcNMjYwMzIwMTMyMjU5WhcNMjYxMjMxMTMy
MjU5WjBFMQswCQYDVQQGEwJIVTEZMBcGA1UECgwQQ2VudHJhbEluZnJhQ29yZTEb
MBkGA1UEAwwSR2Fib3IgWm9sdGFuIFNpbmtvMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAEIG2CVmTfmLB9pLLclj7YmP2eedAjklpy4LGrU2ijoiy6Xqpuybv7OgJe
i+ez31s65NEV8+X/ByeX1cstR988z6N8MHowCQYDVR0TBAIwADAdBgNVHQ4EFgQU
yZN6AIX/TNnIJ9GwAa/NRN3ujHAwHwYDVR0jBBgwFoAUXn6CHYzPUqU4JVP8g+OS
WeDYjhcwDgYDVR0PAQH/BAQDAgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEF
BQcDBDAKBggqhkjOPQQDAgNIADBFAiEA+bFzXRoJ4PCQbhAAtpkcMjt0vNj5rEW0
lOMBGDNyaWkCIB1vmM7PcZzv/c9bIrxF5kqv6QXomouhByUfeNUTbpKW
-----END CERTIFICATE-----
@sinkog
sinkog changed the base branch from feat/value-newtypes to main October 17, 2026 12:03
@sinkog sinkog closed this Oct 17, 2026
@sinkog sinkog reopened this Oct 17, 2026
@sinkog
sinkog merged commit f5f8bdb into main Oct 17, 2026
4 checks passed
@sinkog
sinkog deleted the fix/float-tie-go-parity branch October 17, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Float canonicalization differs from Go on exact decimal ties (round-half-even)

1 participant