ilog

crate documentation build status crate

Base 10 and 2 logarithm functions for integer types.

The IntLog trait defines the following methods:

rust fn log10(self) -> usize fn log2(self) -> usize fn checked_log10(self) -> Option<usize> fn checked_log2(self) -> Option<usize>

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.

Examples

```rust use ilog::IntLog;

let hundred: u32 = 100; asserteq!(hundred.log10(), 2); asserteq!(u32::log10(99), 1);

let value: u64 = 256; asserteq!(value.log2(), 8); asserteq!(u64::log2(255), 7);

asserteq!(u32::checkedlog2(63), Some(5)); asserteq!(0u32.checked_log2(), None); ```

Usage

Add this dependency to your Cargo.toml file:

toml [dependencies] ilog = "0"

Compatibility

The ilog crate is tested for rustc 1.65 and greater.

Releases

RELEASES.md keeps a log of all the releases.

License

Licensed under MIT license.