1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Provides a single trait `IntTraits` which provides extended traits for the
//! integer type. These are typically special cases such as `sqrt` and are aimed
//! primarily at reducing the incessant casting that is otherwise required for
//! floored integer behaviour.

/// Provides functions which extended the class methods on integers.
pub trait IntTraits<T: Sized> where Self: Sized {
    /// Takes the floored square root of a number.
    ///
    /// ## Panics
    /// Panics if `n` is negative.
    fn sqrt(self) -> T;

    /// Takes the floored cubic root of a number.
    ///
    /// ## Panics
    /// Panics if `n` is negative.
    fn cbrt(self) -> T;

    /// Returns the floored logarithm of `n`.
    ///
    /// The logarithm must be of integer base. This is to avoid unnecessary
    /// casts and is purely ergonomic.
    ///
    /// ## Panics
    /// Panics if `n` <= 0.
    fn log(self, n: u64) -> T;

    /// Returns the floored base 10 logarithm of `n`.
    ///
    /// ## Panics
    /// Panics if `n` <= 0.
    fn log10(self) -> T {
        self.log(10 as u64)
    }

    /// Returns the floored base 10 logarithm of `n`.
    ///
    /// ## Panics
    /// Panics if `n` <= 0.
    fn log2(self) -> T {
        self.log(2 as u64)
    }
}

macro_rules! impl_int_trait {
    ($t:ty) => {
        impl IntTraits<$t> for $t {
            fn sqrt(self) -> $t {
                if self < 0 {
                    panic!("cannot take sqrt of a negative value: {}", self)
                }
                (self as f64).sqrt() as $t
            }

            fn cbrt(self) -> $t {
                if self < 0 {
                    panic!("cannot take cbrt of a negative value: {}", self)
                }
                (self as f64).cbrt() as $t
            }

            fn log(self, n: u64) -> $t {
                if self <= 0 {
                    panic!("cannot take log of a value less than or equal to 0: {}", self)
                }
                (self as f64).log(n as f64) as $t
            }
        }
    };
}

macro_rules! impl_uint_trait {
    ($t:ty) => {
        impl IntTraits<$t> for $t {
            fn sqrt(self) -> $t {
                (self as f64).sqrt() as $t
            }

            fn cbrt(self) -> $t {
                (self as f64).cbrt() as $t
            }

            fn log(self, n: u64) -> $t {
                if self == 0 {
                    panic!("cannot take log of a value less than or equal to 0: {}", self)
                }
                (self as f64).log(n as f64) as $t
            }
        }
    };
}

impl_int_trait!(i8);
impl_int_trait!(i16);
impl_int_trait!(i32);
impl_int_trait!(i64);
impl_int_trait!(isize);

impl_uint_trait!(u8);
impl_uint_trait!(u16);
impl_uint_trait!(u32);
impl_uint_trait!(u64);
impl_uint_trait!(usize);

#[cfg(test)]
mod tests {
    use super::IntTraits;

    #[test]
    fn unsigned_sqrt_overall() {
        assert_eq!(63_u8.sqrt(), 7);
        assert_eq!(63_u16.sqrt(), 7);
        assert_eq!(63_u32.sqrt(), 7);
        assert_eq!(63_u64.sqrt(), 7);
        assert_eq!(63_usize.sqrt(), 7);
    }

    #[test]
    fn signed_sqrt_overall() {
        assert_eq!(63_i8.sqrt(), 7);
        assert_eq!(63_i16.sqrt(), 7);
        assert_eq!(63_i32.sqrt(), 7);
        assert_eq!(63_i64.sqrt(), 7);
        assert_eq!(63_isize.sqrt(), 7);
    }

    #[test]
    fn unsigned_cbrt_overall() {
        assert_eq!(247_u8.cbrt(), 6);
        assert_eq!(891_u16.cbrt(), 9);
        assert_eq!(891_u32.cbrt(), 9);
        assert_eq!(891_u64.cbrt(), 9);
        assert_eq!(891_usize.cbrt(), 9);
    }

    #[test]
    fn signed_cbrt_overall() {
        assert_eq!(113_i8.cbrt(), 4);
        assert_eq!(891_i16.cbrt(), 9);
        assert_eq!(891_i32.cbrt(), 9);
        assert_eq!(891_i64.cbrt(), 9);
        assert_eq!(891_isize.cbrt(), 9);
    }

    #[test]
    #[should_panic]
    fn unsigned_zero_log() {
        let _ = 0.log(5);
    }

    #[test]
    #[should_panic]
    fn signed_zero_log() {
        let _ = 0_isize.log(5);
    }

    #[test]
    #[should_panic]
    fn signed_less_zero_log() {
        let _ = (-5).log(5);
    }

    #[test]
    fn zero_sqrt() {
        assert_eq!(0.sqrt(), 0);
    }

    #[test]
    fn zero_cbrt() {
        assert_eq!(0.cbrt(), 0);
    }
}