We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
checked_ilog
base
1 parent 4056082 commit 8948390Copy full SHA for 8948390
library/core/src/num/uint_macros.rs
@@ -1571,9 +1571,14 @@ macro_rules! uint_impl {
1571
// applied by the compiler. If you want those specific bases,
1572
// use `.checked_ilog2()` or `.checked_ilog10()` directly.
1573
if core::intrinsics::is_val_statically_known(base) {
1574
- if base == 2 {
1575
- return self.checked_ilog2();
1576
- } else if base == 10 {
+ // change of base:
+ // if base == 2 ** k, then
+ // log(base, n) == log(2, n) / k
1577
+ if base.is_power_of_two() && base > 1 {
1578
+ let k = base.ilog2();
1579
+ return Some(try_opt!(self.checked_ilog2()) / k);
1580
+ }
1581
+ if base == 10 {
1582
return self.checked_ilog10();
1583
}
1584
0 commit comments