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
//! Square root functionality for integral types
//! 
//! These all return f64 for extra precision. Feel free to down-cast if needed

///A trait that allows calculation of square root into a Float(64)
pub trait SquareRoot64 {
    ///Calculates the square root of a number
    fn sqrt(self) -> f64;
}

impl SquareRoot64 for i8 {

    ///Calculates the square root of an i8, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for u8 {

    ///Calculates the square root of a u8, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for i16 {

    ///Calculates the square root of an i16, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for u16 {

    ///Calculates the square root of a u16, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for i32 {

    ///Calculates the square root of an i32, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for u32 {

    ///Calculates the square root of a u32, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for i64 {

    ///Calculates the square root of an i64, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for u64 {

    ///Calculates the square root of a u64, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for isize {

    ///Calculates the square root of an isize, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

impl SquareRoot64 for usize {

    ///Calculates the square root of a usize, returning a f64
    fn sqrt(self) -> f64 {
        (self as f64).sqrt()
    }
}

#[cfg(test)]
mod test {

    use super::SquareRoot64;

    ///Test square root functions on i8s
    #[test]
    fn test_i8_sqrt() {
        let value: i8 = 55;
        assert!(value.sqrt().abs_sub(7.4161984871f64) <= 0.001);
    }

    ///Test square root functions on u8s
    #[test]
    fn test_u8_sqrt() {
        let value: u8 = 214;
        assert!(value.sqrt().abs_sub(14.6287388383f64) <= 0.001);
    }


    ///Test square root functions on i16s
    #[test]
    fn test_i16_sqrt() {
        let value: i16 = 25000;
        assert!(value.sqrt().abs_sub(158.113883008f64) <= 0.001);
    }

    ///Test square root functions on u16s
    #[test]
    fn test_u16_sqrt() {
        let value: u16 = 50000;
        assert!(value.sqrt().abs_sub(223.60679775f64) <= 0.001);
    }

    ///Test square root functions on i32s
    #[test]
    fn test_i32_sqrt() {
        let value: i32 = 100;
        assert!(value.sqrt().abs_sub(10f64) <= 0.001);
    }

    ///Test square root functions on u32s
    #[test]
    fn test_u32_sqrt() {
        let value: u32 = 4000000000;
        assert!(value.sqrt().abs_sub(63245.5532034f64) <= 0.001);
    }

    ///Test square root functions on i64s
    #[test]
    fn test_i64_sqrt() {
        let value: i64 = 7777777777777777;
        assert!(value.sqrt().abs_sub(88191710.3688f64) <= 0.001); //Oh fuck this
    }

    ///Test square root functions on u64s
    #[test]
    fn test_u64_sqrt() {
        let value: u64 = 777777777777777777;
        assert!(value.sqrt().abs_sub(881917103.688f64) <= 0.001); //Oh fuck this
    }

    ///Test square root functions on isizes
    #[test]
    fn test_isize_sqrt() {
        let value: isize = 7777777777777777;
        assert!(value.sqrt().abs_sub(88191710.3688f64) <= 0.001); //Oh fuck this
    }

    ///Test square root functions on usizes
    #[test]
    fn test_usize_sqrt() {
        let value: usize = 777777777777777777;
        assert!(value.sqrt().abs_sub(881917103.688f64) <= 0.001); //Oh fuck this
    }

}