From 8ca5caf4d936e3193c81c7e9cfa1c6a99717b14a Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 31 Mar 2014 07:00:26 -0400 Subject: [PATCH] num: rm wrapping of `Float` methods as functions The `Float` trait methods will be usable as functions via UFCS, and we came to a consensus to remove duplicate functions like this a long time ago. It does still make sense to keep the duplicate functions when the trait methods are static, unless the decision to leave out the in-scope trait name resolution for static methods changes. --- src/doc/guide-pointers.md | 4 +- src/doc/rust.md | 6 +-- src/doc/tutorial.md | 8 ++-- src/librand/distributions/gamma.rs | 7 ++- src/libstd/num/mod.rs | 73 ------------------------------ 5 files changed, 10 insertions(+), 88 deletions(-) diff --git a/src/doc/guide-pointers.md b/src/doc/guide-pointers.md index 19696b42a3747..76e3b521957d7 100644 --- a/src/doc/guide-pointers.md +++ b/src/doc/guide-pointers.md @@ -332,8 +332,6 @@ sense, they're simple: just keep whatever ownership the data already has. For example: ~~~rust -use std::num::sqrt; - struct Point { x: f32, y: f32, @@ -343,7 +341,7 @@ fn compute_distance(p1: &Point, p2: &Point) -> f32 { let x_d = p1.x - p2.x; let y_d = p1.y - p2.y; - sqrt(x_d * x_d + y_d * y_d) + (x_d * x_d + y_d * y_d).sqrt() } fn main() { diff --git a/src/doc/rust.md b/src/doc/rust.md index 359a9c8052b7a..0f7dda6b7bc7e 100644 --- a/src/doc/rust.md +++ b/src/doc/rust.md @@ -826,14 +826,14 @@ Use declarations support a number of convenient shortcuts: An example of `use` declarations: ~~~~ -use std::num::sin; +use std::iter::range_step; use std::option::{Some, None}; # fn foo(_: T){} fn main() { - // Equivalent to 'std::num::sin(1.0);' - sin(1.0); + // Equivalent to 'std::iter::range_step(0, 10, 2);' + range_step(0, 10, 2); // Equivalent to 'foo(~[std::option::Some(1.0), std::option::None]);' foo(~[Some(1.0), None]); diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index e15145e5f1d34..73c0d0b899a00 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -504,13 +504,12 @@ matching in order to bind names to the contents of data types. ~~~~ use std::f64; -use std::num::atan; fn angle(vector: (f64, f64)) -> f64 { let pi = f64::consts::PI; match vector { (0.0, y) if y < 0.0 => 1.5 * pi, (0.0, _) => 0.5 * pi, - (x, y) => atan(y / x) + (x, y) => (y / x).atan() } } ~~~~ @@ -1430,12 +1429,11 @@ bad, but often copies are expensive. So we’d like to define a function that takes the points by pointer. We can use references to do this: ~~~ -use std::num::sqrt; # struct Point { x: f64, y: f64 } fn compute_distance(p1: &Point, p2: &Point) -> f64 { let x_d = p1.x - p2.x; let y_d = p1.y - p2.y; - sqrt(x_d * x_d + y_d * y_d) + (x_d * x_d + y_d * y_d).sqrt() } ~~~ @@ -2303,7 +2301,7 @@ impl Shape for Circle { fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } } } impl Shape for Square { - fn new(area: f64) -> Square { Square { length: (area).sqrt() } } + fn new(area: f64) -> Square { Square { length: area.sqrt() } } } let area = 42.5; diff --git a/src/librand/distributions/gamma.rs b/src/librand/distributions/gamma.rs index 029333cd78894..a65f4254080f5 100644 --- a/src/librand/distributions/gamma.rs +++ b/src/librand/distributions/gamma.rs @@ -11,7 +11,6 @@ //! The Gamma and derived distributions. use std::num::Float; -use std::num; use {Rng, Open01}; use super::normal::StandardNormal; use super::{IndependentSample, Sample, Exp}; @@ -114,7 +113,7 @@ impl GammaLargeShape { GammaLargeShape { shape: shape, scale: scale, - c: 1. / num::sqrt(9. * d), + c: 1. / (9. * d).sqrt(), d: d } } @@ -143,7 +142,7 @@ impl IndependentSample for GammaSmallShape { fn ind_sample(&self, rng: &mut R) -> f64 { let Open01(u) = rng.gen::>(); - self.large_shape.ind_sample(rng) * num::powf(u, self.inv_shape) + self.large_shape.ind_sample(rng) * u.powf(&self.inv_shape) } } impl IndependentSample for GammaLargeShape { @@ -160,7 +159,7 @@ impl IndependentSample for GammaLargeShape { let x_sqr = x * x; if u < 1.0 - 0.0331 * x_sqr * x_sqr || - num::ln(u) < 0.5 * x_sqr + self.d * (1.0 - v + num::ln(v)) { + u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) { return self.d * v * self.scale } } diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index feff0e7e6c0e5..2c628112957a5 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -553,79 +553,6 @@ pub trait Float: Signed + Round + Primitive { fn to_radians(&self) -> Self; } -/// Returns the exponential of the number, minus `1`, `exp(n) - 1`, in a way -/// that is accurate even if the number is close to zero. -#[inline(always)] pub fn exp_m1(value: T) -> T { value.exp_m1() } -/// Returns the natural logarithm of the number plus `1`, `ln(n + 1)`, more -/// accurately than if the operations were performed separately. -#[inline(always)] pub fn ln_1p(value: T) -> T { value.ln_1p() } -/// Fused multiply-add. Computes `(a * b) + c` with only one rounding error. -/// -/// This produces a more accurate result with better performance (on some -/// architectures) than a separate multiplication operation followed by an add. -#[inline(always)] pub fn mul_add(a: T, b: T, c: T) -> T { a.mul_add(b, c) } - -/// Raise a number to a power. -/// -/// # Example -/// -/// ```rust -/// use std::num; -/// -/// let sixteen: f64 = num::powf(2.0, 4.0); -/// assert_eq!(sixteen, 16.0); -/// ``` -#[inline(always)] pub fn powf(value: T, n: T) -> T { value.powf(&n) } -/// Take the square root of a number. -#[inline(always)] pub fn sqrt(value: T) -> T { value.sqrt() } -/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`. -#[inline(always)] pub fn rsqrt(value: T) -> T { value.rsqrt() } -/// Take the cubic root of a number. -#[inline(always)] pub fn cbrt(value: T) -> T { value.cbrt() } -/// Calculate the length of the hypotenuse of a right-angle triangle given legs -/// of length `x` and `y`. -#[inline(always)] pub fn hypot(x: T, y: T) -> T { x.hypot(&y) } -/// Sine function. -#[inline(always)] pub fn sin(value: T) -> T { value.sin() } -/// Cosine function. -#[inline(always)] pub fn cos(value: T) -> T { value.cos() } -/// Tangent function. -#[inline(always)] pub fn tan(value: T) -> T { value.tan() } -/// Compute the arcsine of the number. -#[inline(always)] pub fn asin(value: T) -> T { value.asin() } -/// Compute the arccosine of the number. -#[inline(always)] pub fn acos(value: T) -> T { value.acos() } -/// Compute the arctangent of the number. -#[inline(always)] pub fn atan(value: T) -> T { value.atan() } -/// Compute the arctangent with 2 arguments. -#[inline(always)] pub fn atan2(x: T, y: T) -> T { x.atan2(&y) } -/// Simultaneously computes the sine and cosine of the number. -#[inline(always)] pub fn sin_cos(value: T) -> (T, T) { value.sin_cos() } -/// Returns `e^(value)`, (the exponential function). -#[inline(always)] pub fn exp(value: T) -> T { value.exp() } -/// Returns 2 raised to the power of the number, `2^(value)`. -#[inline(always)] pub fn exp2(value: T) -> T { value.exp2() } -/// Returns the natural logarithm of the number. -#[inline(always)] pub fn ln(value: T) -> T { value.ln() } -/// Returns the logarithm of the number with respect to an arbitrary base. -#[inline(always)] pub fn log(value: T, base: T) -> T { value.log(&base) } -/// Returns the base 2 logarithm of the number. -#[inline(always)] pub fn log2(value: T) -> T { value.log2() } -/// Returns the base 10 logarithm of the number. -#[inline(always)] pub fn log10(value: T) -> T { value.log10() } -/// Hyperbolic sine function. -#[inline(always)] pub fn sinh(value: T) -> T { value.sinh() } -/// Hyperbolic cosine function. -#[inline(always)] pub fn cosh(value: T) -> T { value.cosh() } -/// Hyperbolic tangent function. -#[inline(always)] pub fn tanh(value: T) -> T { value.tanh() } -/// Inverse hyperbolic sine function. -#[inline(always)] pub fn asinh(value: T) -> T { value.asinh() } -/// Inverse hyperbolic cosine function. -#[inline(always)] pub fn acosh(value: T) -> T { value.acosh() } -/// Inverse hyperbolic tangent function. -#[inline(always)] pub fn atanh(value: T) -> T { value.atanh() } - /// A generic trait for converting a value to a number. pub trait ToPrimitive { /// Converts the value of `self` to an `int`.