Repository navigation
fix: follow Go's round-half-even on exact decimal ties in float formatting - #58
Merged
Merged
Conversation
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-----
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #57. Stacked on #56 (which is stacked on #55); retarget to
mainas 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.3is the double912968984149762.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_digitsapplies Go's tie rule (cheap exact-mode pre-check, tie confirmed with exact rational arithmetic, even neighbour used only if it round-trips);canonical_floatandgo_float_display(the%vport) 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%vforms. (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