Skip to content

Commit 30fe550

Browse files
committed
auto merge of #13597 : bjz/rust/float-api, r=brson
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.
2 parents 696f16e + 2d9dfc6 commit 30fe550

File tree

10 files changed

+572
-483
lines changed

10 files changed

+572
-483
lines changed

src/doc/guide-tasks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ be distributed on the available cores.
306306
fn partial_sum(start: uint) -> f64 {
307307
let mut local_sum = 0f64;
308308
for num in range(start*100000, (start+1)*100000) {
309-
local_sum += (num as f64 + 1.0).powf(&-2.0);
309+
local_sum += (num as f64 + 1.0).powf(-2.0);
310310
}
311311
local_sum
312312
}
@@ -343,7 +343,7 @@ extern crate sync;
343343
use sync::Arc;
344344
345345
fn pnorm(nums: &[f64], p: uint) -> f64 {
346-
nums.iter().fold(0.0, |a,b| a+(*b).powf(&(p as f64)) ).powf(&(1.0 / (p as f64)))
346+
nums.iter().fold(0.0, |a, b| a + b.powf(p as f64)).powf(1.0 / (p as f64))
347347
}
348348
349349
fn main() {

src/libnum/complex.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ impl<T: Clone + Float> Cmplx<T> {
8282
/// Calculate |self|
8383
#[inline]
8484
pub fn norm(&self) -> T {
85-
self.re.hypot(&self.im)
85+
self.re.hypot(self.im)
8686
}
8787
}
8888

8989
impl<T: Clone + Float> Cmplx<T> {
9090
/// Calculate the principal Arg of self.
9191
#[inline]
9292
pub fn arg(&self) -> T {
93-
self.im.atan2(&self.re)
93+
self.im.atan2(self.re)
9494
}
9595
/// Convert to polar form (r, theta), such that `self = r * exp(i
9696
/// * theta)`

src/libnum/rational.rs

+41-46
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use Integer;
1515
use std::cmp;
1616
use std::fmt;
1717
use std::from_str::FromStr;
18-
use std::num::{Zero,One,ToStrRadix,FromStrRadix,Round};
18+
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
1919
use bigint::{BigInt, BigUint, Sign, Plus, Minus};
2020

2121
/// Represents the ratio between 2 numbers.
@@ -113,6 +113,40 @@ impl<T: Clone + Integer + Ord>
113113
pub fn recip(&self) -> Ratio<T> {
114114
Ratio::new_raw(self.denom.clone(), self.numer.clone())
115115
}
116+
117+
pub fn floor(&self) -> Ratio<T> {
118+
if *self < Zero::zero() {
119+
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
120+
} else {
121+
Ratio::from_integer(self.numer / self.denom)
122+
}
123+
}
124+
125+
pub fn ceil(&self) -> Ratio<T> {
126+
if *self < Zero::zero() {
127+
Ratio::from_integer(self.numer / self.denom)
128+
} else {
129+
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
130+
}
131+
}
132+
133+
#[inline]
134+
pub fn round(&self) -> Ratio<T> {
135+
if *self < Zero::zero() {
136+
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
137+
} else {
138+
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
139+
}
140+
}
141+
142+
#[inline]
143+
pub fn trunc(&self) -> Ratio<T> {
144+
Ratio::from_integer(self.numer / self.denom)
145+
}
146+
147+
pub fn fract(&self) -> Ratio<T> {
148+
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
149+
}
116150
}
117151

118152
impl Ratio<BigInt> {
@@ -238,45 +272,6 @@ impl<T: Clone + Integer + Ord>
238272
impl<T: Clone + Integer + Ord>
239273
Num for Ratio<T> {}
240274

241-
/* Utils */
242-
impl<T: Clone + Integer + Ord>
243-
Round for Ratio<T> {
244-
245-
fn floor(&self) -> Ratio<T> {
246-
if *self < Zero::zero() {
247-
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
248-
} else {
249-
Ratio::from_integer(self.numer / self.denom)
250-
}
251-
}
252-
253-
fn ceil(&self) -> Ratio<T> {
254-
if *self < Zero::zero() {
255-
Ratio::from_integer(self.numer / self.denom)
256-
} else {
257-
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
258-
}
259-
}
260-
261-
#[inline]
262-
fn round(&self) -> Ratio<T> {
263-
if *self < Zero::zero() {
264-
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
265-
} else {
266-
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
267-
}
268-
}
269-
270-
#[inline]
271-
fn trunc(&self) -> Ratio<T> {
272-
Ratio::from_integer(self.numer / self.denom)
273-
}
274-
275-
fn fract(&self) -> Ratio<T> {
276-
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
277-
}
278-
}
279-
280275
/* String conversions */
281276
impl<T: fmt::Show> fmt::Show for Ratio<T> {
282277
/// Renders as `numer/denom`.
@@ -636,19 +631,19 @@ mod test {
636631

637632
// f32
638633
test(3.14159265359f32, ("13176795", "4194304"));
639-
test(2f32.powf(&100.), ("1267650600228229401496703205376", "1"));
640-
test(-2f32.powf(&100.), ("-1267650600228229401496703205376", "1"));
641-
test(1.0 / 2f32.powf(&100.), ("1", "1267650600228229401496703205376"));
634+
test(2f32.powf(100.), ("1267650600228229401496703205376", "1"));
635+
test(-2f32.powf(100.), ("-1267650600228229401496703205376", "1"));
636+
test(1.0 / 2f32.powf(100.), ("1", "1267650600228229401496703205376"));
642637
test(684729.48391f32, ("1369459", "2"));
643638
test(-8573.5918555f32, ("-4389679", "512"));
644639

645640
// f64
646641
test(3.14159265359f64, ("3537118876014453", "1125899906842624"));
647-
test(2f64.powf(&100.), ("1267650600228229401496703205376", "1"));
648-
test(-2f64.powf(&100.), ("-1267650600228229401496703205376", "1"));
642+
test(2f64.powf(100.), ("1267650600228229401496703205376", "1"));
643+
test(-2f64.powf(100.), ("-1267650600228229401496703205376", "1"));
649644
test(684729.48391f64, ("367611342500051", "536870912"));
650645
test(-8573.5918555, ("-4713381968463931", "549755813888"));
651-
test(1.0 / 2f64.powf(&100.), ("1", "1267650600228229401496703205376"));
646+
test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376"));
652647
}
653648

654649
#[test]

src/librand/distributions/gamma.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl IndependentSample<f64> for GammaSmallShape {
147147
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
148148
let Open01(u) = rng.gen::<Open01<f64>>();
149149

150-
self.large_shape.ind_sample(rng) * u.powf(&self.inv_shape)
150+
self.large_shape.ind_sample(rng) * u.powf(self.inv_shape)
151151
}
152152
}
153153
impl IndependentSample<f64> for GammaLargeShape {

0 commit comments

Comments
 (0)