-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
core: Implement feature float_exact_integer_constants
#152512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
okaneco
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
okaneco:exact_integer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+331
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,6 +275,68 @@ impl f128 { | |
| #[unstable(feature = "f128", issue = "116909")] | ||
| pub const NEG_INFINITY: f128 = -1.0_f128 / 0.0_f128; | ||
|
|
||
| /// Maximum integer that can be represented exactly in an [`f128`] value, | ||
| /// with no other integer converting to the same floating point value. | ||
| /// | ||
| /// For an integer `x` which satisfies `MIN_EXACT_INTEGER <= x <= MAX_EXACT_INTEGER`, | ||
| /// there is a "one-to-one" mapping between [`i128`] and [`f128`] values. | ||
| /// `MAX_EXACT_INTEGER + 1` also converts losslessly to [`f128`] and back to | ||
| /// [`i128`], but `MAX_EXACT_INTEGER + 2` converts to the same [`f128`] value | ||
| /// (and back to `MAX_EXACT_INTEGER + 1` as an integer) so there is not a | ||
| /// "one-to-one" mapping. | ||
| /// | ||
| /// [`MAX_EXACT_INTEGER`]: f128::MAX_EXACT_INTEGER | ||
| /// [`MIN_EXACT_INTEGER`]: f128::MIN_EXACT_INTEGER | ||
| /// ``` | ||
| /// #![feature(f128)] | ||
| /// #![feature(float_exact_integer_constants)] | ||
| /// # #[cfg(not(all(target_arch = "x86", not(target_feature = "sse"))))] { | ||
| /// # #[cfg(target_has_reliable_f128)] { | ||
| /// let max_exact_int = f128::MAX_EXACT_INTEGER; | ||
| /// assert_eq!(max_exact_int, max_exact_int as f128 as i128); | ||
| /// assert_eq!(max_exact_int + 1, (max_exact_int + 1) as f128 as i128); | ||
| /// assert_ne!(max_exact_int + 2, (max_exact_int + 2) as f128 as i128); | ||
| /// | ||
| /// // Beyond `f128::MAX_EXACT_INTEGER`, multiple integers can map to one float value | ||
| /// assert_eq!((max_exact_int + 1) as f128, (max_exact_int + 2) as f128); | ||
| /// # }} | ||
| /// ``` | ||
| // #[unstable(feature = "f128", issue = "116909")] | ||
| #[unstable(feature = "float_exact_integer_constants", issue = "152466")] | ||
| pub const MAX_EXACT_INTEGER: i128 = (1 << Self::MANTISSA_DIGITS) - 1; | ||
|
|
||
| /// Minimum integer that can be represented exactly in an [`f128`] value, | ||
| /// with no other integer converting to the same floating point value. | ||
| /// | ||
| /// For an integer `x` which satisfies `MIN_EXACT_INTEGER <= x <= MAX_EXACT_INTEGER`, | ||
| /// there is a "one-to-one" mapping between [`i128`] and [`f128`] values. | ||
| /// `MAX_EXACT_INTEGER + 1` also converts losslessly to [`f128`] and back to | ||
| /// [`i128`], but `MAX_EXACT_INTEGER + 2` converts to the same [`f128`] value | ||
| /// (and back to `MAX_EXACT_INTEGER + 1` as an integer) so there is not a | ||
| /// "one-to-one" mapping. | ||
| /// | ||
| /// This constant is equivalent to `-MAX_EXACT_INTEGER`. | ||
| /// | ||
| /// [`MAX_EXACT_INTEGER`]: f128::MAX_EXACT_INTEGER | ||
| /// [`MIN_EXACT_INTEGER`]: f128::MIN_EXACT_INTEGER | ||
| /// ``` | ||
| /// #![feature(f128)] | ||
| /// #![feature(float_exact_integer_constants)] | ||
| /// # #[cfg(not(all(target_arch = "x86", not(target_feature = "sse"))))] { | ||
| /// # #[cfg(target_has_reliable_f128)] { | ||
| /// let min_exact_int = f128::MIN_EXACT_INTEGER; | ||
| /// assert_eq!(min_exact_int, min_exact_int as f128 as i128); | ||
| /// assert_eq!(min_exact_int - 1, (min_exact_int - 1) as f128 as i128); | ||
| /// assert_ne!(min_exact_int - 2, (min_exact_int - 2) as f128 as i128); | ||
| /// | ||
| /// // Below `f128::MIN_EXACT_INTEGER`, multiple integers can map to one float value | ||
| /// assert_eq!((min_exact_int - 1) as f128, (min_exact_int - 2) as f128); | ||
| /// # }} | ||
| /// ``` | ||
| // #[unstable(feature = "f128", issue = "116909")] | ||
| #[unstable(feature = "float_exact_integer_constants", issue = "152466")] | ||
| pub const MIN_EXACT_INTEGER: i128 = -Self::MAX_EXACT_INTEGER; | ||
|
Comment on lines
308
to
338
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth mentioning that this is just
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
|
|
||
| /// Sign bit | ||
| pub(crate) const SIGN_MASK: u128 = 0x8000_0000_0000_0000_0000_0000_0000_0000; | ||
|
|
||
|
|
||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add examples to both constants? It would be good to demo the
MAX_EXACT_INTEGER,MAX_EXACT_INTEGER + 1,MAX_EXACT_INTEGER + 2property.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was uncertain about adding examples because every other float const seems to lack examples, but they'd come in handy here.
Added examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think examples are particularly important here because I would have expected this to be the value without the
- 1, so the more opportunities to emphasize its definition the better.(Things like
f32::INFINITYdon't have that issue.)