Uh oh!
There was an error while loading. Please reload this page.
Add Integer::log variants - #80918
Conversation
rust-highfive
commented
Jan 11, 2021
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.
yoshuawuyts
commented
Jan 11, 2021
Okay, applied the fix from mentioned above. Fingers crossed this passes now. |
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
yoshuawuyts
commented
Jan 13, 2021
CDirkx
commented
Jan 14, 2021
All of these methods could also be made const; that would only depend on |
bors
commented
Jan 14, 2021
☔ The latest upstream changes (presumably #78259) made this pull request unmergeable. Please resolve the merge conflicts. |
yoshuawuyts
commented
Jan 15, 2021
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? |
the8472
commented
Jan 16, 2021
The methods are unstable, so this could also be discussed before stabilization. |
yoshuawuyts
commented
Jan 16, 2021
@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. |
EdorianDark
commented
Jan 16, 2021
@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. |
m-ou-se
commented
Jan 24, 2021
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? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
bors
commented
Jan 31, 2021
☔ The latest upstream changes (presumably #81578) made this pull request unmergeable. Please resolve the merge conflicts. |
yoshuawuyts
commented
Feb 3, 2021
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? |
yoshuawuyts
commented
Jun 25, 2021
cc/ @m-ou-se this should be ready for another review when you have time |
yoshuawuyts
commented
Jun 25, 2021
Was halfway through making dinner when I realized that we could just turn off the overflow errors here, evading the use of |
m-ou-se
left a comment
There was a problem hiding this comment.
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.
m-ou-se
commented
Jul 6, 2021
@bors r+ Thanks for making this happen, @yoshuawuyts! |
m-ou-se
commented
Jul 6, 2021
@bors ping? |
m-ou-se
commented
Jul 6, 2021
@bors r+ |
bors
commented
Jul 6, 2021
📌 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
bors
commented
Jul 7, 2021
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
mstankus
commented
Jul 16, 2022
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
base10for an integer. We might try and calculate thebase2for 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 theilogfunction. 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