(index<- ) ./libstd/num/num.rs
1 // Copyright 2012-2013 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, float}`.
15
16 #[allow(missing_doc)];
17
18 use clone::{Clone, DeepClone};
19 use cmp::{Eq, ApproxEq, Ord};
20 use ops::{Add, Sub, Mul, Div, Rem, Neg};
21 use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
22 use option::{Option, Some, None};
23
24 pub mod strconv;
25
26 /// The base trait for numeric types
27 pub trait Num: Eq + Zero + One
28 + Neg<Self>
29 + Add<Self,Self>
30 + Sub<Self,Self>
31 + Mul<Self,Self>
32 + Div<Self,Self>
33 + Rem<Self,Self> {}
34
35 pub trait Orderable: Ord {
36 // These should be methods on `Ord`, with overridable default implementations. We don't want
37 // to encumber all implementors of Ord by requiring them to implement these functions, but at
38 // the same time we want to be able to take advantage of the speed of the specific numeric
39 // functions (like the `fmin` and `fmax` intrinsics).
40 fn min(&self, other: &Self) -> Self;
41 fn max(&self, other: &Self) -> Self;
42 fn clamp(&self, mn: &Self, mx: &Self) -> Self;
43 }
44
45 /// Return the smaller number.
46 #[inline(always)] pub fn min<T: Orderable>(x: T, y: T) -> T { x.min(&y) }
47 /// Return the larger number.
48 #[inline(always)] pub fn max<T: Orderable>(x: T, y: T) -> T { x.max(&y) }
49 /// Returns the number constrained within the range `mn <= self <= mx`.
50 #[inline(always)] pub fn clamp<T: Orderable>(value: T, mn: T, mx: T) -> T { value.clamp(&mn, &mx) }
51
52 pub trait Zero {
53 fn zero() -> Self; // FIXME (#5527): This should be an associated constant
54 fn is_zero(&self) -> bool;
55 }
56
57 /// Returns `0` of appropriate type.
58 #[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
59
60 pub trait One {
61 fn one() -> Self; // FIXME (#5527): This should be an associated constant
62 }
63
64 /// Returns `1` of appropriate type.
65 #[inline(always)] pub fn one<T: One>() -> T { One::one() }
66
67 pub trait Signed: Num
68 + Neg<Self> {
69 fn abs(&self) -> Self;
70 fn abs_sub(&self, other: &Self) -> Self;
71 fn signum(&self) -> Self;
72
73 fn is_positive(&self) -> bool;
74 fn is_negative(&self) -> bool;
75 }
76
77 /// Computes the absolute value.
78 ///
79 /// For float, f32, and f64, `NaN` will be returned if the number is `NaN`
80 #[inline(always)] pub fn abs<T: Signed>(value: T) -> T { value.abs() }
81 /// The positive difference of two numbers.
82 ///
83 /// Returns `zero` if the number is less than or equal to `other`,
84 /// otherwise the difference between `self` and `other` is returned.
85 #[inline(always)] pub fn abs_sub<T: Signed>(x: T, y: T) -> T { x.abs_sub(&y) }
86 /// Returns the sign of the number.
87 ///
88 /// For float, f32, f64:
89 /// - `1.0` if the number is positive, `+0.0` or `infinity`
90 /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
91 /// - `NaN` if the number is `NaN`
92 ///
93 /// For int:
94 /// - `0` if the number is zero
95 /// - `1` if the number is positive
96 /// - `-1` if the number is negative
97 #[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
98
99 pub trait Unsigned: Num {}
100
101 /// Times trait
102 ///
103 /// ```rust
104 /// use num::Times;
105 /// let ten = 10 as uint;
106 /// let mut accum = 0;
107 /// do ten.times { accum += 1; }
108 /// ```
109 ///
110 pub trait Times {
111 fn times(&self, it: &fn());
112 }
113
114 pub trait Integer: Num
115 + Orderable
116 + Div<Self,Self>
117 + Rem<Self,Self> {
118 fn div_rem(&self, other: &Self) -> (Self,Self);
119
120 fn div_floor(&self, other: &Self) -> Self;
121 fn mod_floor(&self, other: &Self) -> Self;
122 fn div_mod_floor(&self, other: &Self) -> (Self,Self);
123
124 fn gcd(&self, other: &Self) -> Self;
125 fn lcm(&self, other: &Self) -> Self;
126
127 fn is_multiple_of(&self, other: &Self) -> bool;
128 fn is_even(&self) -> bool;
129 fn is_odd(&self) -> bool;
130 }
131
132 /// Calculates the Greatest Common Divisor (GCD) of the number and `other`.
133 ///
134 /// The result is always positive.
135 #[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
136 /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
137 #[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
138
139 pub trait Round {
140 fn floor(&self) -> Self;
141 fn ceil(&self) -> Self;
142 fn round(&self) -> Self;
143 fn trunc(&self) -> Self;
144 fn fract(&self) -> Self;
145 }
146
147 pub trait Fractional: Num
148 + Orderable
149 + Round
150 + Div<Self,Self> {
151 fn recip(&self) -> Self;
152 }
153
154 pub trait Algebraic {
155 fn pow(&self, n: &Self) -> Self;
156 fn sqrt(&self) -> Self;
157 fn rsqrt(&self) -> Self;
158 fn cbrt(&self) -> Self;
159 fn hypot(&self, other: &Self) -> Self;
160 }
161
162 /// Raise a number to a power.
163 ///
164 /// # Example
165 ///
166 /// ```rust
167 /// let sixteen: float = num::pow(2.0, 4.0);
168 /// assert_eq!(sixteen, 16.0);
169 /// ```
170 #[inline(always)] pub fn pow<T: Algebraic>(value: T, n: T) -> T { value.pow(&n) }
171 /// Take the squre root of a number.
172 #[inline(always)] pub fn sqrt<T: Algebraic>(value: T) -> T { value.sqrt() }
173 /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
174 #[inline(always)] pub fn rsqrt<T: Algebraic>(value: T) -> T { value.rsqrt() }
175 /// Take the cubic root of a number.
176 #[inline(always)] pub fn cbrt<T: Algebraic>(value: T) -> T { value.cbrt() }
177 /// Calculate the length of the hypotenuse of a right-angle triangle given legs of length `x` and
178 /// `y`.
179 #[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }
180
181 pub trait Trigonometric {
182 fn sin(&self) -> Self;
183 fn cos(&self) -> Self;
184 fn tan(&self) -> Self;
185
186 fn asin(&self) -> Self;
187 fn acos(&self) -> Self;
188 fn atan(&self) -> Self;
189
190 fn atan2(&self, other: &Self) -> Self;
191 fn sin_cos(&self) -> (Self, Self);
192 }
193
194 /// Sine function.
195 #[inline(always)] pub fn sin<T: Trigonometric>(value: T) -> T { value.sin() }
196 /// Cosine function.
197 #[inline(always)] pub fn cos<T: Trigonometric>(value: T) -> T { value.cos() }
198 /// Tangent function.
199 #[inline(always)] pub fn tan<T: Trigonometric>(value: T) -> T { value.tan() }
200
201 /// Compute the arcsine of the number.
202 #[inline(always)] pub fn asin<T: Trigonometric>(value: T) -> T { value.asin() }
203 /// Compute the arccosine of the number.
204 #[inline(always)] pub fn acos<T: Trigonometric>(value: T) -> T { value.acos() }
205 /// Compute the arctangent of the number.
206 #[inline(always)] pub fn atan<T: Trigonometric>(value: T) -> T { value.atan() }
207
208 /// Compute the arctangent with 2 arguments.
209 #[inline(always)] pub fn atan2<T: Trigonometric>(x: T, y: T) -> T { x.atan2(&y) }
210 /// Simultaneously computes the sine and cosine of the number.
211 #[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }
212
213 pub trait Exponential {
214 fn exp(&self) -> Self;
215 fn exp2(&self) -> Self;
216
217 fn ln(&self) -> Self;
218 fn log(&self, base: &Self) -> Self;
219 fn log2(&self) -> Self;
220 fn log10(&self) -> Self;
221 }
222
223 /// Returns `e^(value)`, (the exponential function).
224 #[inline(always)] pub fn exp<T: Exponential>(value: T) -> T { value.exp() }
225 /// Returns 2 raised to the power of the number, `2^(value)`.
226 #[inline(always)] pub fn exp2<T: Exponential>(value: T) -> T { value.exp2() }
227
228 /// Returns the natural logarithm of the number.
229 #[inline(always)] pub fn ln<T: Exponential>(value: T) -> T { value.ln() }
230 /// Returns the logarithm of the number with respect to an arbitrary base.
231 #[inline(always)] pub fn log<T: Exponential>(value: T, base: T) -> T { value.log(&base) }
232 /// Returns the base 2 logarithm of the number.
233 #[inline(always)] pub fn log2<T: Exponential>(value: T) -> T { value.log2() }
234 /// Returns the base 10 logarithm of the number.
235 #[inline(always)] pub fn log10<T: Exponential>(value: T) -> T { value.log10() }
236
237 pub trait Hyperbolic: Exponential {
238 fn sinh(&self) -> Self;
239 fn cosh(&self) -> Self;
240 fn tanh(&self) -> Self;
241
242 fn asinh(&self) -> Self;
243 fn acosh(&self) -> Self;
244 fn atanh(&self) -> Self;
245 }
246
247 /// Hyperbolic cosine function.
248 #[inline(always)] pub fn sinh<T: Hyperbolic>(value: T) -> T { value.sinh() }
249 /// Hyperbolic sine function.
250 #[inline(always)] pub fn cosh<T: Hyperbolic>(value: T) -> T { value.cosh() }
251 /// Hyperbolic tangent function.
252 #[inline(always)] pub fn tanh<T: Hyperbolic>(value: T) -> T { value.tanh() }
253
254 /// Inverse hyperbolic sine function.
255 #[inline(always)] pub fn asinh<T: Hyperbolic>(value: T) -> T { value.asinh() }
256 /// Inverse hyperbolic cosine function.
257 #[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() }
258 /// Inverse hyperbolic tangent function.
259 #[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() }
260
261 /// Defines constants and methods common to real numbers
262 pub trait Real: Signed
263 + Fractional
264 + Algebraic
265 + Trigonometric
266 + Hyperbolic {
267 // Common Constants
268 // FIXME (#5527): These should be associated constants
269 fn pi() -> Self;
270 fn two_pi() -> Self;
271 fn frac_pi_2() -> Self;
272 fn frac_pi_3() -> Self;
273 fn frac_pi_4() -> Self;
274 fn frac_pi_6() -> Self;
275 fn frac_pi_8() -> Self;
276 fn frac_1_pi() -> Self;
277 fn frac_2_pi() -> Self;
278 fn frac_2_sqrtpi() -> Self;
279 fn sqrt2() -> Self;
280 fn frac_1_sqrt2() -> Self;
281 fn e() -> Self;
282 fn log2_e() -> Self;
283 fn log10_e() -> Self;
284 fn ln_2() -> Self;
285 fn ln_10() -> Self;
286
287 // Angular conversions
288 fn to_degrees(&self) -> Self;
289 fn to_radians(&self) -> Self;
290 }
291
292 /// Methods that are harder to implement and not commonly used.
293 pub trait RealExt: Real {
294 // FIXME (#5527): usages of `int` should be replaced with an associated
295 // integer type once these are implemented
296
297 // Gamma functions
298 fn lgamma(&self) -> (int, Self);
299 fn tgamma(&self) -> Self;
300
301 // Bessel functions
302 fn j0(&self) -> Self;
303 fn j1(&self) -> Self;
304 fn jn(&self, n: int) -> Self;
305 fn y0(&self) -> Self;
306 fn y1(&self) -> Self;
307 fn yn(&self, n: int) -> Self;
308 }
309
310 /// Collects the bitwise operators under one trait.
311 pub trait Bitwise: Not<Self>
312 + BitAnd<Self,Self>
313 + BitOr<Self,Self>
314 + BitXor<Self,Self>
315 + Shl<Self,Self>
316 + Shr<Self,Self> {}
317
318 pub trait BitCount {
319 fn population_count(&self) -> Self;
320 fn leading_zeros(&self) -> Self;
321 fn trailing_zeros(&self) -> Self;
322 }
323
324 pub trait Bounded {
325 // FIXME (#5527): These should be associated constants
326 fn min_value() -> Self;
327 fn max_value() -> Self;
328 }
329
330 /// Specifies the available operations common to all of Rust's core numeric primitives.
331 /// These may not always make sense from a purely mathematical point of view, but
332 /// may be useful for systems programming.
333 pub trait Primitive: Clone
334 + DeepClone
335 + Num
336 + NumCast
337 + Orderable
338 + Bounded
339 + Neg<Self>
340 + Add<Self,Self>
341 + Sub<Self,Self>
342 + Mul<Self,Self>
343 + Div<Self,Self>
344 + Rem<Self,Self> {
345 // FIXME (#5527): These should be associated constants
346 // FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
347 fn bits(unused_self: Option<Self>) -> uint;
348 fn bytes(unused_self: Option<Self>) -> uint;
349 fn is_signed(unused_self: Option<Self>) -> bool;
350 }
351
352 /// A collection of traits relevant to primitive signed and unsigned integers
353 pub trait Int: Integer
354 + Primitive
355 + Bitwise
356 + BitCount {}
357
358 /// Used for representing the classification of floating point numbers
359 #[deriving(Eq)]
360 pub enum FPCategory {
361 /// "Not a Number", often obtained by dividing by zero
362 FPNaN,
363 /// Positive or negative infinity
364 FPInfinite ,
365 /// Positive or negative zero
366 FPZero,
367 /// De-normalized floating point representation (less precise than `FPNormal`)
368 FPSubnormal,
369 /// A regular floating point number
370 FPNormal,
371 }
372
373 /// Primitive floating point numbers
374 pub trait Float: Real
375 + Signed
376 + Primitive
377 + ApproxEq<Self> {
378 // FIXME (#5527): These should be associated constants
379 fn nan() -> Self;
380 fn infinity() -> Self;
381 fn neg_infinity() -> Self;
382 fn neg_zero() -> Self;
383
384 fn is_nan(&self) -> bool;
385 fn is_infinite(&self) -> bool;
386 fn is_finite(&self) -> bool;
387 fn is_normal(&self) -> bool;
388 fn classify(&self) -> FPCategory;
389
390 // FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
391 fn mantissa_digits(unused_self: Option<Self>) -> uint;
392 fn digits(unused_self: Option<Self>) -> uint;
393 fn epsilon() -> Self;
394 fn min_exp(unused_self: Option<Self>) -> int;
395 fn max_exp(unused_self: Option<Self>) -> int;
396 fn min_10_exp(unused_self: Option<Self>) -> int;
397 fn max_10_exp(unused_self: Option<Self>) -> int;
398
399 fn ldexp(x: Self, exp: int) -> Self;
400 fn frexp(&self) -> (Self, int);
401
402 fn exp_m1(&self) -> Self;
403 fn ln_1p(&self) -> Self;
404 fn mul_add(&self, a: Self, b: Self) -> Self;
405 fn next_after(&self, other: Self) -> Self;
406 }
407
408 /// Returns the exponential of the number, minus `1`, `exp(n) - 1`, in a way
409 /// that is accurate even if the number is close to zero.
410 #[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() }
411 /// Returns the natural logarithm of the number plus `1`, `ln(n + 1)`, more
412 /// accurately than if the operations were performed separately.
413 #[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() }
414 /// Fused multiply-add. Computes `(a * b) + c` with only one rounding error.
415 ///
416 /// This produces a more accurate result with better performance (on some
417 /// architectures) than a separate multiplication operation followed by an add.
418 #[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
419
420 /// A generic trait for converting a value to a number.
421 pub trait ToPrimitive {
422 /// Converts the value of `self` to an `int`.
423 #[inline]
424 fn to_int(&self) -> Option<int> {
425 self.to_i64().and_then(|x| x.to_int())
426 }
427
428 /// Converts the value of `self` to an `i8`.
429 #[inline]
430 fn to_i8(&self) -> Option<i8> {
431 self.to_i64().and_then(|x| x.to_i8())
432 }
433
434 /// Converts the value of `self` to an `i16`.
435 #[inline]
436 fn to_i16(&self) -> Option<i16> {
437 self.to_i64().and_then(|x| x.to_i16())
438 }
439
440 /// Converts the value of `self` to an `i32`.
441 #[inline]
442 fn to_i32(&self) -> Option<i32> {
443 self.to_i64().and_then(|x| x.to_i32())
444 }
445
446 /// Converts the value of `self` to an `i64`.
447 fn to_i64(&self) -> Option<i64>;
448
449 /// Converts the value of `self` to an `uint`.
450 #[inline]
451 fn to_uint(&self) -> Option<uint> {
452 self.to_u64().and_then(|x| x.to_uint())
453 }
454
455 /// Converts the value of `self` to an `u8`.
456 #[inline]
457 fn to_u8(&self) -> Option<u8> {
458 self.to_u64().and_then(|x| x.to_u8())
459 }
460
461 /// Converts the value of `self` to an `u16`.
462 #[inline]
463 fn to_u16(&self) -> Option<u16> {
464 self.to_u64().and_then(|x| x.to_u16())
465 }
466
467 /// Converts the value of `self` to an `u32`.
468 #[inline]
469 fn to_u32(&self) -> Option<u32> {
470 self.to_u64().and_then(|x| x.to_u32())
471 }
472
473 /// Converts the value of `self` to an `u64`.
474 #[inline]
475 fn to_u64(&self) -> Option<u64>;
476
477 /// Converts the value of `self` to an `f32`.
478 #[inline]
479 fn to_f32(&self) -> Option<f32> {
480 self.to_f64().and_then(|x| x.to_f32())
481 }
482
483 /// Converts the value of `self` to an `f64`.
484 #[inline]
485 fn to_f64(&self) -> Option<f64> {
486 self.to_i64().and_then(|x| x.to_f64())
487 }
488 }
489
490 macro_rules! impl_to_primitive_int_to_int(
491 ($SrcT:ty, $DstT:ty) => (
492 {
493 if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
494 Some(*self as $DstT)
495 } else {
496 let n = *self as i64;
497 let min_value: $DstT = Bounded::min_value();
498 let max_value: $DstT = Bounded::max_value();
499 if min_value as i64 <= n && n <= max_value as i64 {
500 Some(*self as $DstT)
501 } else {
502 None
503 }
504 }
505 }
506 )
507 )
508
509 macro_rules! impl_to_primitive_int_to_uint(
510 ($SrcT:ty, $DstT:ty) => (
511 {
512 let zero: $SrcT = Zero::zero();
513 let max_value: $DstT = Bounded::max_value();
514 if zero <= *self && *self as u64 <= max_value as u64 {
515 Some(*self as $DstT)
516 } else {
517 None
518 }
519 }
520 )
521 )
522
523 macro_rules! impl_to_primitive_int(
524 ($T:ty) => (
525 impl ToPrimitive for $T {
526 #[inline]
527 fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int) }
528 #[inline]
529 fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8) }
530 #[inline]
531 fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16) }
532 #[inline]
533 fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32) }
534 #[inline]
535 fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64) }
536
537 #[inline]
538 fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint) }
539 #[inline]
540 fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8) }
541 #[inline]
542 fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16) }
543 #[inline]
544 fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32) }
545 #[inline]
546 fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64) }
547
548 #[inline]
549 fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
550 #[inline]
551 fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
552 }
553 )
554 )
555
556 impl_to_primitive_int!(int)
557 impl_to_primitive_int!(i8)
558 impl_to_primitive_int!(i16)
559 impl_to_primitive_int!(i32)
560 impl_to_primitive_int!(i64)
561
562 macro_rules! impl_to_primitive_uint_to_int(
563 ($DstT:ty) => (
564 {
565 let max_value: $DstT = Bounded::max_value();
566 if *self as u64 <= max_value as u64 {
567 Some(*self as $DstT)
568 } else {
569 None
570 }
571 }
572 )
573 )
574
575 macro_rules! impl_to_primitive_uint_to_uint(
576 ($SrcT:ty, $DstT:ty) => (
577 {
578 if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
579 Some(*self as $DstT)
580 } else {
581 let zero: $SrcT = Zero::zero();
582 let max_value: $DstT = Bounded::max_value();
583 if zero <= *self && *self as u64 <= max_value as u64 {
584 Some(*self as $DstT)
585 } else {
586 None
587 }
588 }
589 }
590 )
591 )
592
593 macro_rules! impl_to_primitive_uint(
594 ($T:ty) => (
595 impl ToPrimitive for $T {
596 #[inline]
597 fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int) }
598 #[inline]
599 fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8) }
600 #[inline]
601 fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16) }
602 #[inline]
603 fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32) }
604 #[inline]
605 fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64) }
606
607 #[inline]
608 fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint) }
609 #[inline]
610 fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8) }
611 #[inline]
612 fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16) }
613 #[inline]
614 fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32) }
615 #[inline]
616 fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64) }
617
618 #[inline]
619 fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
620 #[inline]
621 fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
622 }
623 )
624 )
625
626 impl_to_primitive_uint!(uint)
627 impl_to_primitive_uint!(u8)
628 impl_to_primitive_uint!(u16)
629 impl_to_primitive_uint!(u32)
630 impl_to_primitive_uint!(u64)
631
632 macro_rules! impl_to_primitive_float_to_float(
633 ($SrcT:ty, $DstT:ty) => (
634 if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
635 Some(*self as $DstT)
636 } else {
637 let n = *self as f64;
638 let max_value: $SrcT = Bounded::max_value();
639 if -max_value as f64 <= n && n <= max_value as f64 {
640 Some(*self as $DstT)
641 } else {
642 None
643 }
644 }
645 )
646 )
647
648 macro_rules! impl_to_primitive_float(
649 ($T:ty) => (
650 impl ToPrimitive for $T {
651 #[inline]
652 fn to_int(&self) -> Option<int> { Some(*self as int) }
653 #[inline]
654 fn to_i8(&self) -> Option<i8> { Some(*self as i8) }
655 #[inline]
656 fn to_i16(&self) -> Option<i16> { Some(*self as i16) }
657 #[inline]
658 fn to_i32(&self) -> Option<i32> { Some(*self as i32) }
659 #[inline]
660 fn to_i64(&self) -> Option<i64> { Some(*self as i64) }
661
662 #[inline]
663 fn to_uint(&self) -> Option<uint> { Some(*self as uint) }
664 #[inline]
665 fn to_u8(&self) -> Option<u8> { Some(*self as u8) }
666 #[inline]
667 fn to_u16(&self) -> Option<u16> { Some(*self as u16) }
668 #[inline]
669 fn to_u32(&self) -> Option<u32> { Some(*self as u32) }
670 #[inline]
671 fn to_u64(&self) -> Option<u64> { Some(*self as u64) }
672
673 #[inline]
674 fn to_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32) }
675 #[inline]
676 fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64) }
677 }
678 )
679 )
680
681 impl_to_primitive_float!(f32)
682 impl_to_primitive_float!(f64)
683
684 /// A generic trait for converting a number to a value.
685 pub trait FromPrimitive {
686 /// Convert an `int` to return an optional value of this type. If the
687 /// value cannot be represented by this value, the `None` is returned.
688 #[inline]
689 fn from_int(n: int) -> Option<Self> {
690 FromPrimitive::from_i64(n as i64)
691 }
692
693 /// Convert an `i8` to return an optional value of this type. If the
694 /// type cannot be represented by this value, the `None` is returned.
695 #[inline]
696 fn from_i8(n: i8) -> Option<Self> {
697 FromPrimitive::from_i64(n as i64)
698 }
699
700 /// Convert an `i16` to return an optional value of this type. If the
701 /// type cannot be represented by this value, the `None` is returned.
702 #[inline]
703 fn from_i16(n: i16) -> Option<Self> {
704 FromPrimitive::from_i64(n as i64)
705 }
706
707 /// Convert an `i32` to return an optional value of this type. If the
708 /// type cannot be represented by this value, the `None` is returned.
709 #[inline]
710 fn from_i32(n: i32) -> Option<Self> {
711 FromPrimitive::from_i64(n as i64)
712 }
713
714 /// Convert an `i64` to return an optional value of this type. If the
715 /// type cannot be represented by this value, the `None` is returned.
716 fn from_i64(n: i64) -> Option<Self>;
717
718 /// Convert an `uint` to return an optional value of this type. If the
719 /// type cannot be represented by this value, the `None` is returned.
720 #[inline]
721 fn from_uint(n: uint) -> Option<Self> {
722 FromPrimitive::from_u64(n as u64)
723 }
724
725 /// Convert an `u8` to return an optional value of this type. If the
726 /// type cannot be represented by this value, the `None` is returned.
727 #[inline]
728 fn from_u8(n: u8) -> Option<Self> {
729 FromPrimitive::from_u64(n as u64)
730 }
731
732 /// Convert an `u16` to return an optional value of this type. If the
733 /// type cannot be represented by this value, the `None` is returned.
734 #[inline]
735 fn from_u16(n: u16) -> Option<Self> {
736 FromPrimitive::from_u64(n as u64)
737 }
738
739 /// Convert an `u32` to return an optional value of this type. If the
740 /// type cannot be represented by this value, the `None` is returned.
741 #[inline]
742 fn from_u32(n: u32) -> Option<Self> {
743 FromPrimitive::from_u64(n as u64)
744 }
745
746 /// Convert an `u64` to return an optional value of this type. If the
747 /// type cannot be represented by this value, the `None` is returned.
748 fn from_u64(n: u64) -> Option<Self>;
749
750 /// Convert a `f32` to return an optional value of this type. If the
751 /// type cannot be represented by this value, the `None` is returned.
752 #[inline]
753 fn from_f32(n: f32) -> Option<Self> {
754 FromPrimitive::from_f64(n as f64)
755 }
756
757 /// Convert a `f64` to return an optional value of this type. If the
758 /// type cannot be represented by this value, the `None` is returned.
759 #[inline]
760 fn from_f64(n: f64) -> Option<Self> {
761 FromPrimitive::from_i64(n as i64)
762 }
763 }
764
765 /// A utility function that just calls `FromPrimitive::from_int`.
766 pub fn from_int<A: FromPrimitive>(n: int) -> Option<A> {
767 FromPrimitive::from_int(n)
768 }
769
770 /// A utility function that just calls `FromPrimitive::from_i8`.
771 pub fn from_i8<A: FromPrimitive>(n: i8) -> Option<A> {
772 FromPrimitive::from_i8(n)
773 }
774
775 /// A utility function that just calls `FromPrimitive::from_i16`.
776 pub fn from_i16<A: FromPrimitive>(n: i16) -> Option<A> {
777 FromPrimitive::from_i16(n)
778 }
779
780 /// A utility function that just calls `FromPrimitive::from_i32`.
781 pub fn from_i32<A: FromPrimitive>(n: i32) -> Option<A> {
782 FromPrimitive::from_i32(n)
783 }
784
785 /// A utility function that just calls `FromPrimitive::from_i64`.
786 pub fn from_i64<A: FromPrimitive>(n: i64) -> Option<A> {
787 FromPrimitive::from_i64(n)
788 }
789
790 /// A utility function that just calls `FromPrimitive::from_uint`.
791 pub fn from_uint<A: FromPrimitive>(n: uint) -> Option<A> {
792 FromPrimitive::from_uint(n)
793 }
794
795 /// A utility function that just calls `FromPrimitive::from_u8`.
796 pub fn from_u8<A: FromPrimitive>(n: u8) -> Option<A> {
797 FromPrimitive::from_u8(n)
798 }
799
800 /// A utility function that just calls `FromPrimitive::from_u16`.
801 pub fn from_u16<A: FromPrimitive>(n: u16) -> Option<A> {
802 FromPrimitive::from_u16(n)
803 }
804
805 /// A utility function that just calls `FromPrimitive::from_u32`.
806 pub fn from_u32<A: FromPrimitive>(n: u32) -> Option<A> {
807 FromPrimitive::from_u32(n)
808 }
809
810 /// A utility function that just calls `FromPrimitive::from_u64`.
811 pub fn from_u64<A: FromPrimitive>(n: u64) -> Option<A> {
812 FromPrimitive::from_u64(n)
813 }
814
815 /// A utility function that just calls `FromPrimitive::from_f32`.
816 pub fn from_f32<A: FromPrimitive>(n: f32) -> Option<A> {
817 FromPrimitive::from_f32(n)
818 }
819
820 /// A utility function that just calls `FromPrimitive::from_f64`.
821 pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
822 FromPrimitive::from_f64(n)
823 }
824
825 macro_rules! impl_from_primitive(
826 ($T:ty, $to_ty:expr) => (
827 impl FromPrimitive for $T {
828 #[inline] fn from_int(n: int) -> Option<$T> { $to_ty }
829 #[inline] fn from_i8(n: i8) -> Option<$T> { $to_ty }
830 #[inline] fn from_i16(n: i16) -> Option<$T> { $to_ty }
831 #[inline] fn from_i32(n: i32) -> Option<$T> { $to_ty }
832 #[inline] fn from_i64(n: i64) -> Option<$T> { $to_ty }
833
834 #[inline] fn from_uint(n: uint) -> Option<$T> { $to_ty }
835 #[inline] fn from_u8(n: u8) -> Option<$T> { $to_ty }
836 #[inline] fn from_u16(n: u16) -> Option<$T> { $to_ty }
837 #[inline] fn from_u32(n: u32) -> Option<$T> { $to_ty }
838 #[inline] fn from_u64(n: u64) -> Option<$T> { $to_ty }
839
840 #[inline] fn from_f32(n: f32) -> Option<$T> { $to_ty }
841 #[inline] fn from_f64(n: f64) -> Option<$T> { $to_ty }
842 }
843 )
844 )
845
846 impl_from_primitive!(int, n.to_int())
847 impl_from_primitive!(i8, n.to_i8())
848 impl_from_primitive!(i16, n.to_i16())
849 impl_from_primitive!(i32, n.to_i32())
850 impl_from_primitive!(i64, n.to_i64())
851 impl_from_primitive!(uint, n.to_uint())
852 impl_from_primitive!(u8, n.to_u8())
853 impl_from_primitive!(u16, n.to_u16())
854 impl_from_primitive!(u32, n.to_u32())
855 impl_from_primitive!(u64, n.to_u64())
856 impl_from_primitive!(f32, n.to_f32())
857 impl_from_primitive!(f64, n.to_f64())
858
859 /// Cast from one machine scalar to another.
860 ///
861 /// # Example
862 ///
863 /// ```
864 /// let twenty: f32 = num::cast(0x14).unwrap();
865 /// assert_eq!(twenty, 20f32);
866 /// ```
867 ///
868 #[inline]
869 pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
870 NumCast::from(n)
871 }
872
873 /// An interface for casting between machine scalars
874 pub trait NumCast: ToPrimitive {
875 fn from<T: ToPrimitive>(n: T) -> Option<Self>;
876 }
877
878 macro_rules! impl_num_cast(
879 ($T:ty, $conv:ident) => (
880 impl NumCast for $T {
881 #[inline]
882 fn from<N: ToPrimitive>(n: N) -> Option<$T> {
883 // `$conv` could be generated using `concat_idents!`, but that
884 // macro seems to be broken at the moment
885 n.$conv()
886 }
887 }
888 )
889 )
890
891 impl_num_cast!(u8, to_u8)
892 impl_num_cast!(u16, to_u16)
893 impl_num_cast!(u32, to_u32)
894 impl_num_cast!(u64, to_u64)
895 impl_num_cast!(uint, to_uint)
896 impl_num_cast!(i8, to_i8)
897 impl_num_cast!(i16, to_i16)
898 impl_num_cast!(i32, to_i32)
899 impl_num_cast!(i64, to_i64)
900 impl_num_cast!(int, to_int)
901 impl_num_cast!(f32, to_f32)
902 impl_num_cast!(f64, to_f64)
903
904 pub trait ToStrRadix {
905 fn to_str_radix(&self, radix: uint) -> ~str;
906 }
907
908 pub trait FromStrRadix {
909 fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
910 }
911
912 /// A utility function that just calls FromStrRadix::from_str_radix.
913 pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
914 FromStrRadix::from_str_radix(str, radix)
915 }
916
917 /// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
918 ///
919 /// Returns `radix^pow` as `T`.
920 ///
921 /// Note:
922 /// Also returns `1` for `0^0`, despite that technically being an
923 /// undefined number. The reason for this is twofold:
924 /// - If code written to use this function cares about that special case, it's
925 /// probably going to catch it before making the call.
926 /// - If code written to use this function doesn't care about it, it's
927 /// probably assuming that `x^0` always equals `1`.
928 ///
929 pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
930 let _0: T = Zero::zero();
931 let _1: T = One::one();
932
933 if pow == 0u { return _1; }
934 if radix == 0u { return _0; }
935 let mut my_pow = pow;
936 let mut total = _1;
937 let mut multiplier = cast(radix).unwrap();
938 while (my_pow > 0u) {
939 if my_pow % 2u == 1u {
940 total = total * multiplier;
941 }
942 my_pow = my_pow / 2u;
943 multiplier = multiplier * multiplier;
944 }
945 total
946 }
947
948 impl<T: Zero + 'static> Zero for @mut T {
949 fn zero() -> @mut T { @mut Zero::zero() }
950 fn is_zero(&self) -> bool { (**self).is_zero() }
951 }
952
953 impl<T: Zero + 'static> Zero for @T {
954 fn zero() -> @T { @Zero::zero() }
955 fn is_zero(&self) -> bool { (**self).is_zero() }
956 }
957
958 impl<T: Zero> Zero for ~T {
959 fn zero() -> ~T { ~Zero::zero() }
960 fn is_zero(&self) -> bool { (**self).is_zero() }
961 }
962
963 /// Saturating math operations
964 pub trait Saturating {
965 /// Saturating addition operator.
966 /// Returns a+b, saturating at the numeric bounds instead of overflowing.
967 fn saturating_add(self, v: Self) -> Self;
968
969 /// Saturating subtraction operator.
970 /// Returns a-b, saturating at the numeric bounds instead of overflowing.
971 fn saturating_sub(self, v: Self) -> Self;
972 }
973
974 impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
975 #[inline]
976 fn saturating_add(self, v: T) -> T {
977 match self.checked_add(&v) {
978 Some(x) => x,
979 None => if v >= Zero::zero() {
980 Bounded::max_value()
981 } else {
982 Bounded::min_value()
983 }
984 }
985 }
986
987 #[inline]
988 fn saturating_sub(self, v: T) -> T {
989 match self.checked_sub(&v) {
990 Some(x) => x,
991 None => if v >= Zero::zero() {
992 Bounded::min_value()
993 } else {
994 Bounded::max_value()
995 }
996 }
997 }
998 }
999
1000 pub trait CheckedAdd: Add<Self, Self> {
1001 fn checked_add(&self, v: &Self) -> Option<Self>;
1002 }
1003
1004 pub trait CheckedSub: Sub<Self, Self> {
1005 fn checked_sub(&self, v: &Self) -> Option<Self>;
1006 }
1007
1008 pub trait CheckedMul: Mul<Self, Self> {
1009 fn checked_mul(&self, v: &Self) -> Option<Self>;
1010 }
1011
1012 pub trait CheckedDiv: Div<Self, Self> {
1013 fn checked_div(&self, v: &Self) -> Option<Self>;
1014 }
1015
1016 /// Helper function for testing numeric operations
1017 #[cfg(test)]
1018 pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
1019 assert_eq!(ten.add(&two), cast(12).unwrap());
1020 assert_eq!(ten.sub(&two), cast(8).unwrap());
1021 assert_eq!(ten.mul(&two), cast(20).unwrap());
1022 assert_eq!(ten.div(&two), cast(5).unwrap());
1023 assert_eq!(ten.rem(&two), cast(0).unwrap());
1024
1025 assert_eq!(ten.add(&two), ten + two);
1026 assert_eq!(ten.sub(&two), ten - two);
1027 assert_eq!(ten.mul(&two), ten * two);
1028 assert_eq!(ten.div(&two), ten / two);
1029 assert_eq!(ten.rem(&two), ten % two);
1030 }
1031
1032 #[cfg(test)]
1033 mod tests {
1034 use prelude::*;
1035 use super::*;
1036 use i8;
1037 use i16;
1038 use i32;
1039 use i64;
1040 use int;
1041 use u8;
1042 use u16;
1043 use u32;
1044 use u64;
1045 use uint;
1046
1047 macro_rules! test_cast_20(
1048 ($_20:expr) => ({
1049 let _20 = $_20;
1050
1051 assert_eq!(20u, _20.to_uint().unwrap());
1052 assert_eq!(20u8, _20.to_u8().unwrap());
1053 assert_eq!(20u16, _20.to_u16().unwrap());
1054 assert_eq!(20u32, _20.to_u32().unwrap());
1055 assert_eq!(20u64, _20.to_u64().unwrap());
1056 assert_eq!(20i, _20.to_int().unwrap());
1057 assert_eq!(20i8, _20.to_i8().unwrap());
1058 assert_eq!(20i16, _20.to_i16().unwrap());
1059 assert_eq!(20i32, _20.to_i32().unwrap());
1060 assert_eq!(20i64, _20.to_i64().unwrap());
1061 assert_eq!(20f32, _20.to_f32().unwrap());
1062 assert_eq!(20f64, _20.to_f64().unwrap());
1063
1064 assert_eq!(_20, NumCast::from(20u).unwrap());
1065 assert_eq!(_20, NumCast::from(20u8).unwrap());
1066 assert_eq!(_20, NumCast::from(20u16).unwrap());
1067 assert_eq!(_20, NumCast::from(20u32).unwrap());
1068 assert_eq!(_20, NumCast::from(20u64).unwrap());
1069 assert_eq!(_20, NumCast::from(20i).unwrap());
1070 assert_eq!(_20, NumCast::from(20i8).unwrap());
1071 assert_eq!(_20, NumCast::from(20i16).unwrap());
1072 assert_eq!(_20, NumCast::from(20i32).unwrap());
1073 assert_eq!(_20, NumCast::from(20i64).unwrap());
1074 assert_eq!(_20, NumCast::from(20f32).unwrap());
1075 assert_eq!(_20, NumCast::from(20f64).unwrap());
1076
1077 assert_eq!(_20, cast(20u).unwrap());
1078 assert_eq!(_20, cast(20u8).unwrap());
1079 assert_eq!(_20, cast(20u16).unwrap());
1080 assert_eq!(_20, cast(20u32).unwrap());
1081 assert_eq!(_20, cast(20u64).unwrap());
1082 assert_eq!(_20, cast(20i).unwrap());
1083 assert_eq!(_20, cast(20i8).unwrap());
1084 assert_eq!(_20, cast(20i16).unwrap());
1085 assert_eq!(_20, cast(20i32).unwrap());
1086 assert_eq!(_20, cast(20i64).unwrap());
1087 assert_eq!(_20, cast(20f32).unwrap());
1088 assert_eq!(_20, cast(20f64).unwrap());
1089 })
1090 )
1091
1092 #[test] fn test_u8_cast() { test_cast_20!(20u8) }
1093 #[test] fn test_u16_cast() { test_cast_20!(20u16) }
1094 #[test] fn test_u32_cast() { test_cast_20!(20u32) }
1095 #[test] fn test_u64_cast() { test_cast_20!(20u64) }
1096 #[test] fn test_uint_cast() { test_cast_20!(20u) }
1097 #[test] fn test_i8_cast() { test_cast_20!(20i8) }
1098 #[test] fn test_i16_cast() { test_cast_20!(20i16) }
1099 #[test] fn test_i32_cast() { test_cast_20!(20i32) }
1100 #[test] fn test_i64_cast() { test_cast_20!(20i64) }
1101 #[test] fn test_int_cast() { test_cast_20!(20i) }
1102 #[test] fn test_f32_cast() { test_cast_20!(20f32) }
1103 #[test] fn test_f64_cast() { test_cast_20!(20f64) }
1104
1105 #[test]
1106 fn test_cast_range_int_min() {
1107 assert_eq!(int::min_value.to_int(), Some(int::min_value as int));
1108 assert_eq!(int::min_value.to_i8(), None);
1109 assert_eq!(int::min_value.to_i16(), None);
1110 // int::min_value.to_i32() is word-size specific
1111 assert_eq!(int::min_value.to_i64(), Some(int::min_value as i64));
1112 assert_eq!(int::min_value.to_uint(), None);
1113 assert_eq!(int::min_value.to_u8(), None);
1114 assert_eq!(int::min_value.to_u16(), None);
1115 assert_eq!(int::min_value.to_u32(), None);
1116 assert_eq!(int::min_value.to_u64(), None);
1117
1118 #[cfg(target_word_size = "32")]
1119 fn check_word_size() {
1120 assert_eq!(int::min_value.to_i32(), Some(int::min_value as i32));
1121 }
1122
1123 #[cfg(target_word_size = "64")]
1124 fn check_word_size() {
1125 assert_eq!(int::min_value.to_i32(), None);
1126 }
1127
1128 check_word_size();
1129 }
1130
1131 #[test]
1132 fn test_cast_range_i8_min() {
1133 assert_eq!(i8::min_value.to_int(), Some(i8::min_value as int));
1134 assert_eq!(i8::min_value.to_i8(), Some(i8::min_value as i8));
1135 assert_eq!(i8::min_value.to_i16(), Some(i8::min_value as i16));
1136 assert_eq!(i8::min_value.to_i32(), Some(i8::min_value as i32));
1137 assert_eq!(i8::min_value.to_i64(), Some(i8::min_value as i64));
1138 assert_eq!(i8::min_value.to_uint(), None);
1139 assert_eq!(i8::min_value.to_u8(), None);
1140 assert_eq!(i8::min_value.to_u16(), None);
1141 assert_eq!(i8::min_value.to_u32(), None);
1142 assert_eq!(i8::min_value.to_u64(), None);
1143 }
1144
1145 #[test]
1146 fn test_cast_range_i16_min() {
1147 assert_eq!(i16::min_value.to_int(), Some(i16::min_value as int));
1148 assert_eq!(i16::min_value.to_i8(), None);
1149 assert_eq!(i16::min_value.to_i16(), Some(i16::min_value as i16));
1150 assert_eq!(i16::min_value.to_i32(), Some(i16::min_value as i32));
1151 assert_eq!(i16::min_value.to_i64(), Some(i16::min_value as i64));
1152 assert_eq!(i16::min_value.to_uint(), None);
1153 assert_eq!(i16::min_value.to_u8(), None);
1154 assert_eq!(i16::min_value.to_u16(), None);
1155 assert_eq!(i16::min_value.to_u32(), None);
1156 assert_eq!(i16::min_value.to_u64(), None);
1157 }
1158
1159 #[test]
1160 fn test_cast_range_i32_min() {
1161 assert_eq!(i32::min_value.to_int(), Some(i32::min_value as int));
1162 assert_eq!(i32::min_value.to_i8(), None);
1163 assert_eq!(i32::min_value.to_i16(), None);
1164 assert_eq!(i32::min_value.to_i32(), Some(i32::min_value as i32));
1165 assert_eq!(i32::min_value.to_i64(), Some(i32::min_value as i64));
1166 assert_eq!(i32::min_value.to_uint(), None);
1167 assert_eq!(i32::min_value.to_u8(), None);
1168 assert_eq!(i32::min_value.to_u16(), None);
1169 assert_eq!(i32::min_value.to_u32(), None);
1170 assert_eq!(i32::min_value.to_u64(), None);
1171 }
1172
1173 #[test]
1174 fn test_cast_range_i64_min() {
1175 // i64::min_value.to_int() is word-size specific
1176 assert_eq!(i64::min_value.to_i8(), None);
1177 assert_eq!(i64::min_value.to_i16(), None);
1178 assert_eq!(i64::min_value.to_i32(), None);
1179 assert_eq!(i64::min_value.to_i64(), Some(i64::min_value as i64));
1180 assert_eq!(i64::min_value.to_uint(), None);
1181 assert_eq!(i64::min_value.to_u8(), None);
1182 assert_eq!(i64::min_value.to_u16(), None);
1183 assert_eq!(i64::min_value.to_u32(), None);
1184 assert_eq!(i64::min_value.to_u64(), None);
1185
1186 #[cfg(target_word_size = "32")]
1187 fn check_word_size() {
1188 assert_eq!(i64::min_value.to_int(), None);
1189 }
1190
1191 #[cfg(target_word_size = "64")]
1192 fn check_word_size() {
1193 assert_eq!(i64::min_value.to_int(), Some(i64::min_value as int));
1194 }
1195
1196 check_word_size();
1197 }
1198
1199 #[test]
1200 fn test_cast_range_int_max() {
1201 assert_eq!(int::max_value.to_int(), Some(int::max_value as int));
1202 assert_eq!(int::max_value.to_i8(), None);
1203 assert_eq!(int::max_value.to_i16(), None);
1204 // int::max_value.to_i32() is word-size specific
1205 assert_eq!(int::max_value.to_i64(), Some(int::max_value as i64));
1206 assert_eq!(int::max_value.to_u8(), None);
1207 assert_eq!(int::max_value.to_u16(), None);
1208 // int::max_value.to_u32() is word-size specific
1209 assert_eq!(int::max_value.to_u64(), Some(int::max_value as u64));
1210
1211 #[cfg(target_word_size = "32")]
1212 fn check_word_size() {
1213 assert_eq!(int::max_value.to_i32(), Some(int::max_value as i32));
1214 assert_eq!(int::max_value.to_u32(), Some(int::max_value as u32));
1215 }
1216
1217 #[cfg(target_word_size = "64")]
1218 fn check_word_size() {
1219 assert_eq!(int::max_value.to_i32(), None);
1220 assert_eq!(int::max_value.to_u32(), None);
1221 }
1222
1223 check_word_size();
1224 }
1225
1226 #[test]
1227 fn test_cast_range_i8_max() {
1228 assert_eq!(i8::max_value.to_int(), Some(i8::max_value as int));
1229 assert_eq!(i8::max_value.to_i8(), Some(i8::max_value as i8));
1230 assert_eq!(i8::max_value.to_i16(), Some(i8::max_value as i16));
1231 assert_eq!(i8::max_value.to_i32(), Some(i8::max_value as i32));
1232 assert_eq!(i8::max_value.to_i64(), Some(i8::max_value as i64));
1233 assert_eq!(i8::max_value.to_uint(), Some(i8::max_value as uint));
1234 assert_eq!(i8::max_value.to_u8(), Some(i8::max_value as u8));
1235 assert_eq!(i8::max_value.to_u16(), Some(i8::max_value as u16));
1236 assert_eq!(i8::max_value.to_u32(), Some(i8::max_value as u32));
1237 assert_eq!(i8::max_value.to_u64(), Some(i8::max_value as u64));
1238 }
1239
1240 #[test]
1241 fn test_cast_range_i16_max() {
1242 assert_eq!(i16::max_value.to_int(), Some(i16::max_value as int));
1243 assert_eq!(i16::max_value.to_i8(), None);
1244 assert_eq!(i16::max_value.to_i16(), Some(i16::max_value as i16));
1245 assert_eq!(i16::max_value.to_i32(), Some(i16::max_value as i32));
1246 assert_eq!(i16::max_value.to_i64(), Some(i16::max_value as i64));
1247 assert_eq!(i16::max_value.to_uint(), Some(i16::max_value as uint));
1248 assert_eq!(i16::max_value.to_u8(), None);
1249 assert_eq!(i16::max_value.to_u16(), Some(i16::max_value as u16));
1250 assert_eq!(i16::max_value.to_u32(), Some(i16::max_value as u32));
1251 assert_eq!(i16::max_value.to_u64(), Some(i16::max_value as u64));
1252 }
1253
1254 #[test]
1255 fn test_cast_range_i32_max() {
1256 assert_eq!(i32::max_value.to_int(), Some(i32::max_value as int));
1257 assert_eq!(i32::max_value.to_i8(), None);
1258 assert_eq!(i32::max_value.to_i16(), None);
1259 assert_eq!(i32::max_value.to_i32(), Some(i32::max_value as i32));
1260 assert_eq!(i32::max_value.to_i64(), Some(i32::max_value as i64));
1261 assert_eq!(i32::max_value.to_uint(), Some(i32::max_value as uint));
1262 assert_eq!(i32::max_value.to_u8(), None);
1263 assert_eq!(i32::max_value.to_u16(), None);
1264 assert_eq!(i32::max_value.to_u32(), Some(i32::max_value as u32));
1265 assert_eq!(i32::max_value.to_u64(), Some(i32::max_value as u64));
1266 }
1267
1268 #[test]
1269 fn test_cast_range_i64_max() {
1270 // i64::max_value.to_int() is word-size specific
1271 assert_eq!(i64::max_value.to_i8(), None);
1272 assert_eq!(i64::max_value.to_i16(), None);
1273 assert_eq!(i64::max_value.to_i32(), None);
1274 assert_eq!(i64::max_value.to_i64(), Some(i64::max_value as i64));
1275 // i64::max_value.to_uint() is word-size specific
1276 assert_eq!(i64::max_value.to_u8(), None);
1277 assert_eq!(i64::max_value.to_u16(), None);
1278 assert_eq!(i64::max_value.to_u32(), None);
1279 assert_eq!(i64::max_value.to_u64(), Some(i64::max_value as u64));
1280
1281 #[cfg(target_word_size = "32")]
1282 fn check_word_size() {
1283 assert_eq!(i64::max_value.to_int(), None);
1284 assert_eq!(i64::max_value.to_uint(), None);
1285 }
1286
1287 #[cfg(target_word_size = "64")]
1288 fn check_word_size() {
1289 assert_eq!(i64::max_value.to_int(), Some(i64::max_value as int));
1290 assert_eq!(i64::max_value.to_uint(), Some(i64::max_value as uint));
1291 }
1292
1293 check_word_size();
1294 }
1295
1296 #[test]
1297 fn test_cast_range_uint_min() {
1298 assert_eq!(uint::min_value.to_int(), Some(uint::min_value as int));
1299 assert_eq!(uint::min_value.to_i8(), Some(uint::min_value as i8));
1300 assert_eq!(uint::min_value.to_i16(), Some(uint::min_value as i16));
1301 assert_eq!(uint::min_value.to_i32(), Some(uint::min_value as i32));
1302 assert_eq!(uint::min_value.to_i64(), Some(uint::min_value as i64));
1303 assert_eq!(uint::min_value.to_uint(), Some(uint::min_value as uint));
1304 assert_eq!(uint::min_value.to_u8(), Some(uint::min_value as u8));
1305 assert_eq!(uint::min_value.to_u16(), Some(uint::min_value as u16));
1306 assert_eq!(uint::min_value.to_u32(), Some(uint::min_value as u32));
1307 assert_eq!(uint::min_value.to_u64(), Some(uint::min_value as u64));
1308 }
1309
1310 #[test]
1311 fn test_cast_range_u8_min() {
1312 assert_eq!(u8::min_value.to_int(), Some(u8::min_value as int));
1313 assert_eq!(u8::min_value.to_i8(), Some(u8::min_value as i8));
1314 assert_eq!(u8::min_value.to_i16(), Some(u8::min_value as i16));
1315 assert_eq!(u8::min_value.to_i32(), Some(u8::min_value as i32));
1316 assert_eq!(u8::min_value.to_i64(), Some(u8::min_value as i64));
1317 assert_eq!(u8::min_value.to_uint(), Some(u8::min_value as uint));
1318 assert_eq!(u8::min_value.to_u8(), Some(u8::min_value as u8));
1319 assert_eq!(u8::min_value.to_u16(), Some(u8::min_value as u16));
1320 assert_eq!(u8::min_value.to_u32(), Some(u8::min_value as u32));
1321 assert_eq!(u8::min_value.to_u64(), Some(u8::min_value as u64));
1322 }
1323
1324 #[test]
1325 fn test_cast_range_u16_min() {
1326 assert_eq!(u16::min_value.to_int(), Some(u16::min_value as int));
1327 assert_eq!(u16::min_value.to_i8(), Some(u16::min_value as i8));
1328 assert_eq!(u16::min_value.to_i16(), Some(u16::min_value as i16));
1329 assert_eq!(u16::min_value.to_i32(), Some(u16::min_value as i32));
1330 assert_eq!(u16::min_value.to_i64(), Some(u16::min_value as i64));
1331 assert_eq!(u16::min_value.to_uint(), Some(u16::min_value as uint));
1332 assert_eq!(u16::min_value.to_u8(), Some(u16::min_value as u8));
1333 assert_eq!(u16::min_value.to_u16(), Some(u16::min_value as u16));
1334 assert_eq!(u16::min_value.to_u32(), Some(u16::min_value as u32));
1335 assert_eq!(u16::min_value.to_u64(), Some(u16::min_value as u64));
1336 }
1337
1338 #[test]
1339 fn test_cast_range_u32_min() {
1340 assert_eq!(u32::min_value.to_int(), Some(u32::min_value as int));
1341 assert_eq!(u32::min_value.to_i8(), Some(u32::min_value as i8));
1342 assert_eq!(u32::min_value.to_i16(), Some(u32::min_value as i16));
1343 assert_eq!(u32::min_value.to_i32(), Some(u32::min_value as i32));
1344 assert_eq!(u32::min_value.to_i64(), Some(u32::min_value as i64));
1345 assert_eq!(u32::min_value.to_uint(), Some(u32::min_value as uint));
1346 assert_eq!(u32::min_value.to_u8(), Some(u32::min_value as u8));
1347 assert_eq!(u32::min_value.to_u16(), Some(u32::min_value as u16));
1348 assert_eq!(u32::min_value.to_u32(), Some(u32::min_value as u32));
1349 assert_eq!(u32::min_value.to_u64(), Some(u32::min_value as u64));
1350 }
1351
1352 #[test]
1353 fn test_cast_range_u64_min() {
1354 assert_eq!(u64::min_value.to_int(), Some(u64::min_value as int));
1355 assert_eq!(u64::min_value.to_i8(), Some(u64::min_value as i8));
1356 assert_eq!(u64::min_value.to_i16(), Some(u64::min_value as i16));
1357 assert_eq!(u64::min_value.to_i32(), Some(u64::min_value as i32));
1358 assert_eq!(u64::min_value.to_i64(), Some(u64::min_value as i64));
1359 assert_eq!(u64::min_value.to_uint(), Some(u64::min_value as uint));
1360 assert_eq!(u64::min_value.to_u8(), Some(u64::min_value as u8));
1361 assert_eq!(u64::min_value.to_u16(), Some(u64::min_value as u16));
1362 assert_eq!(u64::min_value.to_u32(), Some(u64::min_value as u32));
1363 assert_eq!(u64::min_value.to_u64(), Some(u64::min_value as u64));
1364 }
1365
1366 #[test]
1367 fn test_cast_range_uint_max() {
1368 assert_eq!(uint::max_value.to_int(), None);
1369 assert_eq!(uint::max_value.to_i8(), None);
1370 assert_eq!(uint::max_value.to_i16(), None);
1371 assert_eq!(uint::max_value.to_i32(), None);
1372 // uint::max_value.to_i64() is word-size specific
1373 assert_eq!(uint::max_value.to_u8(), None);
1374 assert_eq!(uint::max_value.to_u16(), None);
1375 // uint::max_value.to_u32() is word-size specific
1376 assert_eq!(uint::max_value.to_u64(), Some(uint::max_value as u64));
1377
1378 #[cfg(target_word_size = "32")]
1379 fn check_word_size() {
1380 assert_eq!(uint::max_value.to_u32(), Some(uint::max_value as u32));
1381 assert_eq!(uint::max_value.to_i64(), Some(uint::max_value as i64));
1382 }
1383
1384 #[cfg(target_word_size = "64")]
1385 fn check_word_size() {
1386 assert_eq!(uint::max_value.to_u32(), None);
1387 assert_eq!(uint::max_value.to_i64(), None);
1388 }
1389
1390 check_word_size();
1391 }
1392
1393 #[test]
1394 fn test_cast_range_u8_max() {
1395 assert_eq!(u8::max_value.to_int(), Some(u8::max_value as int));
1396 assert_eq!(u8::max_value.to_i8(), None);
1397 assert_eq!(u8::max_value.to_i16(), Some(u8::max_value as i16));
1398 assert_eq!(u8::max_value.to_i32(), Some(u8::max_value as i32));
1399 assert_eq!(u8::max_value.to_i64(), Some(u8::max_value as i64));
1400 assert_eq!(u8::max_value.to_uint(), Some(u8::max_value as uint));
1401 assert_eq!(u8::max_value.to_u8(), Some(u8::max_value as u8));
1402 assert_eq!(u8::max_value.to_u16(), Some(u8::max_value as u16));
1403 assert_eq!(u8::max_value.to_u32(), Some(u8::max_value as u32));
1404 assert_eq!(u8::max_value.to_u64(), Some(u8::max_value as u64));
1405 }
1406
1407 #[test]
1408 fn test_cast_range_u16_max() {
1409 assert_eq!(u16::max_value.to_int(), Some(u16::max_value as int));
1410 assert_eq!(u16::max_value.to_i8(), None);
1411 assert_eq!(u16::max_value.to_i16(), None);
1412 assert_eq!(u16::max_value.to_i32(), Some(u16::max_value as i32));
1413 assert_eq!(u16::max_value.to_i64(), Some(u16::max_value as i64));
1414 assert_eq!(u16::max_value.to_uint(), Some(u16::max_value as uint));
1415 assert_eq!(u16::max_value.to_u8(), None);
1416 assert_eq!(u16::max_value.to_u16(), Some(u16::max_value as u16));
1417 assert_eq!(u16::max_value.to_u32(), Some(u16::max_value as u32));
1418 assert_eq!(u16::max_value.to_u64(), Some(u16::max_value as u64));
1419 }
1420
1421 #[test]
1422 fn test_cast_range_u32_max() {
1423 // u32::max_value.to_int() is word-size specific
1424 assert_eq!(u32::max_value.to_i8(), None);
1425 assert_eq!(u32::max_value.to_i16(), None);
1426 assert_eq!(u32::max_value.to_i32(), None);
1427 assert_eq!(u32::max_value.to_i64(), Some(u32::max_value as i64));
1428 assert_eq!(u32::max_value.to_uint(), Some(u32::max_value as uint));
1429 assert_eq!(u32::max_value.to_u8(), None);
1430 assert_eq!(u32::max_value.to_u16(), None);
1431 assert_eq!(u32::max_value.to_u32(), Some(u32::max_value as u32));
1432 assert_eq!(u32::max_value.to_u64(), Some(u32::max_value as u64));
1433
1434 #[cfg(target_word_size = "32")]
1435 fn check_word_size() {
1436 assert_eq!(u32::max_value.to_int(), None);
1437 }
1438
1439 #[cfg(target_word_size = "64")]
1440 fn check_word_size() {
1441 assert_eq!(u32::max_value.to_int(), Some(u32::max_value as int));
1442 }
1443
1444 check_word_size();
1445 }
1446
1447 #[test]
1448 fn test_cast_range_u64_max() {
1449 assert_eq!(u64::max_value.to_int(), None);
1450 assert_eq!(u64::max_value.to_i8(), None);
1451 assert_eq!(u64::max_value.to_i16(), None);
1452 assert_eq!(u64::max_value.to_i32(), None);
1453 assert_eq!(u64::max_value.to_i64(), None);
1454 // u64::max_value.to_uint() is word-size specific
1455 assert_eq!(u64::max_value.to_u8(), None);
1456 assert_eq!(u64::max_value.to_u16(), None);
1457 assert_eq!(u64::max_value.to_u32(), None);
1458 assert_eq!(u64::max_value.to_u64(), Some(u64::max_value as u64));
1459
1460 #[cfg(target_word_size = "32")]
1461 fn check_word_size() {
1462 assert_eq!(u64::max_value.to_uint(), None);
1463 }
1464
1465 #[cfg(target_word_size = "64")]
1466 fn check_word_size() {
1467 assert_eq!(u64::max_value.to_uint(), Some(u64::max_value as uint));
1468 }
1469
1470 check_word_size();
1471 }
1472
1473 #[test]
1474 fn test_saturating_add_uint() {
1475 use uint::max_value;
1476 assert_eq!(3u.saturating_add(5u), 8u);
1477 assert_eq!(3u.saturating_add(max_value-1), max_value);
1478 assert_eq!(max_value.saturating_add(max_value), max_value);
1479 assert_eq!((max_value-2).saturating_add(1), max_value-1);
1480 }
1481
1482 #[test]
1483 fn test_saturating_sub_uint() {
1484 use uint::max_value;
1485 assert_eq!(5u.saturating_sub(3u), 2u);
1486 assert_eq!(3u.saturating_sub(5u), 0u);
1487 assert_eq!(0u.saturating_sub(1u), 0u);
1488 assert_eq!((max_value-1).saturating_sub(max_value), 0);
1489 }
1490
1491 #[test]
1492 fn test_saturating_add_int() {
1493 use int::{min_value,max_value};
1494 assert_eq!(3i.saturating_add(5i), 8i);
1495 assert_eq!(3i.saturating_add(max_value-1), max_value);
1496 assert_eq!(max_value.saturating_add(max_value), max_value);
1497 assert_eq!((max_value-2).saturating_add(1), max_value-1);
1498 assert_eq!(3i.saturating_add(-5i), -2i);
1499 assert_eq!(min_value.saturating_add(-1i), min_value);
1500 assert_eq!((-2i).saturating_add(-max_value), min_value);
1501 }
1502
1503 #[test]
1504 fn test_saturating_sub_int() {
1505 use int::{min_value,max_value};
1506 assert_eq!(3i.saturating_sub(5i), -2i);
1507 assert_eq!(min_value.saturating_sub(1i), min_value);
1508 assert_eq!((-2i).saturating_sub(max_value), min_value);
1509 assert_eq!(3i.saturating_sub(-5i), 8i);
1510 assert_eq!(3i.saturating_sub(-(max_value-1)), max_value);
1511 assert_eq!(max_value.saturating_sub(-max_value), max_value);
1512 assert_eq!((max_value-2).saturating_sub(-1), max_value-1);
1513 }
1514
1515 #[test]
1516 fn test_checked_add() {
1517 let five_less = uint::max_value - 5;
1518 assert_eq!(five_less.checked_add(&0), Some(uint::max_value - 5));
1519 assert_eq!(five_less.checked_add(&1), Some(uint::max_value - 4));
1520 assert_eq!(five_less.checked_add(&2), Some(uint::max_value - 3));
1521 assert_eq!(five_less.checked_add(&3), Some(uint::max_value - 2));
1522 assert_eq!(five_less.checked_add(&4), Some(uint::max_value - 1));
1523 assert_eq!(five_less.checked_add(&5), Some(uint::max_value));
1524 assert_eq!(five_less.checked_add(&6), None);
1525 assert_eq!(five_less.checked_add(&7), None);
1526 }
1527
1528 #[test]
1529 fn test_checked_sub() {
1530 assert_eq!(5u.checked_sub(&0), Some(5));
1531 assert_eq!(5u.checked_sub(&1), Some(4));
1532 assert_eq!(5u.checked_sub(&2), Some(3));
1533 assert_eq!(5u.checked_sub(&3), Some(2));
1534 assert_eq!(5u.checked_sub(&4), Some(1));
1535 assert_eq!(5u.checked_sub(&5), Some(0));
1536 assert_eq!(5u.checked_sub(&6), None);
1537 assert_eq!(5u.checked_sub(&7), None);
1538 }
1539
1540 #[test]
1541 fn test_checked_mul() {
1542 let third = uint::max_value / 3;
1543 assert_eq!(third.checked_mul(&0), Some(0));
1544 assert_eq!(third.checked_mul(&1), Some(third));
1545 assert_eq!(third.checked_mul(&2), Some(third * 2));
1546 assert_eq!(third.checked_mul(&3), Some(third * 3));
1547 assert_eq!(third.checked_mul(&4), None);
1548 }
1549
1550
1551 #[deriving(Eq)]
1552 struct Value { x: int }
1553
1554 impl ToPrimitive for Value {
1555 fn to_i64(&self) -> Option<i64> { self.x.to_i64() }
1556 fn to_u64(&self) -> Option<u64> { self.x.to_u64() }
1557 }
1558
1559 impl FromPrimitive for Value {
1560 fn from_i64(n: i64) -> Option<Value> { Some(Value { x: n as int }) }
1561 fn from_u64(n: u64) -> Option<Value> { Some(Value { x: n as int }) }
1562 }
1563
1564 #[test]
1565 fn test_to_primitive() {
1566 let value = Value { x: 5 };
1567 assert_eq!(value.to_int(), Some(5));
1568 assert_eq!(value.to_i8(), Some(5));
1569 assert_eq!(value.to_i16(), Some(5));
1570 assert_eq!(value.to_i32(), Some(5));
1571 assert_eq!(value.to_i64(), Some(5));
1572 assert_eq!(value.to_uint(), Some(5));
1573 assert_eq!(value.to_u8(), Some(5));
1574 assert_eq!(value.to_u16(), Some(5));
1575 assert_eq!(value.to_u32(), Some(5));
1576 assert_eq!(value.to_u64(), Some(5));
1577 assert_eq!(value.to_f32(), Some(5f32));
1578 assert_eq!(value.to_f64(), Some(5f64));
1579 }
1580
1581 #[test]
1582 fn test_from_primitive() {
1583 assert_eq!(from_int(5), Some(Value { x: 5 }));
1584 assert_eq!(from_i8(5), Some(Value { x: 5 }));
1585 assert_eq!(from_i16(5), Some(Value { x: 5 }));
1586 assert_eq!(from_i32(5), Some(Value { x: 5 }));
1587 assert_eq!(from_i64(5), Some(Value { x: 5 }));
1588 assert_eq!(from_uint(5), Some(Value { x: 5 }));
1589 assert_eq!(from_u8(5), Some(Value { x: 5 }));
1590 assert_eq!(from_u16(5), Some(Value { x: 5 }));
1591 assert_eq!(from_u32(5), Some(Value { x: 5 }));
1592 assert_eq!(from_u64(5), Some(Value { x: 5 }));
1593 assert_eq!(from_f32(5f32), Some(Value { x: 5 }));
1594 assert_eq!(from_f64(5f64), Some(Value { x: 5 }));
1595 }
1596 }
libstd/num/num.rs:868:10-868:10 -fn- definition:
#[inline]
pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
references:-937: let mut multiplier = cast(radix).unwrap();
libstd/num/strconv.rs:
250: let radix_gen: T = cast(radix as int).unwrap();
143: let radix_gen: T = cast(radix).unwrap();
495: let radix_gen: T = cast(radix as int).unwrap();
559: (last_accum != ((accum - cast(digit as int).unwrap())/radix_gen.clone())) {
563: (last_accum != ((accum + cast(digit as int).unwrap())/radix_gen.clone())) {
546: accum = accum + cast(digit as int).unwrap();
548: accum = accum - cast(digit as int).unwrap();
599: let digit_t: T = cast(digit).unwrap();
libstd/num/num.rs:963:31-963:31 -trait- definition:
/// Saturating math operations
pub trait Saturating {
references:-971: fn saturating_sub(self, v: Self) -> Self;
967: fn saturating_add(self, v: Self) -> Self;
974: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
967: fn saturating_add(self, v: Self) -> Self;
971: fn saturating_sub(self, v: Self) -> Self;
libstd/num/num.rs:359:16-359:16 -enum- definition:
#[deriving(Eq)]
pub enum FPCategory {
references:-359: #[deriving(Eq)]
359: #[deriving(Eq)]
359: #[deriving(Eq)]
388: fn classify(&self) -> FPCategory;
libstd/num/f32.rs:
635: fn classify(&self) -> FPCategory {
libstd/num/f64.rs:
683: fn classify(&self) -> FPCategory {
libstd/num/num.rs:261:57-261:57 -trait- definition:
/// Defines constants and methods common to real numbers
pub trait Real: Signed
references:-279: fn sqrt2() -> Self;
374: pub trait Float: Real
289: fn to_radians(&self) -> Self;
276: fn frac_1_pi() -> Self;
280: fn frac_1_sqrt2() -> Self;
282: fn log2_e() -> Self;
278: fn frac_2_sqrtpi() -> Self;
284: fn ln_2() -> Self;
273: fn frac_pi_4() -> Self;
272: fn frac_pi_3() -> Self;
288: fn to_degrees(&self) -> Self;
281: fn e() -> Self;
271: fn frac_pi_2() -> Self;
293: pub trait RealExt: Real {
270: fn two_pi() -> Self;
277: fn frac_2_pi() -> Self;
274: fn frac_pi_6() -> Self;
275: fn frac_pi_8() -> Self;
269: fn pi() -> Self;
285: fn ln_10() -> Self;
283: fn log10_e() -> Self;
libstd/num/f32.rs:
498: impl Real for f32 {
libstd/num/f64.rs:
516: impl Real for f64 {
libstd/num/num.rs:48:18-48:18 -fn- definition:
#[inline(always)] pub fn max<T: Orderable>(x: T, y: T) -> T { x.max(&y) }
/// Returns the number constrained within the range `mn <= self <= mx`.
references:-libstd/io.rs:
1695: let count = num::max(bytes.len(), *self.pos + v_len);
libstd/hashmap.rs:
344: let cap = num::max(INITIAL_CAPACITY, capacity);
libstd/num/num.rs:80:18-80:18 -fn- definition:
#[inline(always)] pub fn abs<T: Signed>(value: T) -> T { value.abs() }
/// The positive difference of two numbers.
references:-libstd/rand/distributions.rs:
42: let test_x = if center_u {num::abs(x)} else {x};
libstd/unstable/extfmt.rs:
511: let s : ~str = uint_to_str_prec(num::abs(i) as uint, radix, prec);
libstd/num/num.rs:1007:1-1007:1 -trait- definition:
pub trait CheckedMul: Mul<Self, Self> {
references:-1009: fn checked_mul(&self, v: &Self) -> Option<Self>;
1009: fn checked_mul(&self, v: &Self) -> Option<Self>;
1008: pub trait CheckedMul: Mul<Self, Self> {
1008: pub trait CheckedMul: Mul<Self, Self> {
libstd/num/int.rs:
112: impl CheckedMul for int {
libstd/num/i8.rs:
55: impl CheckedMul for i8 {
libstd/num/i16.rs:
55: impl CheckedMul for i16 {
libstd/num/i32.rs:
55: impl CheckedMul for i32 {
libstd/num/i64.rs:
59: impl CheckedMul for i64 {
libstd/num/uint.rs:
173: impl CheckedMul for uint {
libstd/num/u8.rs:
41: impl CheckedMul for u8 {
libstd/num/u16.rs:
41: impl CheckedMul for u16 {
libstd/num/u32.rs:
41: impl CheckedMul for u32 {
libstd/num/u64.rs:
45: impl CheckedMul for u64 {
libstd/num/num.rs:310:52-310:52 -trait- definition:
/// Collects the bitwise operators under one trait.
pub trait Bitwise: Not<Self>
references:-312: + BitAnd<Self,Self>
355: + Bitwise
313: + BitOr<Self,Self>
316: + Shr<Self,Self> {}
315: + Shl<Self,Self>
311: pub trait Bitwise: Not<Self>
312: + BitAnd<Self,Self>
316: + Shr<Self,Self> {}
314: + BitXor<Self,Self>
314: + BitXor<Self,Self>
315: + Shl<Self,Self>
313: + BitOr<Self,Self>
libstd/num/int_macros.rs:
329: impl Bitwise for $T {}
329: impl Bitwise for $T {}
329: impl Bitwise for $T {}
329: impl Bitwise for $T {}
329: impl Bitwise for $T {}
libstd/num/uint_macros.rs:
192: impl Bitwise for $T {}
192: impl Bitwise for $T {}
192: impl Bitwise for $T {}
192: impl Bitwise for $T {}
192: impl Bitwise for $T {}
libstd/num/num.rs:292:64-292:64 -trait- definition:
/// Methods that are harder to implement and not commonly used.
pub trait RealExt: Real {
references:-305: fn y0(&self) -> Self;
304: fn jn(&self, n: int) -> Self;
303: fn j1(&self) -> Self;
307: fn yn(&self, n: int) -> Self;
299: fn tgamma(&self) -> Self;
302: fn j0(&self) -> Self;
306: fn y1(&self) -> Self;
298: fn lgamma(&self) -> (int, Self);
libstd/num/f64.rs:
597: impl RealExt for f64 {
libstd/num/num.rs:317:1-317:1 -trait- definition:
pub trait BitCount {
references:-356: + BitCount {}
319: fn population_count(&self) -> Self;
321: fn trailing_zeros(&self) -> Self;
320: fn leading_zeros(&self) -> Self;
libstd/num/uint_macros.rs:
314: impl BitCount for $T {
314: impl BitCount for $T {
314: impl BitCount for $T {
314: impl BitCount for $T {
314: impl BitCount for $T {
libstd/num/int.rs:
42: impl BitCount for int {
libstd/num/i8.rs:
21: impl BitCount for i8 {
libstd/num/i16.rs:
21: impl BitCount for i16 {
libstd/num/i32.rs:
21: impl BitCount for i32 {
libstd/num/i64.rs:
23: impl BitCount for i64 {
libstd/num/num.rs:212:1-212:1 -trait- definition:
pub trait Exponential {
references:-217: fn ln(&self) -> Self;
229: #[inline(always)] pub fn ln<T: Exponential>(value: T) -> T { value.ln() }
226: #[inline(always)] pub fn exp2<T: Exponential>(value: T) -> T { value.exp2() }
231: #[inline(always)] pub fn log<T: Exponential>(value: T, base: T) -> T { value.log(&base) }
237: pub trait Hyperbolic: Exponential {
235: #[inline(always)] pub fn log10<T: Exponential>(value: T) -> T { value.log10() }
224: #[inline(always)] pub fn exp<T: Exponential>(value: T) -> T { value.exp() }
220: fn log10(&self) -> Self;
214: fn exp(&self) -> Self;
218: fn log(&self, base: &Self) -> Self;
233: #[inline(always)] pub fn log2<T: Exponential>(value: T) -> T { value.log2() }
219: fn log2(&self) -> Self;
218: fn log(&self, base: &Self) -> Self;
215: fn exp2(&self) -> Self;
libstd/num/f32.rs:
410: impl Exponential for f32 {
libstd/num/f64.rs:
428: impl Exponential for f64 {
libstd/num/num.rs:373:37-373:37 -trait- definition:
/// Primitive floating point numbers
pub trait Float: Real
references:-404: fn mul_add(&self, a: Self, b: Self) -> Self;
379: fn nan() -> Self;
395: fn max_exp(unused_self: Option<Self>) -> int;
380: fn infinity() -> Self;
377: + ApproxEq<Self> {
399: fn ldexp(x: Self, exp: int) -> Self;
397: fn max_10_exp(unused_self: Option<Self>) -> int;
399: fn ldexp(x: Self, exp: int) -> Self;
382: fn neg_zero() -> Self;
418: #[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
405: fn next_after(&self, other: Self) -> Self;
404: fn mul_add(&self, a: Self, b: Self) -> Self;
405: fn next_after(&self, other: Self) -> Self;
413: #[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() }
391: fn mantissa_digits(unused_self: Option<Self>) -> uint;
410: #[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() }
396: fn min_10_exp(unused_self: Option<Self>) -> int;
392: fn digits(unused_self: Option<Self>) -> uint;
402: fn exp_m1(&self) -> Self;
393: fn epsilon() -> Self;
381: fn neg_infinity() -> Self;
400: fn frexp(&self) -> (Self, int);
394: fn min_exp(unused_self: Option<Self>) -> int;
404: fn mul_add(&self, a: Self, b: Self) -> Self;
403: fn ln_1p(&self) -> Self;
libstd/num/strconv.rs:
222: pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
413: pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
libstd/num/f32.rs:
598: impl Float for f32 {
libstd/num/f64.rs:
646: impl Float for f64 {
libstd/num/num.rs:146:1-146:1 -trait- definition:
pub trait Fractional: Num
references:-151: fn recip(&self) -> Self;
150: + Div<Self,Self> {
150: + Div<Self,Self> {
263: + Fractional
libstd/num/f32.rs:
358: impl Fractional for f32 {
libstd/num/f64.rs:
376: impl Fractional for f64 {
libstd/num/num.rs:113:1-113:1 -trait- definition:
pub trait Integer: Num
references:-124: fn gcd(&self, other: &Self) -> Self;
121: fn mod_floor(&self, other: &Self) -> Self;
116: + Div<Self,Self>
118: fn div_rem(&self, other: &Self) -> (Self,Self);
118: fn div_rem(&self, other: &Self) -> (Self,Self);
122: fn div_mod_floor(&self, other: &Self) -> (Self,Self);
135: #[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
116: + Div<Self,Self>
137: #[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
117: + Rem<Self,Self> {
127: fn is_multiple_of(&self, other: &Self) -> bool;
117: + Rem<Self,Self> {
125: fn lcm(&self, other: &Self) -> Self;
124: fn gcd(&self, other: &Self) -> Self;
120: fn div_floor(&self, other: &Self) -> Self;
122: fn div_mod_floor(&self, other: &Self) -> (Self,Self);
118: fn div_rem(&self, other: &Self) -> (Self,Self);
120: fn div_floor(&self, other: &Self) -> Self;
125: fn lcm(&self, other: &Self) -> Self;
353: pub trait Int: Integer
122: fn div_mod_floor(&self, other: &Self) -> (Self,Self);
121: fn mod_floor(&self, other: &Self) -> Self;
libstd/num/strconv.rs:
135: pub fn int_to_str_bytes_common<T:NumCast+Zero+Eq+Ord+Integer+
libstd/iter.rs:
1850: impl<A: Integer + Ord + Clone> DoubleEndedIterator<A> for Range<A> {
1907: impl<A: Sub<A, A> + Integer + Ord + Clone> DoubleEndedIterator<A> for RangeInclusive<A> {
libstd/num/int_macros.rs:
211: impl Integer for $T {
211: impl Integer for $T {
211: impl Integer for $T {
211: impl Integer for $T {
211: impl Integer for $T {
libstd/num/uint_macros.rs:
(138)(138)(138)(138)(138)libstd/num/num.rs:1003:1-1003:1 -trait- definition:
pub trait CheckedSub: Sub<Self, Self> {
references:-1004: pub trait CheckedSub: Sub<Self, Self> {
1005: fn checked_sub(&self, v: &Self) -> Option<Self>;
1004: pub trait CheckedSub: Sub<Self, Self> {
1005: fn checked_sub(&self, v: &Self) -> Option<Self>;
974: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
libstd/num/int.rs:
90: impl CheckedSub for int {
libstd/num/i8.rs:
45: impl CheckedSub for i8 {
libstd/num/i16.rs:
45: impl CheckedSub for i16 {
libstd/num/i32.rs:
45: impl CheckedSub for i32 {
libstd/num/i64.rs:
47: impl CheckedSub for i64 {
libstd/num/uint.rs:
151: impl CheckedSub for uint {
libstd/num/u8.rs:
31: impl CheckedSub for u8 {
libstd/num/u16.rs:
31: impl CheckedSub for u16 {
libstd/num/u32.rs:
31: impl CheckedSub for u32 {
libstd/num/u64.rs:
33: impl CheckedSub for u64 {
libstd/num/num.rs:420:56-420:56 -trait- definition:
/// A generic trait for converting a value to a number.
pub trait ToPrimitive {
references:-525: impl ToPrimitive for $T {
650: impl ToPrimitive for $T {
595: impl ToPrimitive for $T {
525: impl ToPrimitive for $T {
525: impl ToPrimitive for $T {
650: impl ToPrimitive for $T {
595: impl ToPrimitive for $T {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
874: pub trait NumCast: ToPrimitive {
525: impl ToPrimitive for $T {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
525: impl ToPrimitive for $T {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
595: impl ToPrimitive for $T {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
595: impl ToPrimitive for $T {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
882: fn from<N: ToPrimitive>(n: N) -> Option<$T> {
595: impl ToPrimitive for $T {
875: fn from<T: ToPrimitive>(n: T) -> Option<Self>;
libstd/num/num.rs:180:1-180:1 -trait- definition:
pub trait Trigonometric {
references:-195: #[inline(always)] pub fn sin<T: Trigonometric>(value: T) -> T { value.sin() }
191: fn sin_cos(&self) -> (Self, Self);
206: #[inline(always)] pub fn atan<T: Trigonometric>(value: T) -> T { value.atan() }
183: fn cos(&self) -> Self;
182: fn sin(&self) -> Self;
188: fn atan(&self) -> Self;
199: #[inline(always)] pub fn tan<T: Trigonometric>(value: T) -> T { value.tan() }
191: fn sin_cos(&self) -> (Self, Self);
187: fn acos(&self) -> Self;
209: #[inline(always)] pub fn atan2<T: Trigonometric>(x: T, y: T) -> T { x.atan2(&y) }
197: #[inline(always)] pub fn cos<T: Trigonometric>(value: T) -> T { value.cos() }
204: #[inline(always)] pub fn acos<T: Trigonometric>(value: T) -> T { value.acos() }
190: fn atan2(&self, other: &Self) -> Self;
265: + Trigonometric
211: #[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }
190: fn atan2(&self, other: &Self) -> Self;
186: fn asin(&self) -> Self;
184: fn tan(&self) -> Self;
202: #[inline(always)] pub fn asin<T: Trigonometric>(value: T) -> T { value.asin() }
libstd/num/f32.rs:
381: impl Trigonometric for f32 {
libstd/num/f64.rs:
399: impl Trigonometric for f64 {
libstd/num/num.rs:46:18-46:18 -fn- definition:
#[inline(always)] pub fn min<T: Orderable>(x: T, y: T) -> T { x.min(&y) }
/// Return the larger number.
references:-libstd/io.rs:
1091: let count = num::min(len, self.bytes.len() - *self.pos);
libstd/rt/io/buffered.rs:
147: let nread = num::min(available.len(), buf.len());
libstd/vec.rs:
2215: let n = num::min(a_len, b_len) as libc::size_t;
libstd/num/num.rs:684:56-684:56 -trait- definition:
/// A generic trait for converting a number to a value.
pub trait FromPrimitive {
references:-801: pub fn from_u16<A: FromPrimitive>(n: u16) -> Option<A> {
696: fn from_i8(n: i8) -> Option<Self> {
827: impl FromPrimitive for $T {
710: fn from_i32(n: i32) -> Option<Self> {
806: pub fn from_u32<A: FromPrimitive>(n: u32) -> Option<A> {
811: pub fn from_u64<A: FromPrimitive>(n: u64) -> Option<A> {
827: impl FromPrimitive for $T {
753: fn from_f32(n: f32) -> Option<Self> {
728: fn from_u8(n: u8) -> Option<Self> {
821: pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
827: impl FromPrimitive for $T {
781: pub fn from_i32<A: FromPrimitive>(n: i32) -> Option<A> {
735: fn from_u16(n: u16) -> Option<Self> {
791: pub fn from_uint<A: FromPrimitive>(n: uint) -> Option<A> {
827: impl FromPrimitive for $T {
786: pub fn from_i64<A: FromPrimitive>(n: i64) -> Option<A> {
766: pub fn from_int<A: FromPrimitive>(n: int) -> Option<A> {
827: impl FromPrimitive for $T {
827: impl FromPrimitive for $T {
796: pub fn from_u8<A: FromPrimitive>(n: u8) -> Option<A> {
760: fn from_f64(n: f64) -> Option<Self> {
827: impl FromPrimitive for $T {
721: fn from_uint(n: uint) -> Option<Self> {
689: fn from_int(n: int) -> Option<Self> {
771: pub fn from_i8<A: FromPrimitive>(n: i8) -> Option<A> {
816: pub fn from_f32<A: FromPrimitive>(n: f32) -> Option<A> {
827: impl FromPrimitive for $T {
827: impl FromPrimitive for $T {
827: impl FromPrimitive for $T {
776: pub fn from_i16<A: FromPrimitive>(n: i16) -> Option<A> {
(716)(827)(827)(748)(742)(703)libstd/num/num.rs:98:1-98:1 -trait- definition:
pub trait Unsigned: Num {}
references:-libstd/num/uint_macros.rs:
136: impl Unsigned for $T {}
136: impl Unsigned for $T {}
136: impl Unsigned for $T {}
136: impl Unsigned for $T {}
136: impl Unsigned for $T {}
libstd/num/num.rs:323:1-323:1 -trait- definition:
pub trait Bounded {
references:-327: fn max_value() -> Self;
974: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
338: + Bounded
326: fn min_value() -> Self;
libstd/num/int_macros.rs:
367: impl Bounded for $T {
367: impl Bounded for $T {
367: impl Bounded for $T {
367: impl Bounded for $T {
367: impl Bounded for $T {
libstd/num/uint_macros.rs:
230: impl Bounded for $T {
230: impl Bounded for $T {
230: impl Bounded for $T {
230: impl Bounded for $T {
230: impl Bounded for $T {
libstd/num/f32.rs:
579: impl Bounded for f32 {
libstd/num/f64.rs:
627: impl Bounded for f64 {
libstd/num/num.rs:138:1-138:1 -trait- definition:
pub trait Round {
references:-141: fn ceil(&self) -> Self;
140: fn floor(&self) -> Self;
149: + Round
143: fn trunc(&self) -> Self;
142: fn round(&self) -> Self;
144: fn fract(&self) -> Self;
libstd/num/strconv.rs:
222: pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
413: pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
libstd/num/f32.rs:
330: impl Round for f32 {
libstd/num/f64.rs:
348: impl Round for f64 {
libstd/num/num.rs:109:4-109:4 -trait- definition:
///
pub trait Times {
references:-libstd/num/uint.rs:
76: impl num::Times for uint {
libstd/num/num.rs:903:1-903:1 -trait- definition:
pub trait ToStrRadix {
references:-libstd/num/int_macros.rs:
437: impl ToStrRadix for $T {
437: impl ToStrRadix for $T {
437: impl ToStrRadix for $T {
437: impl ToStrRadix for $T {
437: impl ToStrRadix for $T {
libstd/num/uint_macros.rs:
289: impl ToStrRadix for $T {
289: impl ToStrRadix for $T {
289: impl ToStrRadix for $T {
289: impl ToStrRadix for $T {
289: impl ToStrRadix for $T {
libstd/num/f32.rs:
806: impl num::ToStrRadix for f32 {
libstd/num/f64.rs:
854: impl num::ToStrRadix for f64 {
libstd/num/num.rs:66:1-66:1 -trait- definition:
pub trait Signed: Num
references:-70: fn abs_sub(&self, other: &Self) -> Self;
70: fn abs_sub(&self, other: &Self) -> Self;
97: #[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
85: #[inline(always)] pub fn abs_sub<T: Signed>(x: T, y: T) -> T { x.abs_sub(&y) }
80: #[inline(always)] pub fn abs<T: Signed>(value: T) -> T { value.abs() }
68: + Neg<Self> {
69: fn abs(&self) -> Self;
71: fn signum(&self) -> Self;
262: pub trait Real: Signed
375: + Signed
libstd/num/int_macros.rs:
170: impl Signed for $T {
170: impl Signed for $T {
170: impl Signed for $T {
170: impl Signed for $T {
170: impl Signed for $T {
libstd/num/f32.rs:
297: impl Signed for f32 {
libstd/num/f64.rs:
315: impl Signed for f64 {
libstd/num/num.rs:999:1-999:1 -trait- definition:
pub trait CheckedAdd: Add<Self, Self> {
references:-1000: pub trait CheckedAdd: Add<Self, Self> {
1001: fn checked_add(&self, v: &Self) -> Option<Self>;
1001: fn checked_add(&self, v: &Self) -> Option<Self>;
1000: pub trait CheckedAdd: Add<Self, Self> {
974: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
libstd/iter.rs:
1939: impl<A: CheckedAdd + Ord + Clone> Iterator<A> for RangeStep<A> {
1973: impl<A: CheckedAdd + Ord + Clone + Eq> Iterator<A> for RangeStepInclusive<A> {
1967: pub fn range_step_inclusive<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A,
1934: pub fn range_step<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A, step: A) -> RangeStep<A> {
libstd/num/int.rs:
68: impl CheckedAdd for int {
libstd/num/i8.rs:
35: impl CheckedAdd for i8 {
libstd/num/i16.rs:
35: impl CheckedAdd for i16 {
libstd/num/i32.rs:
35: impl CheckedAdd for i32 {
libstd/num/i64.rs:
37: impl CheckedAdd for i64 {
libstd/num/uint.rs:
129: impl CheckedAdd for uint {
libstd/num/u8.rs:
21: impl CheckedAdd for u8 {
libstd/num/u16.rs:
21: impl CheckedAdd for u16 {
libstd/num/u32.rs:
21: impl CheckedAdd for u32 {
libstd/num/u64.rs:
23: impl CheckedAdd for u64 {
libstd/num/num.rs:26:37-26:37 -trait- definition:
/// The base trait for numeric types
pub trait Num: Eq + Zero + One
references:-114: pub trait Integer: Num
31: + Mul<Self,Self>
29: + Add<Self,Self>
32: + Div<Self,Self>
147: pub trait Fractional: Num
32: + Div<Self,Self>
28: + Neg<Self>
33: + Rem<Self,Self> {}
67: pub trait Signed: Num
99: pub trait Unsigned: Num {}
31: + Mul<Self,Self>
30: + Sub<Self,Self>
335: + Num
33: + Rem<Self,Self> {}
30: + Sub<Self,Self>
29: + Add<Self,Self>
libstd/num/int_macros.rs:
45: impl Num for $T {}
45: impl Num for $T {}
45: impl Num for $T {}
45: impl Num for $T {}
45: impl Num for $T {}
libstd/num/uint_macros.rs:
46: impl Num for $T {}
46: impl Num for $T {}
46: impl Num for $T {}
46: impl Num for $T {}
46: impl Num for $T {}
libstd/num/f32.rs:
170: impl Num for f32 {}
libstd/num/f64.rs:
193: impl Num for f64 {}
libstd/num/num.rs:873:53-873:53 -trait- definition:
/// An interface for casting between machine scalars
pub trait NumCast: ToPrimitive {
references:-336: + NumCast
880: impl NumCast for $T {
880: impl NumCast for $T {
880: impl NumCast for $T {
880: impl NumCast for $T {
875: fn from<T: ToPrimitive>(n: T) -> Option<Self>;
869: pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
880: impl NumCast for $T {
880: impl NumCast for $T {
880: impl NumCast for $T {
929: pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
880: impl NumCast for $T {
880: impl NumCast for $T {
880: impl NumCast for $T {
880: impl NumCast for $T {
869: pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
880: impl NumCast for $T {
libstd/num/strconv.rs:
413: pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
135: pub fn int_to_str_bytes_common<T:NumCast+Zero+Eq+Ord+Integer+
467: pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
674: pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+Mul<T,T>+
222: pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
libstd/num/num.rs:34:1-34:1 -trait- definition:
pub trait Orderable: Ord {
references:-115: + Orderable
42: fn clamp(&self, mn: &Self, mx: &Self) -> Self;
46: #[inline(always)] pub fn min<T: Orderable>(x: T, y: T) -> T { x.min(&y) }
41: fn max(&self, other: &Self) -> Self;
50: #[inline(always)] pub fn clamp<T: Orderable>(value: T, mn: T, mx: T) -> T { value.clamp(&mn, &mx) }
42: fn clamp(&self, mn: &Self, mx: &Self) -> Self;
42: fn clamp(&self, mn: &Self, mx: &Self) -> Self;
337: + Orderable
48: #[inline(always)] pub fn max<T: Orderable>(x: T, y: T) -> T { x.max(&y) }
41: fn max(&self, other: &Self) -> Self;
148: + Orderable
40: fn min(&self, other: &Self) -> Self;
40: fn min(&self, other: &Self) -> Self;
libstd/num/int_macros.rs:
59: impl Orderable for $T {
59: impl Orderable for $T {
59: impl Orderable for $T {
59: impl Orderable for $T {
59: impl Orderable for $T {
libstd/num/uint_macros.rs:
60: impl Orderable for $T {
60: impl Orderable for $T {
60: impl Orderable for $T {
60: impl Orderable for $T {
60: impl Orderable for $T {
libstd/num/f32.rs:
206: impl Orderable for f32 {
libstd/num/f64.rs:
229: impl Orderable for f64 {
libstd/num/num.rs:907:1-907:1 -trait- definition:
pub trait FromStrRadix {
references:-913: pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
909: fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
libstd/num/int_macros.rs:
405: impl FromStrRadix for $T {
405: impl FromStrRadix for $T {
405: impl FromStrRadix for $T {
405: impl FromStrRadix for $T {
405: impl FromStrRadix for $T {
libstd/num/uint_macros.rs:
257: impl FromStrRadix for $T {
257: impl FromStrRadix for $T {
257: impl FromStrRadix for $T {
257: impl FromStrRadix for $T {
257: impl FromStrRadix for $T {
libstd/num/f32.rs:
897: impl num::FromStrRadix for f32 {
libstd/num/f64.rs:
945: impl num::FromStrRadix for f64 {
libstd/num/num.rs:153:1-153:1 -trait- definition:
pub trait Algebraic {
references:-174: #[inline(always)] pub fn rsqrt<T: Algebraic>(value: T) -> T { value.rsqrt() }
159: fn hypot(&self, other: &Self) -> Self;
155: fn pow(&self, n: &Self) -> Self;
159: fn hypot(&self, other: &Self) -> Self;
170: #[inline(always)] pub fn pow<T: Algebraic>(value: T, n: T) -> T { value.pow(&n) }
176: #[inline(always)] pub fn cbrt<T: Algebraic>(value: T) -> T { value.cbrt() }
264: + Algebraic
179: #[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }
157: fn rsqrt(&self) -> Self;
172: #[inline(always)] pub fn sqrt<T: Algebraic>(value: T) -> T { value.sqrt() }
158: fn cbrt(&self) -> Self;
155: fn pow(&self, n: &Self) -> Self;
156: fn sqrt(&self) -> Self;
libstd/num/f32.rs:
364: impl Algebraic for f32 {
libstd/num/f64.rs:
382: impl Algebraic for f64 {
libstd/num/num.rs:352:78-352:78 -trait- definition:
/// A collection of traits relevant to primitive signed and unsigned integers
pub trait Int: Integer
references:-libstd/rand/mod.rs:
244: fn gen_integer_range<T: Rand + Int>(&mut self, low: T, high: T) -> T {
libstd/num/int_macros.rs:
375: impl Int for $T {}
375: impl Int for $T {}
375: impl Int for $T {}
375: impl Int for $T {}
375: impl Int for $T {}
libstd/num/uint_macros.rs:
238: impl Int for $T {}
238: impl Int for $T {}
238: impl Int for $T {}
238: impl Int for $T {}
238: impl Int for $T {}
libstd/num/num.rs:51:1-51:1 -trait- definition:
pub trait Zero {
references:-974: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
948: impl<T: Zero + 'static> Zero for @mut T {
953: impl<T: Zero + 'static> Zero for @T {
958: impl<T: Zero> Zero for ~T {
958: impl<T: Zero> Zero for ~T {
929: pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
27: pub trait Num: Eq + Zero + One
953: impl<T: Zero + 'static> Zero for @T {
948: impl<T: Zero + 'static> Zero for @mut T {
58: #[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
53: fn zero() -> Self; // FIXME (#5527): This should be an associated constant
libstd/num/strconv.rs:
413: pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
135: pub fn int_to_str_bytes_common<T:NumCast+Zero+Eq+Ord+Integer+
674: pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+Mul<T,T>+
222: pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
467: pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
libstd/iter.rs:
823: impl<A: Add<A, A> + Zero, T: Iterator<A>> AdditiveIterator<A> for T {
1934: pub fn range_step<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A, step: A) -> RangeStep<A> {
1967: pub fn range_step_inclusive<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A,
libstd/option.rs:
443: impl<T: Zero> Option<T> {
libstd/num/int_macros.rs:
82: impl Zero for $T {
82: impl Zero for $T {
82: impl Zero for $T {
82: impl Zero for $T {
82: impl Zero for $T {
libstd/num/uint_macros.rs:
87: impl Zero for $T {
87: impl Zero for $T {
87: impl Zero for $T {
87: impl Zero for $T {
87: impl Zero for $T {
libstd/num/f32.rs:
(247)libstd/num/f64.rs:
(270)libstd/unit.rs:
(55)libstd/bool.rs:
(334)libstd/char.rs:
(455)libstd/tuple.rs:
(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)(187)..35more..
libstd/num/num.rs:1011:1-1011:1 -trait- definition:
pub trait CheckedDiv: Div<Self, Self> {
references:-1013: fn checked_div(&self, v: &Self) -> Option<Self>;
1012: pub trait CheckedDiv: Div<Self, Self> {
1013: fn checked_div(&self, v: &Self) -> Option<Self>;
1012: pub trait CheckedDiv: Div<Self, Self> {
libstd/num/int_macros.rs:
34: impl CheckedDiv for $T {
34: impl CheckedDiv for $T {
34: impl CheckedDiv for $T {
34: impl CheckedDiv for $T {
34: impl CheckedDiv for $T {
libstd/num/uint_macros.rs:
35: impl CheckedDiv for $T {
35: impl CheckedDiv for $T {
35: impl CheckedDiv for $T {
35: impl CheckedDiv for $T {
35: impl CheckedDiv for $T {
libstd/num/num.rs:332:43-332:43 -trait- definition:
/// may be useful for systems programming.
pub trait Primitive: Clone
references:-342: + Mul<Self,Self>
339: + Neg<Self>
340: + Add<Self,Self>
349: fn is_signed(unused_self: Option<Self>) -> bool;
347: fn bits(unused_self: Option<Self>) -> uint;
376: + Primitive
342: + Mul<Self,Self>
343: + Div<Self,Self>
341: + Sub<Self,Self>
344: + Rem<Self,Self> {
340: + Add<Self,Self>
344: + Rem<Self,Self> {
343: + Div<Self,Self>
354: + Primitive
341: + Sub<Self,Self>
348: fn bytes(unused_self: Option<Self>) -> uint;
libstd/num/int_macros.rs:
377: impl Primitive for $T {
377: impl Primitive for $T {
377: impl Primitive for $T {
377: impl Primitive for $T {
377: impl Primitive for $T {
libstd/num/uint_macros.rs:
303: impl Primitive for $T {
303: impl Primitive for $T {
303: impl Primitive for $T {
303: impl Primitive for $T {
303: impl Primitive for $T {
libstd/num/f32.rs:
587: impl Primitive for f32 {
libstd/num/f64.rs:
635: impl Primitive for f64 {
libstd/num/num.rs:59:1-59:1 -trait- definition:
pub trait One {
references:-65: #[inline(always)] pub fn one<T: One>() -> T { One::one() }
27: pub trait Num: Eq + Zero + One
61: fn one() -> Self; // FIXME (#5527): This should be an associated constant
929: pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
libstd/num/strconv.rs:
467: pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
674: pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+Mul<T,T>+
222: pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
413: pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
libstd/iter.rs:
851: impl<A: Mul<A, A> + One, T: Iterator<A>> MultiplicativeIterator<A> for T {
1828: pub fn range<A: Add<A, A> + Ord + Clone + One>(start: A, stop: A) -> Range<A> {
1871: pub fn range_inclusive<A: Add<A, A> + Ord + Clone + One>(start: A, stop: A) -> RangeInclusive<A> {
libstd/num/int_macros.rs:
90: impl One for $T {
90: impl One for $T {
90: impl One for $T {
90: impl One for $T {
90: impl One for $T {
libstd/num/uint_macros.rs:
95: impl One for $T {
95: impl One for $T {
95: impl One for $T {
95: impl One for $T {
95: impl One for $T {
libstd/num/f32.rs:
256: impl One for f32 {
libstd/num/f64.rs:
279: impl One for f64 {
libstd/num/num.rs:928:4-928:4 -fn- definition:
///
pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
references:-libstd/num/strconv.rs:
657: _1 / pow_with_uint::<T>(base, (-exp_pow.to_int().unwrap()) as uint)
659: pow_with_uint::<T>(base, exp_pow.to_int().unwrap() as uint)
libstd/num/num.rs:236:1-236:1 -trait- definition:
pub trait Hyperbolic: Exponential {
references:-259: #[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() }
257: #[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() }
240: fn tanh(&self) -> Self;
238: fn sinh(&self) -> Self;
255: #[inline(always)] pub fn asinh<T: Hyperbolic>(value: T) -> T { value.asinh() }
242: fn asinh(&self) -> Self;
239: fn cosh(&self) -> Self;
250: #[inline(always)] pub fn cosh<T: Hyperbolic>(value: T) -> T { value.cosh() }
244: fn atanh(&self) -> Self;
252: #[inline(always)] pub fn tanh<T: Hyperbolic>(value: T) -> T { value.tanh() }
243: fn acosh(&self) -> Self;
248: #[inline(always)] pub fn sinh<T: Hyperbolic>(value: T) -> T { value.sinh() }
266: + Hyperbolic {
libstd/num/f32.rs:
436: impl Hyperbolic for f32 {
libstd/num/f64.rs:
454: impl Hyperbolic for f64 {