Crates.io | int_traits |
lib.rs | int_traits |
version | 0.1.1 |
source | src |
created_at | 2016-07-14 02:22:03.178301 |
updated_at | 2016-09-28 09:49:33.06803 |
description | Provides a trait for extended functions on integers |
homepage | |
repository | https://github.com/tiehuis/int_traits |
max_upload_size | |
id | 5663 |
size | 17,803 |
This crate provides extra traits for working with integers. The traits provided are those provided by default for floating point values, but extended to provide optimized integer routines.
This is mainly intended to avoid extensive casting when we want extended operations for integers.
let a = 50_usize;
let a_sqrt = (a as f32).sqrt() as usize;
becomes
let a = 50_usize;
let a_sqrt = a.sqrt();
We provide the following functions
sqrt
cbrt
log
log2
log10
for the following types
i8
i16
i32
i64
u8
u16
u32
u64
isize
usize
If a negative number is passed to one of the functions, we panic on runtime.
extern crate int_traits;
use int_traits::IntTraits;
fn main() {
let a = 50_usize;
println!("{}", a.sqrt());
}
It is not known whether we want to return floating point values or perform the integer rounding explicitly.
This could likely be split up into seperate functions isqrt
and sqrt
or
two different trait implementations could be provided.
We do not perform any optimized functions currently. For example, since we do not need to calculate the fractional part of a square root we can make some potential shortcuts to improve this.