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