]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/mod.rs
Rename and namespace `FPCategory`
[rust.git] / src / libstd / num / mod.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Numeric traits and functions for generic mathematics
12 //!
13 //! These are implemented for the primitive numeric types in `std::{u8, u16,
14 //! u32, u64, uint, i8, i16, i32, i64, int, f32, f64}`.
15
16 #![stable]
17 #![allow(missing_docs)]
18
19 #[cfg(test)] use cmp::PartialEq;
20 #[cfg(test)] use fmt::Show;
21 #[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
22 #[cfg(test)] use kinds::Copy;
23
24 pub use core::num::{Num, div_rem, Zero, zero, One, one};
25 pub use core::num::{Unsigned, pow, Bounded};
26 pub use core::num::{Primitive, Int, SignedInt, UnsignedInt};
27 pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive};
28 pub use core::num::{next_power_of_two, is_power_of_two};
29 pub use core::num::{checked_next_power_of_two};
30 pub use core::num::{from_int, from_i8, from_i16, from_i32, from_i64};
31 pub use core::num::{from_uint, from_u8, from_u16, from_u32, from_u64};
32 pub use core::num::{from_f32, from_f64};
33 pub use core::num::{FromStrRadix, from_str_radix};
34 pub use core::num::{FpCategory, Float};
35
36 #[experimental = "may be removed or relocated"]
37 pub mod strconv;
38
39 /// Mathematical operations on primitive floating point numbers.
40 #[unstable = "may be altered to inline the Float trait"]
41 pub trait FloatMath: Float {
42     /// Constructs a floating point number created by multiplying `x` by 2
43     /// raised to the power of `exp`.
44     fn ldexp(x: Self, exp: int) -> Self;
45     /// Breaks the number into a normalized fraction and a base-2 exponent,
46     /// satisfying:
47     ///
48     ///  * `self = x * pow(2, exp)`
49     ///
50     ///  * `0.5 <= abs(x) < 1.0`
51     fn frexp(self) -> (Self, int);
52
53     /// Returns the next representable floating-point value in the direction of
54     /// `other`.
55     fn next_after(self, other: Self) -> Self;
56
57     /// Returns the maximum of the two numbers.
58     fn max(self, other: Self) -> Self;
59     /// Returns the minimum of the two numbers.
60     fn min(self, other: Self) -> Self;
61
62     /// The positive difference of two numbers. Returns `0.0` if the number is
63     /// less than or equal to `other`, otherwise the difference between`self`
64     /// and `other` is returned.
65     fn abs_sub(self, other: Self) -> Self;
66
67     /// Take the cubic root of a number.
68     fn cbrt(self) -> Self;
69     /// Calculate the length of the hypotenuse of a right-angle triangle given
70     /// legs of length `x` and `y`.
71     fn hypot(self, other: Self) -> Self;
72
73     /// Computes the sine of a number (in radians).
74     fn sin(self) -> Self;
75     /// Computes the cosine of a number (in radians).
76     fn cos(self) -> Self;
77     /// Computes the tangent of a number (in radians).
78     fn tan(self) -> Self;
79
80     /// Computes the arcsine of a number. Return value is in radians in
81     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
82     /// [-1, 1].
83     fn asin(self) -> Self;
84     /// Computes the arccosine of a number. Return value is in radians in
85     /// the range [0, pi] or NaN if the number is outside the range
86     /// [-1, 1].
87     fn acos(self) -> Self;
88     /// Computes the arctangent of a number. Return value is in radians in the
89     /// range [-pi/2, pi/2];
90     fn atan(self) -> Self;
91     /// Computes the four quadrant arctangent of a number, `y`, and another
92     /// number `x`. Return value is in radians in the range [-pi, pi].
93     fn atan2(self, other: Self) -> Self;
94     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
95     /// `(sin(x), cos(x))`.
96     fn sin_cos(self) -> (Self, Self);
97
98     /// Returns the exponential of the number, minus 1, in a way that is
99     /// accurate even if the number is close to zero.
100     fn exp_m1(self) -> Self;
101     /// Returns the natural logarithm of the number plus 1 (`ln(1+n)`) more
102     /// accurately than if the operations were performed separately.
103     fn ln_1p(self) -> Self;
104
105     /// Hyperbolic sine function.
106     fn sinh(self) -> Self;
107     /// Hyperbolic cosine function.
108     fn cosh(self) -> Self;
109     /// Hyperbolic tangent function.
110     fn tanh(self) -> Self;
111     /// Inverse hyperbolic sine function.
112     fn asinh(self) -> Self;
113     /// Inverse hyperbolic cosine function.
114     fn acosh(self) -> Self;
115     /// Inverse hyperbolic tangent function.
116     fn atanh(self) -> Self;
117 }
118
119 // DEPRECATED
120
121 #[deprecated = "Use `FloatMath::abs_sub`"]
122 pub fn abs_sub<T: FloatMath>(x: T, y: T) -> T {
123     x.abs_sub(y)
124 }
125
126 /// Helper function for testing numeric operations
127 #[cfg(test)]
128 pub fn test_num<T>(ten: T, two: T) where
129     T: PartialEq + NumCast
130      + Add<T, T> + Sub<T, T>
131      + Mul<T, T> + Div<T, T>
132      + Rem<T, T> + Show
133      + Copy
134 {
135     assert_eq!(ten.add(two),  cast(12i).unwrap());
136     assert_eq!(ten.sub(two),  cast(8i).unwrap());
137     assert_eq!(ten.mul(two),  cast(20i).unwrap());
138     assert_eq!(ten.div(two),  cast(5i).unwrap());
139     assert_eq!(ten.rem(two),  cast(0i).unwrap());
140
141     assert_eq!(ten.add(two),  ten + two);
142     assert_eq!(ten.sub(two),  ten - two);
143     assert_eq!(ten.mul(two),  ten * two);
144     assert_eq!(ten.div(two),  ten / two);
145     assert_eq!(ten.rem(two),  ten % two);
146 }
147
148 #[cfg(test)]
149 mod tests {
150     use prelude::*;
151     use super::*;
152     use i8;
153     use i16;
154     use i32;
155     use i64;
156     use int;
157     use u8;
158     use u16;
159     use u32;
160     use u64;
161     use uint;
162
163     macro_rules! test_cast_20 {
164         ($_20:expr) => ({
165             let _20 = $_20;
166
167             assert_eq!(20u,   _20.to_uint().unwrap());
168             assert_eq!(20u8,  _20.to_u8().unwrap());
169             assert_eq!(20u16, _20.to_u16().unwrap());
170             assert_eq!(20u32, _20.to_u32().unwrap());
171             assert_eq!(20u64, _20.to_u64().unwrap());
172             assert_eq!(20i,   _20.to_int().unwrap());
173             assert_eq!(20i8,  _20.to_i8().unwrap());
174             assert_eq!(20i16, _20.to_i16().unwrap());
175             assert_eq!(20i32, _20.to_i32().unwrap());
176             assert_eq!(20i64, _20.to_i64().unwrap());
177             assert_eq!(20f32, _20.to_f32().unwrap());
178             assert_eq!(20f64, _20.to_f64().unwrap());
179
180             assert_eq!(_20, NumCast::from(20u).unwrap());
181             assert_eq!(_20, NumCast::from(20u8).unwrap());
182             assert_eq!(_20, NumCast::from(20u16).unwrap());
183             assert_eq!(_20, NumCast::from(20u32).unwrap());
184             assert_eq!(_20, NumCast::from(20u64).unwrap());
185             assert_eq!(_20, NumCast::from(20i).unwrap());
186             assert_eq!(_20, NumCast::from(20i8).unwrap());
187             assert_eq!(_20, NumCast::from(20i16).unwrap());
188             assert_eq!(_20, NumCast::from(20i32).unwrap());
189             assert_eq!(_20, NumCast::from(20i64).unwrap());
190             assert_eq!(_20, NumCast::from(20f32).unwrap());
191             assert_eq!(_20, NumCast::from(20f64).unwrap());
192
193             assert_eq!(_20, cast(20u).unwrap());
194             assert_eq!(_20, cast(20u8).unwrap());
195             assert_eq!(_20, cast(20u16).unwrap());
196             assert_eq!(_20, cast(20u32).unwrap());
197             assert_eq!(_20, cast(20u64).unwrap());
198             assert_eq!(_20, cast(20i).unwrap());
199             assert_eq!(_20, cast(20i8).unwrap());
200             assert_eq!(_20, cast(20i16).unwrap());
201             assert_eq!(_20, cast(20i32).unwrap());
202             assert_eq!(_20, cast(20i64).unwrap());
203             assert_eq!(_20, cast(20f32).unwrap());
204             assert_eq!(_20, cast(20f64).unwrap());
205         })
206     }
207
208     #[test] fn test_u8_cast()    { test_cast_20!(20u8)  }
209     #[test] fn test_u16_cast()   { test_cast_20!(20u16) }
210     #[test] fn test_u32_cast()   { test_cast_20!(20u32) }
211     #[test] fn test_u64_cast()   { test_cast_20!(20u64) }
212     #[test] fn test_uint_cast()  { test_cast_20!(20u)   }
213     #[test] fn test_i8_cast()    { test_cast_20!(20i8)  }
214     #[test] fn test_i16_cast()   { test_cast_20!(20i16) }
215     #[test] fn test_i32_cast()   { test_cast_20!(20i32) }
216     #[test] fn test_i64_cast()   { test_cast_20!(20i64) }
217     #[test] fn test_int_cast()   { test_cast_20!(20i)   }
218     #[test] fn test_f32_cast()   { test_cast_20!(20f32) }
219     #[test] fn test_f64_cast()   { test_cast_20!(20f64) }
220
221     #[test]
222     fn test_cast_range_int_min() {
223         assert_eq!(int::MIN.to_int(),  Some(int::MIN as int));
224         assert_eq!(int::MIN.to_i8(),   None);
225         assert_eq!(int::MIN.to_i16(),  None);
226         // int::MIN.to_i32() is word-size specific
227         assert_eq!(int::MIN.to_i64(),  Some(int::MIN as i64));
228         assert_eq!(int::MIN.to_uint(), None);
229         assert_eq!(int::MIN.to_u8(),   None);
230         assert_eq!(int::MIN.to_u16(),  None);
231         assert_eq!(int::MIN.to_u32(),  None);
232         assert_eq!(int::MIN.to_u64(),  None);
233
234         #[cfg(target_word_size = "32")]
235         fn check_word_size() {
236             assert_eq!(int::MIN.to_i32(), Some(int::MIN as i32));
237         }
238
239         #[cfg(target_word_size = "64")]
240         fn check_word_size() {
241             assert_eq!(int::MIN.to_i32(), None);
242         }
243
244         check_word_size();
245     }
246
247     #[test]
248     fn test_cast_range_i8_min() {
249         assert_eq!(i8::MIN.to_int(),  Some(i8::MIN as int));
250         assert_eq!(i8::MIN.to_i8(),   Some(i8::MIN as i8));
251         assert_eq!(i8::MIN.to_i16(),  Some(i8::MIN as i16));
252         assert_eq!(i8::MIN.to_i32(),  Some(i8::MIN as i32));
253         assert_eq!(i8::MIN.to_i64(),  Some(i8::MIN as i64));
254         assert_eq!(i8::MIN.to_uint(), None);
255         assert_eq!(i8::MIN.to_u8(),   None);
256         assert_eq!(i8::MIN.to_u16(),  None);
257         assert_eq!(i8::MIN.to_u32(),  None);
258         assert_eq!(i8::MIN.to_u64(),  None);
259     }
260
261     #[test]
262     fn test_cast_range_i16_min() {
263         assert_eq!(i16::MIN.to_int(),  Some(i16::MIN as int));
264         assert_eq!(i16::MIN.to_i8(),   None);
265         assert_eq!(i16::MIN.to_i16(),  Some(i16::MIN as i16));
266         assert_eq!(i16::MIN.to_i32(),  Some(i16::MIN as i32));
267         assert_eq!(i16::MIN.to_i64(),  Some(i16::MIN as i64));
268         assert_eq!(i16::MIN.to_uint(), None);
269         assert_eq!(i16::MIN.to_u8(),   None);
270         assert_eq!(i16::MIN.to_u16(),  None);
271         assert_eq!(i16::MIN.to_u32(),  None);
272         assert_eq!(i16::MIN.to_u64(),  None);
273     }
274
275     #[test]
276     fn test_cast_range_i32_min() {
277         assert_eq!(i32::MIN.to_int(),  Some(i32::MIN as int));
278         assert_eq!(i32::MIN.to_i8(),   None);
279         assert_eq!(i32::MIN.to_i16(),  None);
280         assert_eq!(i32::MIN.to_i32(),  Some(i32::MIN as i32));
281         assert_eq!(i32::MIN.to_i64(),  Some(i32::MIN as i64));
282         assert_eq!(i32::MIN.to_uint(), None);
283         assert_eq!(i32::MIN.to_u8(),   None);
284         assert_eq!(i32::MIN.to_u16(),  None);
285         assert_eq!(i32::MIN.to_u32(),  None);
286         assert_eq!(i32::MIN.to_u64(),  None);
287     }
288
289     #[test]
290     fn test_cast_range_i64_min() {
291         // i64::MIN.to_int() is word-size specific
292         assert_eq!(i64::MIN.to_i8(),   None);
293         assert_eq!(i64::MIN.to_i16(),  None);
294         assert_eq!(i64::MIN.to_i32(),  None);
295         assert_eq!(i64::MIN.to_i64(),  Some(i64::MIN as i64));
296         assert_eq!(i64::MIN.to_uint(), None);
297         assert_eq!(i64::MIN.to_u8(),   None);
298         assert_eq!(i64::MIN.to_u16(),  None);
299         assert_eq!(i64::MIN.to_u32(),  None);
300         assert_eq!(i64::MIN.to_u64(),  None);
301
302         #[cfg(target_word_size = "32")]
303         fn check_word_size() {
304             assert_eq!(i64::MIN.to_int(), None);
305         }
306
307         #[cfg(target_word_size = "64")]
308         fn check_word_size() {
309             assert_eq!(i64::MIN.to_int(), Some(i64::MIN as int));
310         }
311
312         check_word_size();
313     }
314
315     #[test]
316     fn test_cast_range_int_max() {
317         assert_eq!(int::MAX.to_int(),  Some(int::MAX as int));
318         assert_eq!(int::MAX.to_i8(),   None);
319         assert_eq!(int::MAX.to_i16(),  None);
320         // int::MAX.to_i32() is word-size specific
321         assert_eq!(int::MAX.to_i64(),  Some(int::MAX as i64));
322         assert_eq!(int::MAX.to_u8(),   None);
323         assert_eq!(int::MAX.to_u16(),  None);
324         // int::MAX.to_u32() is word-size specific
325         assert_eq!(int::MAX.to_u64(),  Some(int::MAX as u64));
326
327         #[cfg(target_word_size = "32")]
328         fn check_word_size() {
329             assert_eq!(int::MAX.to_i32(), Some(int::MAX as i32));
330             assert_eq!(int::MAX.to_u32(), Some(int::MAX as u32));
331         }
332
333         #[cfg(target_word_size = "64")]
334         fn check_word_size() {
335             assert_eq!(int::MAX.to_i32(), None);
336             assert_eq!(int::MAX.to_u32(), None);
337         }
338
339         check_word_size();
340     }
341
342     #[test]
343     fn test_cast_range_i8_max() {
344         assert_eq!(i8::MAX.to_int(),  Some(i8::MAX as int));
345         assert_eq!(i8::MAX.to_i8(),   Some(i8::MAX as i8));
346         assert_eq!(i8::MAX.to_i16(),  Some(i8::MAX as i16));
347         assert_eq!(i8::MAX.to_i32(),  Some(i8::MAX as i32));
348         assert_eq!(i8::MAX.to_i64(),  Some(i8::MAX as i64));
349         assert_eq!(i8::MAX.to_uint(), Some(i8::MAX as uint));
350         assert_eq!(i8::MAX.to_u8(),   Some(i8::MAX as u8));
351         assert_eq!(i8::MAX.to_u16(),  Some(i8::MAX as u16));
352         assert_eq!(i8::MAX.to_u32(),  Some(i8::MAX as u32));
353         assert_eq!(i8::MAX.to_u64(),  Some(i8::MAX as u64));
354     }
355
356     #[test]
357     fn test_cast_range_i16_max() {
358         assert_eq!(i16::MAX.to_int(),  Some(i16::MAX as int));
359         assert_eq!(i16::MAX.to_i8(),   None);
360         assert_eq!(i16::MAX.to_i16(),  Some(i16::MAX as i16));
361         assert_eq!(i16::MAX.to_i32(),  Some(i16::MAX as i32));
362         assert_eq!(i16::MAX.to_i64(),  Some(i16::MAX as i64));
363         assert_eq!(i16::MAX.to_uint(), Some(i16::MAX as uint));
364         assert_eq!(i16::MAX.to_u8(),   None);
365         assert_eq!(i16::MAX.to_u16(),  Some(i16::MAX as u16));
366         assert_eq!(i16::MAX.to_u32(),  Some(i16::MAX as u32));
367         assert_eq!(i16::MAX.to_u64(),  Some(i16::MAX as u64));
368     }
369
370     #[test]
371     fn test_cast_range_i32_max() {
372         assert_eq!(i32::MAX.to_int(),  Some(i32::MAX as int));
373         assert_eq!(i32::MAX.to_i8(),   None);
374         assert_eq!(i32::MAX.to_i16(),  None);
375         assert_eq!(i32::MAX.to_i32(),  Some(i32::MAX as i32));
376         assert_eq!(i32::MAX.to_i64(),  Some(i32::MAX as i64));
377         assert_eq!(i32::MAX.to_uint(), Some(i32::MAX as uint));
378         assert_eq!(i32::MAX.to_u8(),   None);
379         assert_eq!(i32::MAX.to_u16(),  None);
380         assert_eq!(i32::MAX.to_u32(),  Some(i32::MAX as u32));
381         assert_eq!(i32::MAX.to_u64(),  Some(i32::MAX as u64));
382     }
383
384     #[test]
385     fn test_cast_range_i64_max() {
386         // i64::MAX.to_int() is word-size specific
387         assert_eq!(i64::MAX.to_i8(),   None);
388         assert_eq!(i64::MAX.to_i16(),  None);
389         assert_eq!(i64::MAX.to_i32(),  None);
390         assert_eq!(i64::MAX.to_i64(),  Some(i64::MAX as i64));
391         // i64::MAX.to_uint() is word-size specific
392         assert_eq!(i64::MAX.to_u8(),   None);
393         assert_eq!(i64::MAX.to_u16(),  None);
394         assert_eq!(i64::MAX.to_u32(),  None);
395         assert_eq!(i64::MAX.to_u64(),  Some(i64::MAX as u64));
396
397         #[cfg(target_word_size = "32")]
398         fn check_word_size() {
399             assert_eq!(i64::MAX.to_int(),  None);
400             assert_eq!(i64::MAX.to_uint(), None);
401         }
402
403         #[cfg(target_word_size = "64")]
404         fn check_word_size() {
405             assert_eq!(i64::MAX.to_int(),  Some(i64::MAX as int));
406             assert_eq!(i64::MAX.to_uint(), Some(i64::MAX as uint));
407         }
408
409         check_word_size();
410     }
411
412     #[test]
413     fn test_cast_range_uint_min() {
414         assert_eq!(uint::MIN.to_int(),  Some(uint::MIN as int));
415         assert_eq!(uint::MIN.to_i8(),   Some(uint::MIN as i8));
416         assert_eq!(uint::MIN.to_i16(),  Some(uint::MIN as i16));
417         assert_eq!(uint::MIN.to_i32(),  Some(uint::MIN as i32));
418         assert_eq!(uint::MIN.to_i64(),  Some(uint::MIN as i64));
419         assert_eq!(uint::MIN.to_uint(), Some(uint::MIN as uint));
420         assert_eq!(uint::MIN.to_u8(),   Some(uint::MIN as u8));
421         assert_eq!(uint::MIN.to_u16(),  Some(uint::MIN as u16));
422         assert_eq!(uint::MIN.to_u32(),  Some(uint::MIN as u32));
423         assert_eq!(uint::MIN.to_u64(),  Some(uint::MIN as u64));
424     }
425
426     #[test]
427     fn test_cast_range_u8_min() {
428         assert_eq!(u8::MIN.to_int(),  Some(u8::MIN as int));
429         assert_eq!(u8::MIN.to_i8(),   Some(u8::MIN as i8));
430         assert_eq!(u8::MIN.to_i16(),  Some(u8::MIN as i16));
431         assert_eq!(u8::MIN.to_i32(),  Some(u8::MIN as i32));
432         assert_eq!(u8::MIN.to_i64(),  Some(u8::MIN as i64));
433         assert_eq!(u8::MIN.to_uint(), Some(u8::MIN as uint));
434         assert_eq!(u8::MIN.to_u8(),   Some(u8::MIN as u8));
435         assert_eq!(u8::MIN.to_u16(),  Some(u8::MIN as u16));
436         assert_eq!(u8::MIN.to_u32(),  Some(u8::MIN as u32));
437         assert_eq!(u8::MIN.to_u64(),  Some(u8::MIN as u64));
438     }
439
440     #[test]
441     fn test_cast_range_u16_min() {
442         assert_eq!(u16::MIN.to_int(),  Some(u16::MIN as int));
443         assert_eq!(u16::MIN.to_i8(),   Some(u16::MIN as i8));
444         assert_eq!(u16::MIN.to_i16(),  Some(u16::MIN as i16));
445         assert_eq!(u16::MIN.to_i32(),  Some(u16::MIN as i32));
446         assert_eq!(u16::MIN.to_i64(),  Some(u16::MIN as i64));
447         assert_eq!(u16::MIN.to_uint(), Some(u16::MIN as uint));
448         assert_eq!(u16::MIN.to_u8(),   Some(u16::MIN as u8));
449         assert_eq!(u16::MIN.to_u16(),  Some(u16::MIN as u16));
450         assert_eq!(u16::MIN.to_u32(),  Some(u16::MIN as u32));
451         assert_eq!(u16::MIN.to_u64(),  Some(u16::MIN as u64));
452     }
453
454     #[test]
455     fn test_cast_range_u32_min() {
456         assert_eq!(u32::MIN.to_int(),  Some(u32::MIN as int));
457         assert_eq!(u32::MIN.to_i8(),   Some(u32::MIN as i8));
458         assert_eq!(u32::MIN.to_i16(),  Some(u32::MIN as i16));
459         assert_eq!(u32::MIN.to_i32(),  Some(u32::MIN as i32));
460         assert_eq!(u32::MIN.to_i64(),  Some(u32::MIN as i64));
461         assert_eq!(u32::MIN.to_uint(), Some(u32::MIN as uint));
462         assert_eq!(u32::MIN.to_u8(),   Some(u32::MIN as u8));
463         assert_eq!(u32::MIN.to_u16(),  Some(u32::MIN as u16));
464         assert_eq!(u32::MIN.to_u32(),  Some(u32::MIN as u32));
465         assert_eq!(u32::MIN.to_u64(),  Some(u32::MIN as u64));
466     }
467
468     #[test]
469     fn test_cast_range_u64_min() {
470         assert_eq!(u64::MIN.to_int(),  Some(u64::MIN as int));
471         assert_eq!(u64::MIN.to_i8(),   Some(u64::MIN as i8));
472         assert_eq!(u64::MIN.to_i16(),  Some(u64::MIN as i16));
473         assert_eq!(u64::MIN.to_i32(),  Some(u64::MIN as i32));
474         assert_eq!(u64::MIN.to_i64(),  Some(u64::MIN as i64));
475         assert_eq!(u64::MIN.to_uint(), Some(u64::MIN as uint));
476         assert_eq!(u64::MIN.to_u8(),   Some(u64::MIN as u8));
477         assert_eq!(u64::MIN.to_u16(),  Some(u64::MIN as u16));
478         assert_eq!(u64::MIN.to_u32(),  Some(u64::MIN as u32));
479         assert_eq!(u64::MIN.to_u64(),  Some(u64::MIN as u64));
480     }
481
482     #[test]
483     fn test_cast_range_uint_max() {
484         assert_eq!(uint::MAX.to_int(),  None);
485         assert_eq!(uint::MAX.to_i8(),   None);
486         assert_eq!(uint::MAX.to_i16(),  None);
487         assert_eq!(uint::MAX.to_i32(),  None);
488         // uint::MAX.to_i64() is word-size specific
489         assert_eq!(uint::MAX.to_u8(),   None);
490         assert_eq!(uint::MAX.to_u16(),  None);
491         // uint::MAX.to_u32() is word-size specific
492         assert_eq!(uint::MAX.to_u64(),  Some(uint::MAX as u64));
493
494         #[cfg(target_word_size = "32")]
495         fn check_word_size() {
496             assert_eq!(uint::MAX.to_u32(), Some(uint::MAX as u32));
497             assert_eq!(uint::MAX.to_i64(), Some(uint::MAX as i64));
498         }
499
500         #[cfg(target_word_size = "64")]
501         fn check_word_size() {
502             assert_eq!(uint::MAX.to_u32(), None);
503             assert_eq!(uint::MAX.to_i64(), None);
504         }
505
506         check_word_size();
507     }
508
509     #[test]
510     fn test_cast_range_u8_max() {
511         assert_eq!(u8::MAX.to_int(),  Some(u8::MAX as int));
512         assert_eq!(u8::MAX.to_i8(),   None);
513         assert_eq!(u8::MAX.to_i16(),  Some(u8::MAX as i16));
514         assert_eq!(u8::MAX.to_i32(),  Some(u8::MAX as i32));
515         assert_eq!(u8::MAX.to_i64(),  Some(u8::MAX as i64));
516         assert_eq!(u8::MAX.to_uint(), Some(u8::MAX as uint));
517         assert_eq!(u8::MAX.to_u8(),   Some(u8::MAX as u8));
518         assert_eq!(u8::MAX.to_u16(),  Some(u8::MAX as u16));
519         assert_eq!(u8::MAX.to_u32(),  Some(u8::MAX as u32));
520         assert_eq!(u8::MAX.to_u64(),  Some(u8::MAX as u64));
521     }
522
523     #[test]
524     fn test_cast_range_u16_max() {
525         assert_eq!(u16::MAX.to_int(),  Some(u16::MAX as int));
526         assert_eq!(u16::MAX.to_i8(),   None);
527         assert_eq!(u16::MAX.to_i16(),  None);
528         assert_eq!(u16::MAX.to_i32(),  Some(u16::MAX as i32));
529         assert_eq!(u16::MAX.to_i64(),  Some(u16::MAX as i64));
530         assert_eq!(u16::MAX.to_uint(), Some(u16::MAX as uint));
531         assert_eq!(u16::MAX.to_u8(),   None);
532         assert_eq!(u16::MAX.to_u16(),  Some(u16::MAX as u16));
533         assert_eq!(u16::MAX.to_u32(),  Some(u16::MAX as u32));
534         assert_eq!(u16::MAX.to_u64(),  Some(u16::MAX as u64));
535     }
536
537     #[test]
538     fn test_cast_range_u32_max() {
539         // u32::MAX.to_int() is word-size specific
540         assert_eq!(u32::MAX.to_i8(),   None);
541         assert_eq!(u32::MAX.to_i16(),  None);
542         assert_eq!(u32::MAX.to_i32(),  None);
543         assert_eq!(u32::MAX.to_i64(),  Some(u32::MAX as i64));
544         assert_eq!(u32::MAX.to_uint(), Some(u32::MAX as uint));
545         assert_eq!(u32::MAX.to_u8(),   None);
546         assert_eq!(u32::MAX.to_u16(),  None);
547         assert_eq!(u32::MAX.to_u32(),  Some(u32::MAX as u32));
548         assert_eq!(u32::MAX.to_u64(),  Some(u32::MAX as u64));
549
550         #[cfg(target_word_size = "32")]
551         fn check_word_size() {
552             assert_eq!(u32::MAX.to_int(),  None);
553         }
554
555         #[cfg(target_word_size = "64")]
556         fn check_word_size() {
557             assert_eq!(u32::MAX.to_int(),  Some(u32::MAX as int));
558         }
559
560         check_word_size();
561     }
562
563     #[test]
564     fn test_cast_range_u64_max() {
565         assert_eq!(u64::MAX.to_int(),  None);
566         assert_eq!(u64::MAX.to_i8(),   None);
567         assert_eq!(u64::MAX.to_i16(),  None);
568         assert_eq!(u64::MAX.to_i32(),  None);
569         assert_eq!(u64::MAX.to_i64(),  None);
570         // u64::MAX.to_uint() is word-size specific
571         assert_eq!(u64::MAX.to_u8(),   None);
572         assert_eq!(u64::MAX.to_u16(),  None);
573         assert_eq!(u64::MAX.to_u32(),  None);
574         assert_eq!(u64::MAX.to_u64(),  Some(u64::MAX as u64));
575
576         #[cfg(target_word_size = "32")]
577         fn check_word_size() {
578             assert_eq!(u64::MAX.to_uint(), None);
579         }
580
581         #[cfg(target_word_size = "64")]
582         fn check_word_size() {
583             assert_eq!(u64::MAX.to_uint(), Some(u64::MAX as uint));
584         }
585
586         check_word_size();
587     }
588
589     #[test]
590     fn test_saturating_add_uint() {
591         use uint::MAX;
592         assert_eq!(3u.saturating_add(5u), 8u);
593         assert_eq!(3u.saturating_add(MAX-1), MAX);
594         assert_eq!(MAX.saturating_add(MAX), MAX);
595         assert_eq!((MAX-2).saturating_add(1), MAX-1);
596     }
597
598     #[test]
599     fn test_saturating_sub_uint() {
600         use uint::MAX;
601         assert_eq!(5u.saturating_sub(3u), 2u);
602         assert_eq!(3u.saturating_sub(5u), 0u);
603         assert_eq!(0u.saturating_sub(1u), 0u);
604         assert_eq!((MAX-1).saturating_sub(MAX), 0);
605     }
606
607     #[test]
608     fn test_saturating_add_int() {
609         use int::{MIN,MAX};
610         assert_eq!(3i.saturating_add(5i), 8i);
611         assert_eq!(3i.saturating_add(MAX-1), MAX);
612         assert_eq!(MAX.saturating_add(MAX), MAX);
613         assert_eq!((MAX-2).saturating_add(1), MAX-1);
614         assert_eq!(3i.saturating_add(-5i), -2i);
615         assert_eq!(MIN.saturating_add(-1i), MIN);
616         assert_eq!((-2i).saturating_add(-MAX), MIN);
617     }
618
619     #[test]
620     fn test_saturating_sub_int() {
621         use int::{MIN,MAX};
622         assert_eq!(3i.saturating_sub(5i), -2i);
623         assert_eq!(MIN.saturating_sub(1i), MIN);
624         assert_eq!((-2i).saturating_sub(MAX), MIN);
625         assert_eq!(3i.saturating_sub(-5i), 8i);
626         assert_eq!(3i.saturating_sub(-(MAX-1)), MAX);
627         assert_eq!(MAX.saturating_sub(-MAX), MAX);
628         assert_eq!((MAX-2).saturating_sub(-1), MAX-1);
629     }
630
631     #[test]
632     fn test_checked_add() {
633         let five_less = uint::MAX - 5;
634         assert_eq!(five_less.checked_add(0), Some(uint::MAX - 5));
635         assert_eq!(five_less.checked_add(1), Some(uint::MAX - 4));
636         assert_eq!(five_less.checked_add(2), Some(uint::MAX - 3));
637         assert_eq!(five_less.checked_add(3), Some(uint::MAX - 2));
638         assert_eq!(five_less.checked_add(4), Some(uint::MAX - 1));
639         assert_eq!(five_less.checked_add(5), Some(uint::MAX));
640         assert_eq!(five_less.checked_add(6), None);
641         assert_eq!(five_less.checked_add(7), None);
642     }
643
644     #[test]
645     fn test_checked_sub() {
646         assert_eq!(5u.checked_sub(0), Some(5));
647         assert_eq!(5u.checked_sub(1), Some(4));
648         assert_eq!(5u.checked_sub(2), Some(3));
649         assert_eq!(5u.checked_sub(3), Some(2));
650         assert_eq!(5u.checked_sub(4), Some(1));
651         assert_eq!(5u.checked_sub(5), Some(0));
652         assert_eq!(5u.checked_sub(6), None);
653         assert_eq!(5u.checked_sub(7), None);
654     }
655
656     #[test]
657     fn test_checked_mul() {
658         let third = uint::MAX / 3;
659         assert_eq!(third.checked_mul(0), Some(0));
660         assert_eq!(third.checked_mul(1), Some(third));
661         assert_eq!(third.checked_mul(2), Some(third * 2));
662         assert_eq!(third.checked_mul(3), Some(third * 3));
663         assert_eq!(third.checked_mul(4), None);
664     }
665
666     macro_rules! test_is_power_of_two {
667         ($test_name:ident, $T:ident) => (
668             fn $test_name() {
669                 #![test]
670                 assert_eq!((0 as $T).is_power_of_two(), false);
671                 assert_eq!((1 as $T).is_power_of_two(), true);
672                 assert_eq!((2 as $T).is_power_of_two(), true);
673                 assert_eq!((3 as $T).is_power_of_two(), false);
674                 assert_eq!((4 as $T).is_power_of_two(), true);
675                 assert_eq!((5 as $T).is_power_of_two(), false);
676                 assert!(($T::MAX / 2 + 1).is_power_of_two(), true);
677             }
678         )
679     }
680
681     test_is_power_of_two!{ test_is_power_of_two_u8, u8 }
682     test_is_power_of_two!{ test_is_power_of_two_u16, u16 }
683     test_is_power_of_two!{ test_is_power_of_two_u32, u32 }
684     test_is_power_of_two!{ test_is_power_of_two_u64, u64 }
685     test_is_power_of_two!{ test_is_power_of_two_uint, uint }
686
687     macro_rules! test_next_power_of_two {
688         ($test_name:ident, $T:ident) => (
689             fn $test_name() {
690                 #![test]
691                 assert_eq!((0 as $T).next_power_of_two(), 1);
692                 let mut next_power = 1;
693                 for i in range::<$T>(1, 40) {
694                      assert_eq!(i.next_power_of_two(), next_power);
695                      if i == next_power { next_power *= 2 }
696                 }
697             }
698         )
699     }
700
701     test_next_power_of_two! { test_next_power_of_two_u8, u8 }
702     test_next_power_of_two! { test_next_power_of_two_u16, u16 }
703     test_next_power_of_two! { test_next_power_of_two_u32, u32 }
704     test_next_power_of_two! { test_next_power_of_two_u64, u64 }
705     test_next_power_of_two! { test_next_power_of_two_uint, uint }
706
707     macro_rules! test_checked_next_power_of_two {
708         ($test_name:ident, $T:ident) => (
709             fn $test_name() {
710                 #![test]
711                 assert_eq!((0 as $T).checked_next_power_of_two(), Some(1));
712                 assert!(($T::MAX / 2).checked_next_power_of_two().is_some());
713                 assert_eq!(($T::MAX - 1).checked_next_power_of_two(), None);
714                 assert_eq!($T::MAX.checked_next_power_of_two(), None);
715                 let mut next_power = 1;
716                 for i in range::<$T>(1, 40) {
717                      assert_eq!(i.checked_next_power_of_two(), Some(next_power));
718                      if i == next_power { next_power *= 2 }
719                 }
720             }
721         )
722     }
723
724     test_checked_next_power_of_two! { test_checked_next_power_of_two_u8, u8 }
725     test_checked_next_power_of_two! { test_checked_next_power_of_two_u16, u16 }
726     test_checked_next_power_of_two! { test_checked_next_power_of_two_u32, u32 }
727     test_checked_next_power_of_two! { test_checked_next_power_of_two_u64, u64 }
728     test_checked_next_power_of_two! { test_checked_next_power_of_two_uint, uint }
729
730     #[deriving(PartialEq, Show)]
731     struct Value { x: int }
732
733     impl ToPrimitive for Value {
734         fn to_i64(&self) -> Option<i64> { self.x.to_i64() }
735         fn to_u64(&self) -> Option<u64> { self.x.to_u64() }
736     }
737
738     impl FromPrimitive for Value {
739         fn from_i64(n: i64) -> Option<Value> { Some(Value { x: n as int }) }
740         fn from_u64(n: u64) -> Option<Value> { Some(Value { x: n as int }) }
741     }
742
743     #[test]
744     fn test_to_primitive() {
745         let value = Value { x: 5 };
746         assert_eq!(value.to_int(),  Some(5));
747         assert_eq!(value.to_i8(),   Some(5));
748         assert_eq!(value.to_i16(),  Some(5));
749         assert_eq!(value.to_i32(),  Some(5));
750         assert_eq!(value.to_i64(),  Some(5));
751         assert_eq!(value.to_uint(), Some(5));
752         assert_eq!(value.to_u8(),   Some(5));
753         assert_eq!(value.to_u16(),  Some(5));
754         assert_eq!(value.to_u32(),  Some(5));
755         assert_eq!(value.to_u64(),  Some(5));
756         assert_eq!(value.to_f32(),  Some(5f32));
757         assert_eq!(value.to_f64(),  Some(5f64));
758     }
759
760     #[test]
761     fn test_from_primitive() {
762         assert_eq!(from_int(5),    Some(Value { x: 5 }));
763         assert_eq!(from_i8(5),     Some(Value { x: 5 }));
764         assert_eq!(from_i16(5),    Some(Value { x: 5 }));
765         assert_eq!(from_i32(5),    Some(Value { x: 5 }));
766         assert_eq!(from_i64(5),    Some(Value { x: 5 }));
767         assert_eq!(from_uint(5),   Some(Value { x: 5 }));
768         assert_eq!(from_u8(5),     Some(Value { x: 5 }));
769         assert_eq!(from_u16(5),    Some(Value { x: 5 }));
770         assert_eq!(from_u32(5),    Some(Value { x: 5 }));
771         assert_eq!(from_u64(5),    Some(Value { x: 5 }));
772         assert_eq!(from_f32(5f32), Some(Value { x: 5 }));
773         assert_eq!(from_f64(5f64), Some(Value { x: 5 }));
774     }
775
776     #[test]
777     fn test_pow() {
778         fn naive_pow<T: Int>(base: T, exp: uint) -> T {
779             let one: T = Int::one();
780             range(0, exp).fold(one, |acc, _| acc * base)
781         }
782         macro_rules! assert_pow {
783             (($num:expr, $exp:expr) => $expected:expr) => {{
784                 let result = $num.pow($exp);
785                 assert_eq!(result, $expected);
786                 assert_eq!(result, naive_pow($num, $exp));
787             }}
788         }
789         assert_pow!((3i,     0 ) => 1);
790         assert_pow!((5i,     1 ) => 5);
791         assert_pow!((-4i,    2 ) => 16);
792         assert_pow!((8i,     3 ) => 512);
793         assert_pow!((2u64,   50) => 1125899906842624);
794     }
795 }
796
797
798 #[cfg(test)]
799 mod bench {
800     extern crate test;
801     use self::test::Bencher;
802     use num::Int;
803     use prelude::*;
804
805     #[bench]
806     fn bench_pow_function(b: &mut Bencher) {
807         let v = Vec::from_fn(1024u, |n| n);
808         b.iter(|| {v.iter().fold(0u, |old, new| old.pow(*new));});
809     }
810 }