Repository navigation
Fix #11131, #10509: std.conv.to: check float to integer range after truncation - #11132
Open
Sarthak-Shreshtha01 wants to merge 1 commit into
Open
Sarthak-Shreshtha01 wants to merge 1 commit into
Sarthak-Shreshtha01 wants to merge 1 commit into
Conversation
…ge after truncation to!T(value) checked the floating value against T.min and T.max before the fraction was dropped, so to!uint(-0.5) and to!ubyte(255.5) threw although the truncated value fits. The check against T.max also compared in the floating type, where long.max rounds up to 2^63, so 2^63.to!long returned long.min instead of throwing. A floating value now fits when it is in (T.min - 1, T.max + 1). Signed-off-by: Sarthak <sarthakshreshtha345@gmail.com>
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.
Rationale
tochecked a floating value againstT.minandT.maxbefore the fraction was dropped, soto!uint(-0.5),to!ubyte(255.5)andto!int(-2147483648.5)threw although the documented truncating conversion gives 0, 255 andint.min.The upper check also compared in the floating type, where
long.maxrounds up to 2^63. Soto!long(9223372036854775296.0)(which is 2^63 as adouble) returnedlong.mininstead of throwing, which is #10509.For floating to integral conversions, a value now fits when it is in
(T.min - 1, T.max + 1).T.max + 1is a power of two, so it is exact in every floating type. Infinities and out-of-range values still throwConvOverflowException, and NaN still throwsConvException.New unittests cover float, double and real at the edges of byte, ubyte, uint, int and long. Both new unittests fail without the change. One existing test accepted a wrong result for
to!int(float(int.max))(that float is 2^31); it now expects the overflow exception when the floating type cannot holdIntegral.max.make std/conv.testpasses (148 unittests), as dostd/json,std/math/roundingandstd/datetime/systime.Feature request or issue tracking
Closes #11131.
Closes #10509.
Pre-review checklist
LLM/AI disclosure
This PR was written entirely by an LLM/AI.