]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/mod.rs
libstd: set baseline stability levels.
[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 #![experimental]
17 #![allow(missing_doc)]
18
19 use option::Option;
20 use string::String;
21
22 #[cfg(test)] use fmt::Show;
23
24 pub use core::num::{Num, div_rem, Zero, zero, One, one};
25 pub use core::num::{Signed, abs, abs_sub, signum};
26 pub use core::num::{Unsigned, pow, Bounded};
27 pub use core::num::{Primitive, Int, Saturating};
28 pub use core::num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
29 pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive};
30 pub use core::num::{next_power_of_two, is_power_of_two};
31 pub use core::num::{checked_next_power_of_two};
32 pub use core::num::{from_int, from_i8, from_i16, from_i32, from_i64};
33 pub use core::num::{from_uint, from_u8, from_u16, from_u32, from_u64};
34 pub use core::num::{from_f32, from_f64};
35 pub use core::num::{FPCategory, FPNaN, FPInfinite, FPZero, FPSubnormal};
36 pub use core::num::{FPNormal, Float};
37
38 pub mod strconv;
39
40 /// Mathematical operations on primitive floating point numbers.
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     /// Take the cubic root of a number.
63     fn cbrt(self) -> Self;
64     /// Calculate the length of the hypotenuse of a right-angle triangle given
65     /// legs of length `x` and `y`.
66     fn hypot(self, other: Self) -> Self;
67
68     /// Computes the sine of a number (in radians).
69     fn sin(self) -> Self;
70     /// Computes the cosine of a number (in radians).
71     fn cos(self) -> Self;
72     /// Computes the tangent of a number (in radians).
73     fn tan(self) -> Self;
74
75     /// Computes the arcsine of a number. Return value is in radians in
76     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
77     /// [-1, 1].
78     fn asin(self) -> Self;
79     /// Computes the arccosine of a number. Return value is in radians in
80     /// the range [0, pi] or NaN if the number is outside the range
81     /// [-1, 1].
82     fn acos(self) -> Self;
83     /// Computes the arctangent of a number. Return value is in radians in the
84     /// range [-pi/2, pi/2];
85     fn atan(self) -> Self;
86     /// Computes the four quadrant arctangent of a number, `y`, and another
87     /// number `x`. Return value is in radians in the range [-pi, pi].
88     fn atan2(self, other: Self) -> Self;
89     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
90     /// `(sin(x), cos(x))`.
91     fn sin_cos(self) -> (Self, Self);
92
93     /// Returns the exponential of the number, minus 1, in a way that is
94     /// accurate even if the number is close to zero.
95     fn exp_m1(self) -> Self;
96     /// Returns the natural logarithm of the number plus 1 (`ln(1+n)`) more
97     /// accurately than if the operations were performed separately.
98     fn ln_1p(self) -> Self;
99
100     /// Hyperbolic sine function.
101     fn sinh(self) -> Self;
102     /// Hyperbolic cosine function.
103     fn cosh(self) -> Self;
104     /// Hyperbolic tangent function.
105     fn tanh(self) -> Self;
106     /// Inverse hyperbolic sine function.
107     fn asinh(self) -> Self;
108     /// Inverse hyperbolic cosine function.
109     fn acosh(self) -> Self;
110     /// Inverse hyperbolic tangent function.
111     fn atanh(self) -> Self;
112 }
113
114 /// A generic trait for converting a value to a string with a radix (base)
115 #[deprecated = "use fmt::radix"]
116 pub trait ToStrRadix {
117     fn to_str_radix(&self, radix: uint) -> String;
118 }
119
120 /// A generic trait for converting a string with a radix (base) to a value
121 #[experimental = "might need to return Result"]
122 pub trait FromStrRadix {
123     fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
124 }
125
126 /// A utility function that just calls FromStrRadix::from_str_radix.
127 #[experimental = "might need to return Result"]
128 pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
129     FromStrRadix::from_str_radix(str, radix)
130 }
131
132 /// Helper function for testing numeric operations
133 #[cfg(test)]
134 pub fn test_num<T:Num + NumCast + Show>(ten: T, two: T) {
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_next_power_of_two(
667         ($test_name:ident, $T:ident) => (
668             fn $test_name() {
669                 #![test]
670                 assert_eq!(next_power_of_two::<$T>(0), 0);
671                 let mut next_power = 1;
672                 for i in range::<$T>(1, 40) {
673                      assert_eq!(next_power_of_two(i), next_power);
674                      if i == next_power { next_power *= 2 }
675                 }
676             }
677         )
678     )
679
680     test_next_power_of_two!(test_next_power_of_two_u8, u8)
681     test_next_power_of_two!(test_next_power_of_two_u16, u16)
682     test_next_power_of_two!(test_next_power_of_two_u32, u32)
683     test_next_power_of_two!(test_next_power_of_two_u64, u64)
684     test_next_power_of_two!(test_next_power_of_two_uint, uint)
685
686     macro_rules! test_checked_next_power_of_two(
687         ($test_name:ident, $T:ident) => (
688             fn $test_name() {
689                 #![test]
690                 assert_eq!(checked_next_power_of_two::<$T>(0), None);
691                 let mut next_power = 1;
692                 for i in range::<$T>(1, 40) {
693                      assert_eq!(checked_next_power_of_two(i), Some(next_power));
694                      if i == next_power { next_power *= 2 }
695                 }
696                 assert!(checked_next_power_of_two::<$T>($T::MAX / 2).is_some());
697                 assert_eq!(checked_next_power_of_two::<$T>($T::MAX - 1), None);
698                 assert_eq!(checked_next_power_of_two::<$T>($T::MAX), None);
699             }
700         )
701     )
702
703     test_checked_next_power_of_two!(test_checked_next_power_of_two_u8, u8)
704     test_checked_next_power_of_two!(test_checked_next_power_of_two_u16, u16)
705     test_checked_next_power_of_two!(test_checked_next_power_of_two_u32, u32)
706     test_checked_next_power_of_two!(test_checked_next_power_of_two_u64, u64)
707     test_checked_next_power_of_two!(test_checked_next_power_of_two_uint, uint)
708
709     #[deriving(PartialEq, Show)]
710     struct Value { x: int }
711
712     impl ToPrimitive for Value {
713         fn to_i64(&self) -> Option<i64> { self.x.to_i64() }
714         fn to_u64(&self) -> Option<u64> { self.x.to_u64() }
715     }
716
717     impl FromPrimitive for Value {
718         fn from_i64(n: i64) -> Option<Value> { Some(Value { x: n as int }) }
719         fn from_u64(n: u64) -> Option<Value> { Some(Value { x: n as int }) }
720     }
721
722     #[test]
723     fn test_to_primitive() {
724         let value = Value { x: 5 };
725         assert_eq!(value.to_int(),  Some(5));
726         assert_eq!(value.to_i8(),   Some(5));
727         assert_eq!(value.to_i16(),  Some(5));
728         assert_eq!(value.to_i32(),  Some(5));
729         assert_eq!(value.to_i64(),  Some(5));
730         assert_eq!(value.to_uint(), Some(5));
731         assert_eq!(value.to_u8(),   Some(5));
732         assert_eq!(value.to_u16(),  Some(5));
733         assert_eq!(value.to_u32(),  Some(5));
734         assert_eq!(value.to_u64(),  Some(5));
735         assert_eq!(value.to_f32(),  Some(5f32));
736         assert_eq!(value.to_f64(),  Some(5f64));
737     }
738
739     #[test]
740     fn test_from_primitive() {
741         assert_eq!(from_int(5),    Some(Value { x: 5 }));
742         assert_eq!(from_i8(5),     Some(Value { x: 5 }));
743         assert_eq!(from_i16(5),    Some(Value { x: 5 }));
744         assert_eq!(from_i32(5),    Some(Value { x: 5 }));
745         assert_eq!(from_i64(5),    Some(Value { x: 5 }));
746         assert_eq!(from_uint(5),   Some(Value { x: 5 }));
747         assert_eq!(from_u8(5),     Some(Value { x: 5 }));
748         assert_eq!(from_u16(5),    Some(Value { x: 5 }));
749         assert_eq!(from_u32(5),    Some(Value { x: 5 }));
750         assert_eq!(from_u64(5),    Some(Value { x: 5 }));
751         assert_eq!(from_f32(5f32), Some(Value { x: 5 }));
752         assert_eq!(from_f64(5f64), Some(Value { x: 5 }));
753     }
754
755     #[test]
756     fn test_pow() {
757         fn naive_pow<T: One + Mul<T, T>>(base: T, exp: uint) -> T {
758             range(0, exp).fold(one::<T>(), |acc, _| acc * base)
759         }
760         macro_rules! assert_pow(
761             (($num:expr, $exp:expr) => $expected:expr) => {{
762                 let result = pow($num, $exp);
763                 assert_eq!(result, $expected);
764                 assert_eq!(result, naive_pow($num, $exp));
765             }}
766         )
767         assert_pow!((3i,     0 ) => 1);
768         assert_pow!((5i,     1 ) => 5);
769         assert_pow!((-4i,    2 ) => 16);
770         assert_pow!((0.5f64, 5 ) => 0.03125);
771         assert_pow!((8i,     3 ) => 512);
772         assert_pow!((8.0f64, 5 ) => 32768.0);
773         assert_pow!((8.5f64, 5 ) => 44370.53125);
774         assert_pow!((2u64,   50) => 1125899906842624);
775     }
776 }
777
778
779 #[cfg(test)]
780 mod bench {
781     extern crate test;
782     use self::test::Bencher;
783     use num;
784     use prelude::*;
785
786     #[bench]
787     fn bench_pow_function(b: &mut Bencher) {
788         let v = Vec::from_fn(1024u, |n| n);
789         b.iter(|| {v.iter().fold(0u, |old, new| num::pow(old, *new));});
790     }
791 }