Skip to content

Commit 6534b4d

Browse files
committed
auto merge of #8115 : bjz/rust/num-traits, r=brson
Continues #4819
2 parents 912d806 + 4f65fc7 commit 6534b4d

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/libstd/num/num.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! An interface for numeric types
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}`.
1215
1316
#[allow(missing_doc)];
1417

@@ -19,9 +22,7 @@ use option::Option;
1922

2023
pub mod strconv;
2124

22-
///
2325
/// The base trait for numeric types
24-
///
2526
pub trait Num: Eq + Zero + One
2627
+ Neg<Self>
2728
+ Add<Self,Self>
@@ -45,18 +46,23 @@ pub trait Orderable: Ord {
4546
fn clamp(&self, mn: &Self, mx: &Self) -> Self;
4647
}
4748

48-
#[inline(always)] pub fn min<T: Orderable>(a: T, b: T) -> T { a.min(&b) }
49-
#[inline(always)] pub fn max<T: Orderable>(a: T, b: T) -> T { a.max(&b) }
49+
#[inline(always)] pub fn min<T: Orderable>(x: T, y: T) -> T { x.min(&y) }
50+
#[inline(always)] pub fn max<T: Orderable>(x: T, y: T) -> T { x.max(&y) }
51+
#[inline(always)] pub fn clamp<T: Orderable>(value: T, mn: T, mx: T) -> T { value.clamp(&mn, &mx) }
5052

5153
pub trait Zero {
5254
fn zero() -> Self; // FIXME (#5527): This should be an associated constant
5355
fn is_zero(&self) -> bool;
5456
}
5557

58+
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
59+
5660
pub trait One {
5761
fn one() -> Self; // FIXME (#5527): This should be an associated constant
5862
}
5963

64+
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
65+
6066
pub trait Signed: Num
6167
+ Neg<Self> {
6268
fn abs(&self) -> Self;
@@ -68,6 +74,7 @@ pub trait Signed: Num
6874
}
6975

7076
#[inline(always)] pub fn abs<T: Signed>(value: T) -> T { value.abs() }
77+
#[inline(always)] pub fn abs_sub<T: Signed>(x: T, y: T) -> T { x.abs_sub(&y) }
7178
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
7279

7380
pub trait Unsigned: Num {}
@@ -90,6 +97,9 @@ pub trait Integer: Num
9097
fn is_odd(&self) -> bool;
9198
}
9299

100+
#[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
101+
#[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
102+
93103
pub trait Round {
94104
fn floor(&self) -> Self;
95105
fn ceil(&self) -> Self;
@@ -113,15 +123,21 @@ pub trait Algebraic {
113123
fn hypot(&self, other: &Self) -> Self;
114124
}
115125

126+
#[inline(always)] pub fn pow<T: Algebraic>(value: T, n: T) -> T { value.pow(&n) }
116127
#[inline(always)] pub fn sqrt<T: Algebraic>(value: T) -> T { value.sqrt() }
128+
#[inline(always)] pub fn rsqrt<T: Algebraic>(value: T) -> T { value.rsqrt() }
129+
#[inline(always)] pub fn cbrt<T: Algebraic>(value: T) -> T { value.cbrt() }
130+
#[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }
117131

118132
pub trait Trigonometric {
119133
fn sin(&self) -> Self;
120134
fn cos(&self) -> Self;
121135
fn tan(&self) -> Self;
136+
122137
fn asin(&self) -> Self;
123138
fn acos(&self) -> Self;
124139
fn atan(&self) -> Self;
140+
125141
fn atan2(&self, other: &Self) -> Self;
126142
fn sin_cos(&self) -> (Self, Self);
127143
}
@@ -135,10 +151,12 @@ pub trait Trigonometric {
135151
#[inline(always)] pub fn atan<T: Trigonometric>(value: T) -> T { value.atan() }
136152

137153
#[inline(always)] pub fn atan2<T: Trigonometric>(x: T, y: T) -> T { x.atan2(&y) }
154+
#[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }
138155

139156
pub trait Exponential {
140157
fn exp(&self) -> Self;
141158
fn exp2(&self) -> Self;
159+
142160
fn ln(&self) -> Self;
143161
fn log(&self, base: &Self) -> Self;
144162
fn log2(&self) -> Self;
@@ -157,6 +175,7 @@ pub trait Hyperbolic: Exponential {
157175
fn sinh(&self) -> Self;
158176
fn cosh(&self) -> Self;
159177
fn tanh(&self) -> Self;
178+
160179
fn asinh(&self) -> Self;
161180
fn acosh(&self) -> Self;
162181
fn atanh(&self) -> Self;
@@ -170,9 +189,7 @@ pub trait Hyperbolic: Exponential {
170189
#[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() }
171190
#[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() }
172191

173-
///
174192
/// Defines constants and methods common to real numbers
175-
///
176193
pub trait Real: Signed
177194
+ Fractional
178195
+ Algebraic
@@ -203,9 +220,7 @@ pub trait Real: Signed
203220
fn to_radians(&self) -> Self;
204221
}
205222

206-
///
207223
/// Methods that are harder to implement and not commonly used.
208-
///
209224
pub trait RealExt: Real {
210225
// FIXME (#5527): usages of `int` should be replaced with an associated
211226
// integer type once these are implemented
@@ -223,9 +238,7 @@ pub trait RealExt: Real {
223238
fn yn(&self, n: int) -> Self;
224239
}
225240

226-
///
227241
/// Collects the bitwise operators under one trait.
228-
///
229242
pub trait Bitwise: Not<Self>
230243
+ BitAnd<Self,Self>
231244
+ BitOr<Self,Self>
@@ -245,11 +258,9 @@ pub trait Bounded {
245258
fn max_value() -> Self;
246259
}
247260

248-
///
249261
/// Specifies the available operations common to all of Rust's core numeric primitives.
250262
/// These may not always make sense from a purely mathematical point of view, but
251263
/// may be useful for systems programming.
252-
///
253264
pub trait Primitive: Num
254265
+ NumCast
255266
+ Bounded
@@ -264,17 +275,13 @@ pub trait Primitive: Num
264275
fn bytes() -> uint;
265276
}
266277

267-
///
268278
/// A collection of traits relevant to primitive signed and unsigned integers
269-
///
270279
pub trait Int: Integer
271280
+ Primitive
272281
+ Bitwise
273282
+ BitCount {}
274283

275-
///
276284
/// Used for representing the classification of floating point numbers
277-
///
278285
#[deriving(Eq)]
279286
pub enum FPCategory {
280287
/// "Not a Number", often obtained by dividing by zero
@@ -289,9 +296,7 @@ pub enum FPCategory {
289296
FPNormal,
290297
}
291298

292-
///
293299
/// Primitive floating point numbers
294-
///
295300
pub trait Float: Real
296301
+ Signed
297302
+ Primitive
@@ -325,7 +330,10 @@ pub trait Float: Real
325330
fn next_after(&self, other: Self) -> Self;
326331
}
327332

328-
///
333+
#[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() }
334+
#[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() }
335+
#[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
336+
329337
/// Cast from one machine scalar to another
330338
///
331339
/// # Example
@@ -340,9 +348,7 @@ pub fn cast<T:NumCast,U:NumCast>(n: T) -> U {
340348
NumCast::from(n)
341349
}
342350

343-
///
344351
/// An interface for casting between machine scalars
345-
///
346352
pub trait NumCast {
347353
fn from<T:NumCast>(n: T) -> Self;
348354

@@ -414,7 +420,6 @@ pub trait FromStrRadix {
414420
pub fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
415421
}
416422

417-
///
418423
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
419424
///
420425
/// Returns `radix^pow` as `T`.

0 commit comments

Comments
 (0)