Skip to content

Commit 1217cfb

Browse files
committed
auto merge of #13225 : thestinger/rust/num, r=cmr
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.
2 parents b71c02e + 5e12e1b commit 1217cfb

File tree

9 files changed

+136
-293
lines changed

9 files changed

+136
-293
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/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ pub mod raw;
206206
/* For internal use, not exported */
207207

208208
mod unicode;
209-
#[path = "num/cmath.rs"]
210-
mod cmath;
211209

212210
// FIXME #7809: This shouldn't be pub, and it should be reexported under 'unstable'
213211
// but name resolution doesn't work without it being pub.

src/libstd/num/cmath.rs

Lines changed: 0 additions & 151 deletions
This file was deleted.

src/libstd/num/f32.rs

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use prelude::*;
1616

17-
use cmath;
1817
use default::Default;
1918
use from_str::FromStr;
2019
use libc::{c_float, c_int};
@@ -23,6 +22,46 @@ use num::{Zero, One, Bounded, strconv};
2322
use num;
2423
use intrinsics;
2524

25+
#[allow(dead_code)]
26+
mod cmath {
27+
use libc::{c_float, c_int};
28+
29+
#[link_name = "m"]
30+
extern {
31+
pub fn acosf(n: c_float) -> c_float;
32+
pub fn asinf(n: c_float) -> c_float;
33+
pub fn atanf(n: c_float) -> c_float;
34+
pub fn atan2f(a: c_float, b: c_float) -> c_float;
35+
pub fn cbrtf(n: c_float) -> c_float;
36+
pub fn coshf(n: c_float) -> c_float;
37+
pub fn erff(n: c_float) -> c_float;
38+
pub fn erfcf(n: c_float) -> c_float;
39+
pub fn expm1f(n: c_float) -> c_float;
40+
pub fn fdimf(a: c_float, b: c_float) -> c_float;
41+
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
42+
pub fn fmaxf(a: c_float, b: c_float) -> c_float;
43+
pub fn fminf(a: c_float, b: c_float) -> c_float;
44+
pub fn nextafterf(x: c_float, y: c_float) -> c_float;
45+
pub fn hypotf(x: c_float, y: c_float) -> c_float;
46+
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
47+
pub fn logbf(n: c_float) -> c_float;
48+
pub fn log1pf(n: c_float) -> c_float;
49+
pub fn ilogbf(n: c_float) -> c_int;
50+
pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
51+
pub fn sinhf(n: c_float) -> c_float;
52+
pub fn tanf(n: c_float) -> c_float;
53+
pub fn tanhf(n: c_float) -> c_float;
54+
pub fn tgammaf(n: c_float) -> c_float;
55+
56+
#[cfg(unix)]
57+
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
58+
59+
#[cfg(windows)]
60+
#[link_name="__lgammaf_r"]
61+
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
62+
}
63+
}
64+
2665
macro_rules! delegate(
2766
(
2867
$(
@@ -66,29 +105,22 @@ delegate!(
66105
fn nearbyint(n: f32) -> f32 = intrinsics::nearbyintf32,
67106
fn round(n: f32) -> f32 = intrinsics::roundf32,
68107

69-
// cmath
70-
fn acos(n: c_float) -> c_float = cmath::c_float::acos,
71-
fn asin(n: c_float) -> c_float = cmath::c_float::asin,
72-
fn atan(n: c_float) -> c_float = cmath::c_float::atan,
73-
fn atan2(a: c_float, b: c_float) -> c_float = cmath::c_float::atan2,
74-
fn cbrt(n: c_float) -> c_float = cmath::c_float::cbrt,
75-
fn cosh(n: c_float) -> c_float = cmath::c_float::cosh,
76-
// fn erf(n: c_float) -> c_float = cmath::c_float::erf,
77-
// fn erfc(n: c_float) -> c_float = cmath::c_float::erfc,
78-
fn exp_m1(n: c_float) -> c_float = cmath::c_float::exp_m1,
79-
fn abs_sub(a: c_float, b: c_float) -> c_float = cmath::c_float::abs_sub,
80-
fn next_after(x: c_float, y: c_float) -> c_float = cmath::c_float::next_after,
81-
fn frexp(n: c_float, value: &mut c_int) -> c_float = cmath::c_float::frexp,
82-
fn hypot(x: c_float, y: c_float) -> c_float = cmath::c_float::hypot,
83-
fn ldexp(x: c_float, n: c_int) -> c_float = cmath::c_float::ldexp,
84-
// fn log_radix(n: c_float) -> c_float = cmath::c_float::log_radix,
85-
fn ln_1p(n: c_float) -> c_float = cmath::c_float::ln_1p,
86-
// fn ilog_radix(n: c_float) -> c_int = cmath::c_float::ilog_radix,
87-
// fn modf(n: c_float, iptr: &mut c_float) -> c_float = cmath::c_float::modf,
88-
// fn ldexp_radix(n: c_float, i: c_int) -> c_float = cmath::c_float::ldexp_radix,
89-
fn sinh(n: c_float) -> c_float = cmath::c_float::sinh,
90-
fn tan(n: c_float) -> c_float = cmath::c_float::tan,
91-
fn tanh(n: c_float) -> c_float = cmath::c_float::tanh
108+
fn acos(n: c_float) -> c_float = cmath::acosf,
109+
fn asin(n: c_float) -> c_float = cmath::asinf,
110+
fn atan(n: c_float) -> c_float = cmath::atanf,
111+
fn atan2(a: c_float, b: c_float) -> c_float = cmath::atan2f,
112+
fn cbrt(n: c_float) -> c_float = cmath::cbrtf,
113+
fn cosh(n: c_float) -> c_float = cmath::coshf,
114+
fn exp_m1(n: c_float) -> c_float = cmath::expm1f,
115+
fn abs_sub(a: c_float, b: c_float) -> c_float = cmath::fdimf,
116+
fn next_after(x: c_float, y: c_float) -> c_float = cmath::nextafterf,
117+
fn frexp(n: c_float, value: &mut c_int) -> c_float = cmath::frexpf,
118+
fn hypot(x: c_float, y: c_float) -> c_float = cmath::hypotf,
119+
fn ldexp(x: c_float, n: c_int) -> c_float = cmath::ldexpf,
120+
fn ln_1p(n: c_float) -> c_float = cmath::log1pf,
121+
fn sinh(n: c_float) -> c_float = cmath::sinhf,
122+
fn tan(n: c_float) -> c_float = cmath::tanf,
123+
fn tanh(n: c_float) -> c_float = cmath::tanhf
92124
)
93125

94126
// FIXME(#11621): These constants should be deprecated once CTFE is implemented
@@ -308,12 +340,12 @@ impl Primitive for f32 {}
308340
impl Float for f32 {
309341
#[inline]
310342
fn max(self, other: f32) -> f32 {
311-
unsafe { cmath::c_float::fmax(self, other) }
343+
unsafe { cmath::fmaxf(self, other) }
312344
}
313345

314346
#[inline]
315347
fn min(self, other: f32) -> f32 {
316-
unsafe { cmath::c_float::fmin(self, other) }
348+
unsafe { cmath::fminf(self, other) }
317349
}
318350

319351
#[inline]

0 commit comments

Comments
 (0)