Skip to content

Commit 8ca5caf

Browse files
committed
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.
1 parent e63b2d3 commit 8ca5caf

File tree

5 files changed

+10
-88
lines changed

5 files changed

+10
-88
lines changed

src/doc/guide-pointers.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,6 @@ sense, they're simple: just keep whatever ownership the data already has. For
332332
example:
333333

334334
~~~rust
335-
use std::num::sqrt;
336-
337335
struct Point {
338336
x: f32,
339337
y: f32,
@@ -343,7 +341,7 @@ fn compute_distance(p1: &Point, p2: &Point) -> f32 {
343341
let x_d = p1.x - p2.x;
344342
let y_d = p1.y - p2.y;
345343

346-
sqrt(x_d * x_d + y_d * y_d)
344+
(x_d * x_d + y_d * y_d).sqrt()
347345
}
348346

349347
fn main() {

src/doc/rust.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,14 +826,14 @@ Use declarations support a number of convenient shortcuts:
826826
An example of `use` declarations:
827827

828828
~~~~
829-
use std::num::sin;
829+
use std::iter::range_step;
830830
use std::option::{Some, None};
831831
832832
# fn foo<T>(_: T){}
833833
834834
fn main() {
835-
// Equivalent to 'std::num::sin(1.0);'
836-
sin(1.0);
835+
// Equivalent to 'std::iter::range_step(0, 10, 2);'
836+
range_step(0, 10, 2);
837837
838838
// Equivalent to 'foo(~[std::option::Some(1.0), std::option::None]);'
839839
foo(~[Some(1.0), None]);

src/doc/tutorial.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,12 @@ matching in order to bind names to the contents of data types.
504504
505505
~~~~
506506
use std::f64;
507-
use std::num::atan;
508507
fn angle(vector: (f64, f64)) -> f64 {
509508
let pi = f64::consts::PI;
510509
match vector {
511510
(0.0, y) if y < 0.0 => 1.5 * pi,
512511
(0.0, _) => 0.5 * pi,
513-
(x, y) => atan(y / x)
512+
(x, y) => (y / x).atan()
514513
}
515514
}
516515
~~~~
@@ -1430,12 +1429,11 @@ bad, but often copies are expensive. So we’d like to define a function
14301429
that takes the points by pointer. We can use references to do this:
14311430
14321431
~~~
1433-
use std::num::sqrt;
14341432
# struct Point { x: f64, y: f64 }
14351433
fn compute_distance(p1: &Point, p2: &Point) -> f64 {
14361434
let x_d = p1.x - p2.x;
14371435
let y_d = p1.y - p2.y;
1438-
sqrt(x_d * x_d + y_d * y_d)
1436+
(x_d * x_d + y_d * y_d).sqrt()
14391437
}
14401438
~~~
14411439
@@ -2303,7 +2301,7 @@ impl Shape for Circle {
23032301
fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
23042302
}
23052303
impl Shape for Square {
2306-
fn new(area: f64) -> Square { Square { length: (area).sqrt() } }
2304+
fn new(area: f64) -> Square { Square { length: area.sqrt() } }
23072305
}
23082306
23092307
let area = 42.5;

src/librand/distributions/gamma.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! The Gamma and derived distributions.
1212
1313
use std::num::Float;
14-
use std::num;
1514
use {Rng, Open01};
1615
use super::normal::StandardNormal;
1716
use super::{IndependentSample, Sample, Exp};
@@ -114,7 +113,7 @@ impl GammaLargeShape {
114113
GammaLargeShape {
115114
shape: shape,
116115
scale: scale,
117-
c: 1. / num::sqrt(9. * d),
116+
c: 1. / (9. * d).sqrt(),
118117
d: d
119118
}
120119
}
@@ -143,7 +142,7 @@ impl IndependentSample<f64> for GammaSmallShape {
143142
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
144143
let Open01(u) = rng.gen::<Open01<f64>>();
145144

146-
self.large_shape.ind_sample(rng) * num::powf(u, self.inv_shape)
145+
self.large_shape.ind_sample(rng) * u.powf(&self.inv_shape)
147146
}
148147
}
149148
impl IndependentSample<f64> for GammaLargeShape {
@@ -160,7 +159,7 @@ impl IndependentSample<f64> for GammaLargeShape {
160159

161160
let x_sqr = x * x;
162161
if u < 1.0 - 0.0331 * x_sqr * x_sqr ||
163-
num::ln(u) < 0.5 * x_sqr + self.d * (1.0 - v + num::ln(v)) {
162+
u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) {
164163
return self.d * v * self.scale
165164
}
166165
}

src/libstd/num/mod.rs

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -553,79 +553,6 @@ pub trait Float: Signed + Round + Primitive {
553553
fn to_radians(&self) -> Self;
554554
}
555555

556-
/// Returns the exponential of the number, minus `1`, `exp(n) - 1`, in a way
557-
/// that is accurate even if the number is close to zero.
558-
#[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() }
559-
/// Returns the natural logarithm of the number plus `1`, `ln(n + 1)`, more
560-
/// accurately than if the operations were performed separately.
561-
#[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() }
562-
/// Fused multiply-add. Computes `(a * b) + c` with only one rounding error.
563-
///
564-
/// This produces a more accurate result with better performance (on some
565-
/// architectures) than a separate multiplication operation followed by an add.
566-
#[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
567-
568-
/// Raise a number to a power.
569-
///
570-
/// # Example
571-
///
572-
/// ```rust
573-
/// use std::num;
574-
///
575-
/// let sixteen: f64 = num::powf(2.0, 4.0);
576-
/// assert_eq!(sixteen, 16.0);
577-
/// ```
578-
#[inline(always)] pub fn powf<T: Float>(value: T, n: T) -> T { value.powf(&n) }
579-
/// Take the square root of a number.
580-
#[inline(always)] pub fn sqrt<T: Float>(value: T) -> T { value.sqrt() }
581-
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
582-
#[inline(always)] pub fn rsqrt<T: Float>(value: T) -> T { value.rsqrt() }
583-
/// Take the cubic root of a number.
584-
#[inline(always)] pub fn cbrt<T: Float>(value: T) -> T { value.cbrt() }
585-
/// Calculate the length of the hypotenuse of a right-angle triangle given legs
586-
/// of length `x` and `y`.
587-
#[inline(always)] pub fn hypot<T: Float>(x: T, y: T) -> T { x.hypot(&y) }
588-
/// Sine function.
589-
#[inline(always)] pub fn sin<T: Float>(value: T) -> T { value.sin() }
590-
/// Cosine function.
591-
#[inline(always)] pub fn cos<T: Float>(value: T) -> T { value.cos() }
592-
/// Tangent function.
593-
#[inline(always)] pub fn tan<T: Float>(value: T) -> T { value.tan() }
594-
/// Compute the arcsine of the number.
595-
#[inline(always)] pub fn asin<T: Float>(value: T) -> T { value.asin() }
596-
/// Compute the arccosine of the number.
597-
#[inline(always)] pub fn acos<T: Float>(value: T) -> T { value.acos() }
598-
/// Compute the arctangent of the number.
599-
#[inline(always)] pub fn atan<T: Float>(value: T) -> T { value.atan() }
600-
/// Compute the arctangent with 2 arguments.
601-
#[inline(always)] pub fn atan2<T: Float>(x: T, y: T) -> T { x.atan2(&y) }
602-
/// Simultaneously computes the sine and cosine of the number.
603-
#[inline(always)] pub fn sin_cos<T: Float>(value: T) -> (T, T) { value.sin_cos() }
604-
/// Returns `e^(value)`, (the exponential function).
605-
#[inline(always)] pub fn exp<T: Float>(value: T) -> T { value.exp() }
606-
/// Returns 2 raised to the power of the number, `2^(value)`.
607-
#[inline(always)] pub fn exp2<T: Float>(value: T) -> T { value.exp2() }
608-
/// Returns the natural logarithm of the number.
609-
#[inline(always)] pub fn ln<T: Float>(value: T) -> T { value.ln() }
610-
/// Returns the logarithm of the number with respect to an arbitrary base.
611-
#[inline(always)] pub fn log<T: Float>(value: T, base: T) -> T { value.log(&base) }
612-
/// Returns the base 2 logarithm of the number.
613-
#[inline(always)] pub fn log2<T: Float>(value: T) -> T { value.log2() }
614-
/// Returns the base 10 logarithm of the number.
615-
#[inline(always)] pub fn log10<T: Float>(value: T) -> T { value.log10() }
616-
/// Hyperbolic sine function.
617-
#[inline(always)] pub fn sinh<T: Float>(value: T) -> T { value.sinh() }
618-
/// Hyperbolic cosine function.
619-
#[inline(always)] pub fn cosh<T: Float>(value: T) -> T { value.cosh() }
620-
/// Hyperbolic tangent function.
621-
#[inline(always)] pub fn tanh<T: Float>(value: T) -> T { value.tanh() }
622-
/// Inverse hyperbolic sine function.
623-
#[inline(always)] pub fn asinh<T: Float>(value: T) -> T { value.asinh() }
624-
/// Inverse hyperbolic cosine function.
625-
#[inline(always)] pub fn acosh<T: Float>(value: T) -> T { value.acosh() }
626-
/// Inverse hyperbolic tangent function.
627-
#[inline(always)] pub fn atanh<T: Float>(value: T) -> T { value.atanh() }
628-
629556
/// A generic trait for converting a value to a number.
630557
pub trait ToPrimitive {
631558
/// Converts the value of `self` to an `int`.

0 commit comments

Comments
 (0)