Trait IntLog

Source
pub trait IntLog {
    // Required methods
    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§

Source

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);
Source

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);
Source

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);
Source

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);

Implementations on Foreign Types§

Source§

impl IntLog for &i8

Source§

impl IntLog for &i16

Source§

impl IntLog for &i32

Source§

impl IntLog for &i64

Source§

impl IntLog for &i128

Source§

impl IntLog for &isize

Source§

impl IntLog for &u8

Source§

impl IntLog for &u16

Source§

impl IntLog for &u32

Source§

impl IntLog for &u64

Source§

impl IntLog for &u128

Source§

impl IntLog for &usize

Source§

impl IntLog for &mut i8

Source§

impl IntLog for &mut i16

Source§

impl IntLog for &mut i32

Source§

impl IntLog for &mut i64

Source§

impl IntLog for &mut i128

Source§

impl IntLog for &mut isize

Source§

impl IntLog for &mut u8

Source§

impl IntLog for &mut u16

Source§

impl IntLog for &mut u32

Source§

impl IntLog for &mut u64

Source§

impl IntLog for &mut u128

Source§

impl IntLog for &mut usize

Source§

impl IntLog for i8

Source§

impl IntLog for i16

Source§

impl IntLog for i32

Source§

impl IntLog for i64

Source§

impl IntLog for i128

Source§

impl IntLog for isize

Source§

impl IntLog for u8

Source§

impl IntLog for u16

Source§

impl IntLog for u32

Source§

impl IntLog for u64

Source§

impl IntLog for u128

Source§

impl IntLog for usize

Source§

impl IntLog for Box<i8>

Source§

impl IntLog for Box<i16>

Source§

impl IntLog for Box<i32>

Source§

impl IntLog for Box<i64>

Source§

impl IntLog for Box<i128>

Source§

impl IntLog for Box<isize>

Source§

impl IntLog for Box<u8>

Source§

impl IntLog for Box<u16>

Source§

impl IntLog for Box<u32>

Source§

impl IntLog for Box<u64>

Source§

impl IntLog for Box<u128>

Source§

impl IntLog for Box<usize>

Implementors§