-
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
Add Integer::log variants #80918
Add Integer::log variants #80918
Conversation
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
This patch still needs to implement the workaround mentioned by @tspiteri in #70835 (comment):
The tests are expected to pass CI, but fail once merged and Bors runs. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Okay, applied the fix from mentioned above. Fingers crossed this passes now. |
This comment has been minimized.
This comment has been minimized.
All of these methods could also be made const; that would only depend on |
☔ The latest upstream changes (presumably #78259) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixed the merge conflict and applied @CDirkx's feedback: all methods are now |
This comment has been minimized.
This comment has been minimized.
I am against the names, since they are very confusing. Lets look againg at what other languages do for log on an integer:
Is there any example to call the integer logarithm log? |
The methods are unstable, so this could also be discussed before stabilization. |
@EdorianDark you've expressed that position repeatedly in earlier threads; namely: #70887 (comment) and #70835 (comment). We concluded in #70835 (comment) that naming should not be a blocker for landing this PR (I commented, you +1'd it). I don't believe much has changed in the interim, and still hold the position that we don't need to finalize the names in order to move forward with adding this functionality to nightly. |
@yoshuawuyts i changed my mind. I feel that in the tracking issue wasn't enough discussion about the names. Of cause my opinion isn't that important since I am not in any Rust team. |
This would produce a very generic panic message in the non- Except for division by zero (which would be undefined behaviour), all other panics on the integer methods and operators only happen in debug mode. In release mode, they wrap around or return zero. For example, /// When return value overflows (i.e., `self > (1 << (N-1))` for type
/// `uN`), it panics in debug mode and return value is wrapped to 0 in
/// release mode (the only situation in which method can return 0). Should we keep that consistency? |
☔ The latest upstream changes (presumably #81578) made this pull request unmergeable. Please resolve the merge conflicts. |
I hadn't considered this, and I think we absolutely should. For now I've updated the PR with the other feedback and fixed the merge conflict. But I believe what we should then do is:
Is that accurate? |
cc/ @m-ou-se this should be ready for another review when you have time |
Was halfway through making dinner when I realized that we could just turn off the overflow errors here, evading the use of |
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.
Looks good to me! (After #80918 (comment) is fixed.)
Can you update the tracking issue to use our new template? That includes a short overview of the public API.
@bors r+ Thanks for making this happen, @yoshuawuyts! |
@bors ping? |
@bors r+ |
📌 Commit 9f57996 has been approved by |
Rollup of 8 pull requests Successful merges: - rust-lang#80918 (Add Integer::log variants) - rust-lang#86717 (Rename some Rust 2021 lints to better names ) - rust-lang#86819 (Clean up rustdoc IDs) - rust-lang#86880 (Test ManuallyDrop::clone_from.) - rust-lang#86906 (Replace deprecated compare_and_swap and fix typo in core::sync::atomic::{fence, compiler_fence} docs) - rust-lang#86907 (Migrate `cpu-usage-over-time.py` to Python 3) - rust-lang#86916 (rewrote documentation for thread::yield_now()) - rust-lang#86919 (Update books) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
special case for integer log10 Now that rust-lang#80918 has been merged, this PR provides a faster version of `log10`. The PR also adds some tests for values close to all powers of 10.
Rollup of 8 pull requests Successful merges: - rust-lang#80918 (Add Integer::log variants) - rust-lang#86717 (Rename some Rust 2021 lints to better names ) - rust-lang#86819 (Clean up rustdoc IDs) - rust-lang#86880 (Test ManuallyDrop::clone_from.) - rust-lang#86906 (Replace deprecated compare_and_swap and fix typo in core::sync::atomic::{fence, compiler_fence} docs) - rust-lang#86907 (Migrate `cpu-usage-over-time.py` to Python 3) - rust-lang#86916 (rewrote documentation for thread::yield_now()) - rust-lang#86919 (Update books) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Question: I am new to rust and am a mathematician. Thanks. |
This merge has been closed for a while. A better place to discuss things would be at the tracking issue. |
This is another attempt at landing #70835, which was approved by the libs team but failed on Android tests through Bors. The text copied here is from the original issue. The only change made so far is the addition of non-
checked_
variants of the log methods.Tracking issue: #70887
This implements
{log,log2,log10}
methods for all integer types. The implementation was provided by @substack for use in the stdlib.Note: I'm not big on math, so this PR is a best effort written with limited knowledge. It's likely I'll be getting things wrong, but happy to learn and correct. Please bare with me.
Motivation
Calculating the logarithm of a number is a generally useful operation. Currently the stdlib only provides implementations for floats, which means that if we want to calculate the logarithm for an integer we have to cast it to a float and then back to an int.
— @substack, 2020-03-08
At higher numbers converting from an integer to a float we also risk overflows. This means that Rust currently only provides log operations for a limited set of integers.
The process of doing log operations by converting between floats and integers is also prone to rounding errors. In the following example we're trying to calculate
base10
for an integer. We might try and calculate thebase2
for the values, and attempt a base swap to arrive atbase10
. However because we're performing intermediate rounding we arrive at the wrong result:playground
This is somewhat nuanced as a lot of the time it'll work well, but in real world code this could lead to some hard to track bugs. By providing correct log implementations directly on integers we can help prevent errors around this.
Implementation notes
I checked whether LLVM intrinsics existed before implementing this, and none exist yet.
Also I couldn't really find a better way to write theilog
function. One option would be to make it a private method on the number, but I didn't see any precedent for that. I also didn't know where to best place the tests, so I added them to the bottom of the file. Even though they might seem like quite a lot they take no time to execute.References