#traits #integer #int

int_traits

Provides a trait for extended functions on integers

2 releases

Uses old Rust 2015

0.1.1 Sep 28, 2016
0.1.0 Jul 14, 2016

#41 in #int

Download history 838/week @ 2024-03-14 735/week @ 2024-03-21 725/week @ 2024-03-28 659/week @ 2024-04-04 522/week @ 2024-04-11 578/week @ 2024-04-18 188/week @ 2024-04-25 303/week @ 2024-05-02 287/week @ 2024-05-09 315/week @ 2024-05-16 395/week @ 2024-05-23 522/week @ 2024-05-30 607/week @ 2024-06-06 688/week @ 2024-06-13 432/week @ 2024-06-20 160/week @ 2024-06-27

2,003 downloads per month
Used in 5 crates

Apache-2.0/MIT

7KB
122 lines

int_traits

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

Coverage

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.

Examples

extern crate int_traits;

use int_traits::IntTraits;

fn main() {
    let a = 50_usize;

    println!("{}", a.sqrt());
}

Design Questions

  • 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.

No runtime deps