Skip to content

Commit

Permalink
auto merge of #13597 : bjz/rust/float-api, r=brson
Browse files Browse the repository at this point in the history
This pull request:

- Merges the `Round` trait into the `Float` trait, continuing issue #10387.
- Has floating point functions take their parameters by value.
- Cleans up the formatting and organisation in the definition and implementations of the `Float` trait.

More information on the breaking changes can be found in the commit messages.
  • Loading branch information
bors committed Apr 23, 2014
2 parents 696f16e + 2d9dfc6 commit 30fe550
Show file tree
Hide file tree
Showing 10 changed files with 572 additions and 483 deletions.
4 changes: 2 additions & 2 deletions src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ be distributed on the available cores.
fn partial_sum(start: uint) -> f64 {
let mut local_sum = 0f64;
for num in range(start*100000, (start+1)*100000) {
local_sum += (num as f64 + 1.0).powf(&-2.0);
local_sum += (num as f64 + 1.0).powf(-2.0);
}
local_sum
}
Expand Down Expand Up @@ -343,7 +343,7 @@ extern crate sync;
use sync::Arc;
fn pnorm(nums: &[f64], p: uint) -> f64 {
nums.iter().fold(0.0, |a,b| a+(*b).powf(&(p as f64)) ).powf(&(1.0 / (p as f64)))
nums.iter().fold(0.0, |a, b| a + b.powf(p as f64)).powf(1.0 / (p as f64))
}
fn main() {
Expand Down
4 changes: 2 additions & 2 deletions src/libnum/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ impl<T: Clone + Float> Cmplx<T> {
/// Calculate |self|
#[inline]
pub fn norm(&self) -> T {
self.re.hypot(&self.im)
self.re.hypot(self.im)
}
}

impl<T: Clone + Float> Cmplx<T> {
/// Calculate the principal Arg of self.
#[inline]
pub fn arg(&self) -> T {
self.im.atan2(&self.re)
self.im.atan2(self.re)
}
/// Convert to polar form (r, theta), such that `self = r * exp(i
/// * theta)`
Expand Down
87 changes: 41 additions & 46 deletions src/libnum/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use Integer;
use std::cmp;
use std::fmt;
use std::from_str::FromStr;
use std::num::{Zero,One,ToStrRadix,FromStrRadix,Round};
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
use bigint::{BigInt, BigUint, Sign, Plus, Minus};

/// Represents the ratio between 2 numbers.
Expand Down Expand Up @@ -113,6 +113,40 @@ impl<T: Clone + Integer + Ord>
pub fn recip(&self) -> Ratio<T> {
Ratio::new_raw(self.denom.clone(), self.numer.clone())
}

pub fn floor(&self) -> Ratio<T> {
if *self < Zero::zero() {
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
} else {
Ratio::from_integer(self.numer / self.denom)
}
}

pub fn ceil(&self) -> Ratio<T> {
if *self < Zero::zero() {
Ratio::from_integer(self.numer / self.denom)
} else {
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
}
}

#[inline]
pub fn round(&self) -> Ratio<T> {
if *self < Zero::zero() {
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
} else {
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
}
}

#[inline]
pub fn trunc(&self) -> Ratio<T> {
Ratio::from_integer(self.numer / self.denom)
}

pub fn fract(&self) -> Ratio<T> {
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
}
}

impl Ratio<BigInt> {
Expand Down Expand Up @@ -238,45 +272,6 @@ impl<T: Clone + Integer + Ord>
impl<T: Clone + Integer + Ord>
Num for Ratio<T> {}

/* Utils */
impl<T: Clone + Integer + Ord>
Round for Ratio<T> {

fn floor(&self) -> Ratio<T> {
if *self < Zero::zero() {
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
} else {
Ratio::from_integer(self.numer / self.denom)
}
}

fn ceil(&self) -> Ratio<T> {
if *self < Zero::zero() {
Ratio::from_integer(self.numer / self.denom)
} else {
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
}
}

#[inline]
fn round(&self) -> Ratio<T> {
if *self < Zero::zero() {
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
} else {
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
}
}

#[inline]
fn trunc(&self) -> Ratio<T> {
Ratio::from_integer(self.numer / self.denom)
}

fn fract(&self) -> Ratio<T> {
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
}
}

/* String conversions */
impl<T: fmt::Show> fmt::Show for Ratio<T> {
/// Renders as `numer/denom`.
Expand Down Expand Up @@ -636,19 +631,19 @@ mod test {

// f32
test(3.14159265359f32, ("13176795", "4194304"));
test(2f32.powf(&100.), ("1267650600228229401496703205376", "1"));
test(-2f32.powf(&100.), ("-1267650600228229401496703205376", "1"));
test(1.0 / 2f32.powf(&100.), ("1", "1267650600228229401496703205376"));
test(2f32.powf(100.), ("1267650600228229401496703205376", "1"));
test(-2f32.powf(100.), ("-1267650600228229401496703205376", "1"));
test(1.0 / 2f32.powf(100.), ("1", "1267650600228229401496703205376"));
test(684729.48391f32, ("1369459", "2"));
test(-8573.5918555f32, ("-4389679", "512"));

// f64
test(3.14159265359f64, ("3537118876014453", "1125899906842624"));
test(2f64.powf(&100.), ("1267650600228229401496703205376", "1"));
test(-2f64.powf(&100.), ("-1267650600228229401496703205376", "1"));
test(2f64.powf(100.), ("1267650600228229401496703205376", "1"));
test(-2f64.powf(100.), ("-1267650600228229401496703205376", "1"));
test(684729.48391f64, ("367611342500051", "536870912"));
test(-8573.5918555, ("-4713381968463931", "549755813888"));
test(1.0 / 2f64.powf(&100.), ("1", "1267650600228229401496703205376"));
test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/librand/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,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) * u.powf(&self.inv_shape)
self.large_shape.ind_sample(rng) * u.powf(self.inv_shape)
}
}
impl IndependentSample<f64> for GammaLargeShape {
Expand Down
Loading

0 comments on commit 30fe550

Please sign in to comment.