pub trait IntLog {
fn log10(self) -> usize;
fn log2(self) -> usize;
fn checked_log10(self) -> Option<usize>;
fn checked_log2(self) -> Option<usize>;
}Expand description
Trait that provides logarithms for integer types.
The log2 and log10 methods are optimized for the integer width and are
[inline] since the code remains small enough. They typically use constant tables
that are only stored once, even if the methods using them are inlined multiple times.
The checked versions of the methods, checked_log2 and checked_log10,
return None if the logarithm is undefined for the parameter value, whereas the unchecked
methods mentioned above simply panic or return a wrong value.
Required Methods§
sourcefn log10(self) -> usize
fn log10(self) -> usize
Returns the largest integer less than or equal to the base 10 logarithm of the integer.
Logarithms are only defined on positive values, calling log10 with a null or a negative
argument may trigger a panic or return a wrong value.
See checked_log10 for a method that checks its argument first.
Examples
let value: u64 = 100;
assert_eq!(value.log10(), 2);
assert_eq!(i32::log10(99), 1);sourcefn log2(self) -> usize
fn log2(self) -> usize
Returns the largest integer less than or equal to the base 2 logarithm of the integer.
Logarithms are only defined on positive values, calling log10 with a null or a negative
argument may trigger a panic or return a wrong value.
See checked_log2 for a method that checks its argument first.
Examples
let value: u64 = 64;
assert_eq!(value.log2(), 6);
assert_eq!(i32::log2(63), 5);sourcefn checked_log10(self) -> Option<usize>
fn checked_log10(self) -> Option<usize>
Checked base 10 logarithm. Returns the largest integer less than or equal to the base 10
logarithm of the integer, or None if it doesn’t exist.
Examples
assert_eq!(100_u32.checked_log10(), Some(2));
assert_eq!(u64::checked_log10(99), Some(1));
assert_eq!(0_u32.checked_log10(), None);sourcefn checked_log2(self) -> Option<usize>
fn checked_log2(self) -> Option<usize>
Checked base 10 logarithm. Returns the largest integer less than or equal to the base 10
logarithm of the integer, or None if it doesn’t exist.
Examples
assert_eq!(64_u32.checked_log2(), Some(6));
assert_eq!(u64::checked_log2(63), Some(5));
assert_eq!(0_u32.checked_log2(), None);