Skip to content

num: rm wrapping of Float methods as functions #13225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/doc/guide-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>(_: 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]);
Expand Down
8 changes: 3 additions & 5 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
~~~~
Expand Down Expand Up @@ -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()
}
~~~

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/librand/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -114,7 +113,7 @@ impl GammaLargeShape {
GammaLargeShape {
shape: shape,
scale: scale,
c: 1. / num::sqrt(9. * d),
c: 1. / (9. * d).sqrt(),
d: d
}
}
Expand Down Expand Up @@ -143,7 +142,7 @@ impl IndependentSample<f64> for GammaSmallShape {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
let Open01(u) = rng.gen::<Open01<f64>>();

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<f64> for GammaLargeShape {
Expand All @@ -160,7 +159,7 @@ impl IndependentSample<f64> 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
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ pub mod raw;
/* For internal use, not exported */

mod unicode;
#[path = "num/cmath.rs"]
mod cmath;

// FIXME #7809: This shouldn't be pub, and it should be reexported under 'unstable'
// but name resolution doesn't work without it being pub.
Expand Down
151 changes: 0 additions & 151 deletions src/libstd/num/cmath.rs

This file was deleted.

84 changes: 58 additions & 26 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use prelude::*;

use cmath;
use default::Default;
use from_str::FromStr;
use libc::{c_float, c_int};
Expand All @@ -23,6 +22,46 @@ use num::{Zero, One, Bounded, strconv};
use num;
use intrinsics;

#[allow(dead_code)]
mod cmath {
use libc::{c_float, c_int};

#[link_name = "m"]
extern {
pub fn acosf(n: c_float) -> c_float;
pub fn asinf(n: c_float) -> c_float;
pub fn atanf(n: c_float) -> c_float;
pub fn atan2f(a: c_float, b: c_float) -> c_float;
pub fn cbrtf(n: c_float) -> c_float;
pub fn coshf(n: c_float) -> c_float;
pub fn erff(n: c_float) -> c_float;
pub fn erfcf(n: c_float) -> c_float;
pub fn expm1f(n: c_float) -> c_float;
pub fn fdimf(a: c_float, b: c_float) -> c_float;
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
pub fn fmaxf(a: c_float, b: c_float) -> c_float;
pub fn fminf(a: c_float, b: c_float) -> c_float;
pub fn nextafterf(x: c_float, y: c_float) -> c_float;
pub fn hypotf(x: c_float, y: c_float) -> c_float;
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
pub fn logbf(n: c_float) -> c_float;
pub fn log1pf(n: c_float) -> c_float;
pub fn ilogbf(n: c_float) -> c_int;
pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
pub fn sinhf(n: c_float) -> c_float;
pub fn tanf(n: c_float) -> c_float;
pub fn tanhf(n: c_float) -> c_float;
pub fn tgammaf(n: c_float) -> c_float;

#[cfg(unix)]
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;

#[cfg(windows)]
#[link_name="__lgammaf_r"]
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
}
}

macro_rules! delegate(
(
$(
Expand Down Expand Up @@ -66,29 +105,22 @@ delegate!(
fn nearbyint(n: f32) -> f32 = intrinsics::nearbyintf32,
fn round(n: f32) -> f32 = intrinsics::roundf32,

// cmath
fn acos(n: c_float) -> c_float = cmath::c_float::acos,
fn asin(n: c_float) -> c_float = cmath::c_float::asin,
fn atan(n: c_float) -> c_float = cmath::c_float::atan,
fn atan2(a: c_float, b: c_float) -> c_float = cmath::c_float::atan2,
fn cbrt(n: c_float) -> c_float = cmath::c_float::cbrt,
fn cosh(n: c_float) -> c_float = cmath::c_float::cosh,
// fn erf(n: c_float) -> c_float = cmath::c_float::erf,
// fn erfc(n: c_float) -> c_float = cmath::c_float::erfc,
fn exp_m1(n: c_float) -> c_float = cmath::c_float::exp_m1,
fn abs_sub(a: c_float, b: c_float) -> c_float = cmath::c_float::abs_sub,
fn next_after(x: c_float, y: c_float) -> c_float = cmath::c_float::next_after,
fn frexp(n: c_float, value: &mut c_int) -> c_float = cmath::c_float::frexp,
fn hypot(x: c_float, y: c_float) -> c_float = cmath::c_float::hypot,
fn ldexp(x: c_float, n: c_int) -> c_float = cmath::c_float::ldexp,
// fn log_radix(n: c_float) -> c_float = cmath::c_float::log_radix,
fn ln_1p(n: c_float) -> c_float = cmath::c_float::ln_1p,
// fn ilog_radix(n: c_float) -> c_int = cmath::c_float::ilog_radix,
// fn modf(n: c_float, iptr: &mut c_float) -> c_float = cmath::c_float::modf,
// fn ldexp_radix(n: c_float, i: c_int) -> c_float = cmath::c_float::ldexp_radix,
fn sinh(n: c_float) -> c_float = cmath::c_float::sinh,
fn tan(n: c_float) -> c_float = cmath::c_float::tan,
fn tanh(n: c_float) -> c_float = cmath::c_float::tanh
fn acos(n: c_float) -> c_float = cmath::acosf,
fn asin(n: c_float) -> c_float = cmath::asinf,
fn atan(n: c_float) -> c_float = cmath::atanf,
fn atan2(a: c_float, b: c_float) -> c_float = cmath::atan2f,
fn cbrt(n: c_float) -> c_float = cmath::cbrtf,
fn cosh(n: c_float) -> c_float = cmath::coshf,
fn exp_m1(n: c_float) -> c_float = cmath::expm1f,
fn abs_sub(a: c_float, b: c_float) -> c_float = cmath::fdimf,
fn next_after(x: c_float, y: c_float) -> c_float = cmath::nextafterf,
fn frexp(n: c_float, value: &mut c_int) -> c_float = cmath::frexpf,
fn hypot(x: c_float, y: c_float) -> c_float = cmath::hypotf,
fn ldexp(x: c_float, n: c_int) -> c_float = cmath::ldexpf,
fn ln_1p(n: c_float) -> c_float = cmath::log1pf,
fn sinh(n: c_float) -> c_float = cmath::sinhf,
fn tan(n: c_float) -> c_float = cmath::tanf,
fn tanh(n: c_float) -> c_float = cmath::tanhf
)

// FIXME(#11621): These constants should be deprecated once CTFE is implemented
Expand Down Expand Up @@ -308,12 +340,12 @@ impl Primitive for f32 {}
impl Float for f32 {
#[inline]
fn max(self, other: f32) -> f32 {
unsafe { cmath::c_float::fmax(self, other) }
unsafe { cmath::fmaxf(self, other) }
}

#[inline]
fn min(self, other: f32) -> f32 {
unsafe { cmath::c_float::fmin(self, other) }
unsafe { cmath::fminf(self, other) }
}

#[inline]
Expand Down
Loading