Preserve precision when passing BigDecimal or BigFloat to sprintf %i#15808
Merged
straight-shoota merged 3 commits intocrystal-lang:masterfrom May 23, 2025
Merged
Conversation
straight-shoota
approved these changes
May 21, 2025
Merged
straight-shoota
pushed a commit
that referenced
this pull request
May 26, 2025
This is the only type in the standard library where `#to_i32` is already defined but `#to_i` isn't. This makes `BigRational` suspectible to precision loss when passed to `sprintf` `%i` (see also #15808), since `BigRational#to_i32` currently delegates to `#to_f64` instead of `#to_big_i`. However this is still an improvement over the current situation where `BigRational` isn't supported by `sprintf` at all.
1 task
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.
When
String::Formatterformats a non-integer for integer specifiers such as%d, it calls#to_ion that value to reuse the implementation forInt32, meaningBigDecimals andBigFloats outsideInt32's range will raise anOverflowError. Fortunately,BigIntis already compatible withsprintf, so they could delegate toBigIntinstead ofInt32.This may look similar to #15203, but the fix there is not as simple as delegating to
#to_big_f, because that also loses precision.