-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Expose tests for {f32,f64}.total_cmp in docs #115412
Expose tests for {f32,f64}.total_cmp in docs #115412
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @scottmcm (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Hmm, I see that #73328 might already explain why this is commented out (i.e. that the sign bit may change on NaN unexpectedly under some optimization cases) but I'm not sure this would occur with the constants used in the sample code. |
It might be due to #115567, in which case adding a suitable |
OTOH, |
I'm approving this under the interpretation that a test passing does not constitute an API guarantee. @bors r+ |
I do generally interpret doctests that are shown in rustdoc as lib API guarantees. If I can't rely on the explicitly given examples then what can I rely on? |
Ah, well, |
That said, is there any reason not to make this an API guarantee? |
Hmm, seems like it'd have to be a lang guarantee, no? Because it's obviously a guarantee in a world where the library code always gets exactly the nans they wrote, and they never change out from under them. This test needs it to stay the canonical nan, I guess, since it's comparing with I guess the test could be updated to check bits-equal for everything except the NAN, and check |
Don't we have consensus that NaN bits stay stable when floats are just passed around? Should we do t-lang FCP on that question? This doesn't have to wait for the broader set of questions around what exactly the NaNs are that come out of a float operation. |
Though I guess there also is the question of what we guarantee about the bit pattern in The docs about that constant even explicitly say
So, in that sense the test is already bogus. |
b7d8417
to
08f10c2
Compare
Given the discussion, I updated the PR to make it clear there may be different results. (My main concern was just seeing an example with an expected output, since "#" inside the comment -- likely done for the same reasons discussed here -- was stripped out of the HTML.) |
This comment has been minimized.
This comment has been minimized.
library/core/src/num/f32.rs
Outdated
/// ``` | ||
/// Since the NaN constant does not have a well-defined sign, | ||
/// this test may result in either: | ||
/// ``` | ||
/// assert!(bois.into_iter().map(|b| b.weight) | ||
/// .zip([-5.0, 0.1, 10.0, 99.0, f32::INFINITY, f32::NAN].iter()) | ||
/// .all(|(a, b)| a.to_bits() == b.to_bits())) | ||
/// ``` | ||
/// or: | ||
/// ``` | ||
/// assert!(bois.into_iter().map(|b| b.weight) | ||
/// .zip([f32::NAN, -5.0, 0.1, 10.0, 99.0, f32::INFINITY].iter()) | ||
/// .all(|(a, b)| a.to_bits() == b.to_bits())) |
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.
Instead of separating this out (and getting error messages when it can't be compiled) why not make this logical disjunction part of the code: simply wrap this in
if f32::NAN.is_sign_negative() {
assert!(..)
} else {
assert!(..)
};
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.
Done (for real?)
They should. They should support checking their sign without that changing, too. |
cfc285d
to
2a62677
Compare
/// # .zip([-5.0, 0.1, 10.0, 99.0, f32::INFINITY, f32::NAN].iter()) | ||
/// # .all(|(a, b)| a.to_bits() == b.to_bits())) | ||
/// | ||
/// if f32::NAN.is_sign_negative() { |
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.
/// if f32::NAN.is_sign_negative() { | |
//// // `f32::NAN` could be positive or negative, which will affect the sort order. | |
/// if f32::NAN.is_sign_negative() { |
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.
Applied (manually) since f64.fs also needs the change.
Uncomment the assert! line and account and document that the sign of NaN is not positive, necessarily.
2a62677
to
8066079
Compare
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.
LGTM.
@bors r+ |
☀️ Test successful - checks-actions |
1 similar comment
☀️ Test successful - checks-actions |
Finished benchmarking commit (6416e2e): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 678.192s -> 675.683s (-0.37%) |
Expose tests for {f32,f64}.total_cmp in docs
Uncomment the helpful
assert_eq!
line, which is stripped out completely in docs, and leaves the reader to mentally play through the algorithm, or go to the playground and add a println!, to see what the result will be.(If these tests are known to fail on some platforms, is there some mechanism to conditionalize this or escape the test so the
assert_eq!
source will be visible on the web? I am a newbie, which is why I was reading docs ;)