-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Give float some examples #22030
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
Give float some examples #22030
Conversation
r? @huonw (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -75,16 +75,86 @@ mod cmath { | |||
|
|||
#[stable(feature = "rust1", since = "1.0.0")] | |||
impl Float for f32 { | |||
/// Return `NaN` |
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 this should be Returns
NaN.
(with an s and a ‘.’)—the indicative style is recommended over the imperative.
(Same thing for the others below.)
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.
Fixed
#[inline] | ||
fn classify(self) -> FpCategory { num::Float::classify(self) } | ||
|
||
/// Returns the mantissa, base 2 exponent, and sign as integers. The original |
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.
Previously didn't specify exponents base (2
? 10
? 7
? e
?). I had to figure out which one worked to recover the original number.
You could document the trait itself rather than this specific implementation of it, no? |
I don't mind documenting the trait. I was thinking there weren't any examples of traits being documented directly: |
11cf2bc
to
cd2c941
Compare
Examples shouldn't break if you add a sign somewhere so prefer assigning to variables before using them: // Should prefer
let f = 2.0f64;
f.powi(2);
// over
2.0f64.powi(2);
// because
-2.0f64.powi(2);
// gets parsed as
-(2.0f64.powi(2));
// when the desired is
(-2.0f64).powi(2);
// but this works properly
let f = -2.0f64;
f.powi(2); Note: #22105 ran into this. |
Yes, we usually document the trait, not the specific implementation. This seems good though, thanks! would you mind squashing? r=me after |
Got a few left but sure. |
Do you want me to just wait until you've got more? I assumed this was ready, if not, I'm happy to hold off. |
Yeah. I've got another commit incoming. I'll squash after. I got most everything, maybe skipping deprecated and possibly a few unstable. I'll change the title from WIP when it's ready. |
Cool, thank you |
c8aac6c
to
396244d
Compare
@steveklabnik This depends on #22076 merging. It won't pass |
396244d
to
fb5d9da
Compare
/// Returns the mantissa, base 2 exponent, and sign as integers, respectively. | ||
/// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. | ||
/// The number is encoded with a scheme documented at | ||
/// [IEEE floating point][IEEE-floating-point]. |
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.
How a float is encoded needs a good reference. This link is probably wrong and is mainly so I remember. Reposting so it's not hidden.
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.
Deferred to #22152 . Just referred to the float section of the reference for now.
fb5d9da
to
208e9bc
Compare
@steveklabnik Seems pretty good. Needs #22076 though so gotta wait until that lands. I'll do r= after that merges and |
Rebased this commit back to tip. Should be mergeable. Didn't retest. |
no rollup as this is a big diff |
⌛ Testing commit 1a133f3 with merge b603823... |
Some examples for `std::num::Float` ~~This is WIP for making examples for `f32`. This probably won't pass `make tidy` and I'm not sure which `f32` needs documentation. rust-lang#22025 shows 2 sets of `f32` which seems split between `core` and `std`. I'm not sure which should be documented but I started doing a couple from `std`. Easy to move if that's where they go...~~ ~~Gotta build it eventually to actually see if the docs actually appear where I think they will or if I'm just disillusioned.~~ cc @steveklabnik
💔 Test failed - auto-win-32-nopt-t |
@bors: retry |
⌛ Testing commit 1a133f3 with merge 4087e5b... |
💔 Test failed - auto-win-32-nopt-t |
@bors: retry |
⌛ Testing commit 1a133f3 with merge 886aca4... |
💔 Test failed - auto-win-64-nopt-t |
@bors: retry |
⌛ Testing commit 1a133f3 with merge c34229c... |
💔 Test failed - auto-linux-64-nopt-t |
@bors: retry |
⌛ Testing commit 1a133f3 with merge 4b92061... |
💔 Test failed - auto-linux-64-x-android-t |
@bors: retry |
⌛ Testing commit 1a133f3 with merge 77988b5... |
💔 Test failed - auto-linux-64-opt |
Some examples for
std::num::Float
This is WIP for making examples forf32
. This probably won't passmake tidy
and I'm not sure whichf32
needs documentation. #22025 shows 2 sets off32
which seems split betweencore
andstd
. I'm not sure which should be documented but I started doing a couple fromstd
. Easy to move if that's where they go...Gotta build it eventually to actually see if the docs actually appear where I think they will or if I'm just disillusioned.cc @steveklabnik