Skip to content

Commit bed70a4

Browse files
committed
Have floating point functions take their parameters by value.
Make all of the methods in `std::num::Float` take `self` and their other parameters by value. Some of the `Float` methods took their parameters by value, and others took them by reference. This standardises them to one convention. The `Float` trait is intended for the built in IEEE 754 numbers only so we don't have to worry about the trait serving types of larger sizes. [breaking-change]
1 parent fe47202 commit bed70a4

File tree

9 files changed

+167
-167
lines changed

9 files changed

+167
-167
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

+6-6
Original file line numberDiff line numberDiff line change
@@ -631,19 +631,19 @@ mod test {
631631

632632
// f32
633633
test(3.14159265359f32, ("13176795", "4194304"));
634-
test(2f32.powf(&100.), ("1267650600228229401496703205376", "1"));
635-
test(-2f32.powf(&100.), ("-1267650600228229401496703205376", "1"));
636-
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"));
637637
test(684729.48391f32, ("1369459", "2"));
638638
test(-8573.5918555f32, ("-4389679", "512"));
639639

640640
// f64
641641
test(3.14159265359f64, ("3537118876014453", "1125899906842624"));
642-
test(2f64.powf(&100.), ("1267650600228229401496703205376", "1"));
643-
test(-2f64.powf(&100.), ("-1267650600228229401496703205376", "1"));
642+
test(2f64.powf(100.), ("1267650600228229401496703205376", "1"));
643+
test(-2f64.powf(100.), ("-1267650600228229401496703205376", "1"));
644644
test(684729.48391f64, ("367611342500051", "536870912"));
645645
test(-8573.5918555, ("-4713381968463931", "549755813888"));
646-
test(1.0 / 2f64.powf(&100.), ("1", "1267650600228229401496703205376"));
646+
test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376"));
647647
}
648648

649649
#[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 {

src/libstd/num/f32.rs

+54-54
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl Bounded for f32 {
250250
impl Primitive for f32 {}
251251

252252
impl Float for f32 {
253-
fn powi(&self, n: i32) -> f32 { unsafe{intrinsics::powif32(*self, n)} }
253+
fn powi(self, n: i32) -> f32 { unsafe{intrinsics::powif32(self, n)} }
254254

255255
#[inline]
256256
fn max(self, other: f32) -> f32 {
@@ -276,33 +276,33 @@ impl Float for f32 {
276276

277277
/// Returns `true` if the number is NaN
278278
#[inline]
279-
fn is_nan(&self) -> bool { *self != *self }
279+
fn is_nan(self) -> bool { self != self }
280280

281281
/// Returns `true` if the number is infinite
282282
#[inline]
283-
fn is_infinite(&self) -> bool {
284-
*self == Float::infinity() || *self == Float::neg_infinity()
283+
fn is_infinite(self) -> bool {
284+
self == Float::infinity() || self == Float::neg_infinity()
285285
}
286286

287287
/// Returns `true` if the number is neither infinite or NaN
288288
#[inline]
289-
fn is_finite(&self) -> bool {
289+
fn is_finite(self) -> bool {
290290
!(self.is_nan() || self.is_infinite())
291291
}
292292

293293
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN
294294
#[inline]
295-
fn is_normal(&self) -> bool {
295+
fn is_normal(self) -> bool {
296296
self.classify() == FPNormal
297297
}
298298

299299
/// Returns the floating point category of the number. If only one property is going to
300300
/// be tested, it is generally faster to use the specific predicate instead.
301-
fn classify(&self) -> FPCategory {
301+
fn classify(self) -> FPCategory {
302302
static EXP_MASK: u32 = 0x7f800000;
303303
static MAN_MASK: u32 = 0x007fffff;
304304

305-
let bits: u32 = unsafe {::cast::transmute(*self)};
305+
let bits: u32 = unsafe {::cast::transmute(self)};
306306
match (bits & MAN_MASK, bits & EXP_MASK) {
307307
(0, 0) => FPZero,
308308
(_, 0) => FPSubnormal,
@@ -342,38 +342,38 @@ impl Float for f32 {
342342
/// - `self = x * pow(2, exp)`
343343
/// - `0.5 <= abs(x) < 1.0`
344344
#[inline]
345-
fn frexp(&self) -> (f32, int) {
345+
fn frexp(self) -> (f32, int) {
346346
unsafe {
347347
let mut exp = 0;
348-
let x = cmath::frexpf(*self, &mut exp);
348+
let x = cmath::frexpf(self, &mut exp);
349349
(x, exp as int)
350350
}
351351
}
352352

353353
/// Returns the exponential of the number, minus `1`, in a way that is accurate
354354
/// even if the number is close to zero
355355
#[inline]
356-
fn exp_m1(&self) -> f32 { unsafe{cmath::expm1f(*self)} }
356+
fn exp_m1(self) -> f32 { unsafe{cmath::expm1f(self)} }
357357

358358
/// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
359359
/// than if the operations were performed separately
360360
#[inline]
361-
fn ln_1p(&self) -> f32 { unsafe{cmath::log1pf(*self)} }
361+
fn ln_1p(self) -> f32 { unsafe{cmath::log1pf(self)} }
362362

363363
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
364364
/// produces a more accurate result with better performance than a separate multiplication
365365
/// operation followed by an add.
366366
#[inline]
367-
fn mul_add(&self, a: f32, b: f32) -> f32 { unsafe{intrinsics::fmaf32(*self, a, b)} }
367+
fn mul_add(self, a: f32, b: f32) -> f32 { unsafe{intrinsics::fmaf32(self, a, b)} }
368368

369369
/// Returns the next representable floating-point value in the direction of `other`
370370
#[inline]
371-
fn next_after(&self, other: f32) -> f32 { unsafe{cmath::nextafterf(*self, other)} }
371+
fn next_after(self, other: f32) -> f32 { unsafe{cmath::nextafterf(self, other)} }
372372

373373
/// Returns the mantissa, exponent and sign as integers.
374-
fn integer_decode(&self) -> (u64, i16, i8) {
374+
fn integer_decode(self) -> (u64, i16, i8) {
375375
let bits: u32 = unsafe {
376-
::cast::transmute(*self)
376+
::cast::transmute(self)
377377
};
378378
let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
379379
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
@@ -389,19 +389,19 @@ impl Float for f32 {
389389

390390
/// Round half-way cases toward `NEG_INFINITY`
391391
#[inline]
392-
fn floor(&self) -> f32 { unsafe{intrinsics::floorf32(*self)} }
392+
fn floor(self) -> f32 { unsafe{intrinsics::floorf32(self)} }
393393

394394
/// Round half-way cases toward `INFINITY`
395395
#[inline]
396-
fn ceil(&self) -> f32 { unsafe{intrinsics::ceilf32(*self)} }
396+
fn ceil(self) -> f32 { unsafe{intrinsics::ceilf32(self)} }
397397

398398
/// Round half-way cases away from `0.0`
399399
#[inline]
400-
fn round(&self) -> f32 { unsafe{intrinsics::roundf32(*self)} }
400+
fn round(self) -> f32 { unsafe{intrinsics::roundf32(self)} }
401401

402402
/// The integer part of the number (rounds towards `0.0`)
403403
#[inline]
404-
fn trunc(&self) -> f32 { unsafe{intrinsics::truncf32(*self)} }
404+
fn trunc(self) -> f32 { unsafe{intrinsics::truncf32(self)} }
405405

406406
/// The fractional part of the number, satisfying:
407407
///
@@ -410,7 +410,7 @@ impl Float for f32 {
410410
/// assert!(x == x.trunc() + x.fract())
411411
/// ```
412412
#[inline]
413-
fn fract(&self) -> f32 { *self - self.trunc() }
413+
fn fract(self) -> f32 { self - self.trunc() }
414414

415415
/// Archimedes' constant
416416
#[inline]
@@ -482,82 +482,82 @@ impl Float for f32 {
482482

483483
/// The reciprocal (multiplicative inverse) of the number
484484
#[inline]
485-
fn recip(&self) -> f32 { 1.0 / *self }
485+
fn recip(self) -> f32 { 1.0 / self }
486486

487487
#[inline]
488-
fn powf(&self, n: &f32) -> f32 { unsafe{intrinsics::powf32(*self, *n)} }
488+
fn powf(self, n: f32) -> f32 { unsafe{intrinsics::powf32(self, n)} }
489489

490490
#[inline]
491-
fn sqrt(&self) -> f32 { unsafe{intrinsics::sqrtf32(*self)} }
491+
fn sqrt(self) -> f32 { unsafe{intrinsics::sqrtf32(self)} }
492492

493493
#[inline]
494-
fn rsqrt(&self) -> f32 { self.sqrt().recip() }
494+
fn rsqrt(self) -> f32 { self.sqrt().recip() }
495495

496496
#[inline]
497-
fn cbrt(&self) -> f32 { unsafe{cmath::cbrtf(*self)} }
497+
fn cbrt(self) -> f32 { unsafe{cmath::cbrtf(self)} }
498498

499499
#[inline]
500-
fn hypot(&self, other: &f32) -> f32 { unsafe{cmath::hypotf(*self, *other)} }
500+
fn hypot(self, other: f32) -> f32 { unsafe{cmath::hypotf(self, other)} }
501501

502502
#[inline]
503-
fn sin(&self) -> f32 { unsafe{intrinsics::sinf32(*self)} }
503+
fn sin(self) -> f32 { unsafe{intrinsics::sinf32(self)} }
504504

505505
#[inline]
506-
fn cos(&self) -> f32 { unsafe{intrinsics::cosf32(*self)} }
506+
fn cos(self) -> f32 { unsafe{intrinsics::cosf32(self)} }
507507

508508
#[inline]
509-
fn tan(&self) -> f32 { unsafe{cmath::tanf(*self)} }
509+
fn tan(self) -> f32 { unsafe{cmath::tanf(self)} }
510510

511511
#[inline]
512-
fn asin(&self) -> f32 { unsafe{cmath::asinf(*self)} }
512+
fn asin(self) -> f32 { unsafe{cmath::asinf(self)} }
513513

514514
#[inline]
515-
fn acos(&self) -> f32 { unsafe{cmath::acosf(*self)} }
515+
fn acos(self) -> f32 { unsafe{cmath::acosf(self)} }
516516

517517
#[inline]
518-
fn atan(&self) -> f32 { unsafe{cmath::atanf(*self)} }
518+
fn atan(self) -> f32 { unsafe{cmath::atanf(self)} }
519519

520520
#[inline]
521-
fn atan2(&self, other: &f32) -> f32 { unsafe{cmath::atan2f(*self, *other)} }
521+
fn atan2(self, other: f32) -> f32 { unsafe{cmath::atan2f(self, other)} }
522522

523523
/// Simultaneously computes the sine and cosine of the number
524524
#[inline]
525-
fn sin_cos(&self) -> (f32, f32) {
525+
fn sin_cos(self) -> (f32, f32) {
526526
(self.sin(), self.cos())
527527
}
528528

529529
/// Returns the exponential of the number
530530
#[inline]
531-
fn exp(&self) -> f32 { unsafe{intrinsics::expf32(*self)} }
531+
fn exp(self) -> f32 { unsafe{intrinsics::expf32(self)} }
532532

533533
/// Returns 2 raised to the power of the number
534534
#[inline]
535-
fn exp2(&self) -> f32 { unsafe{intrinsics::exp2f32(*self)} }
535+
fn exp2(self) -> f32 { unsafe{intrinsics::exp2f32(self)} }
536536

537537
/// Returns the natural logarithm of the number
538538
#[inline]
539-
fn ln(&self) -> f32 { unsafe{intrinsics::logf32(*self)} }
539+
fn ln(self) -> f32 { unsafe{intrinsics::logf32(self)} }
540540

541541
/// Returns the logarithm of the number with respect to an arbitrary base
542542
#[inline]
543-
fn log(&self, base: &f32) -> f32 { self.ln() / base.ln() }
543+
fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
544544

545545
/// Returns the base 2 logarithm of the number
546546
#[inline]
547-
fn log2(&self) -> f32 { unsafe{intrinsics::log2f32(*self)} }
547+
fn log2(self) -> f32 { unsafe{intrinsics::log2f32(self)} }
548548

549549
/// Returns the base 10 logarithm of the number
550550
#[inline]
551-
fn log10(&self) -> f32 { unsafe{intrinsics::log10f32(*self)} }
551+
fn log10(self) -> f32 { unsafe{intrinsics::log10f32(self)} }
552552

553553
#[inline]
554-
fn sinh(&self) -> f32 { unsafe{cmath::sinhf(*self)} }
554+
fn sinh(self) -> f32 { unsafe{cmath::sinhf(self)} }
555555

556556
#[inline]
557-
fn cosh(&self) -> f32 { unsafe{cmath::coshf(*self)} }
557+
fn cosh(self) -> f32 { unsafe{cmath::coshf(self)} }
558558

559559
#[inline]
560-
fn tanh(&self) -> f32 { unsafe{cmath::tanhf(*self)} }
560+
fn tanh(self) -> f32 { unsafe{cmath::tanhf(self)} }
561561

562562
/// Inverse hyperbolic sine
563563
///
@@ -567,8 +567,8 @@ impl Float for f32 {
567567
/// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
568568
/// - `NAN` if `self` is `NAN`
569569
#[inline]
570-
fn asinh(&self) -> f32 {
571-
match *self {
570+
fn asinh(self) -> f32 {
571+
match self {
572572
NEG_INFINITY => NEG_INFINITY,
573573
x => (x + ((x * x) + 1.0).sqrt()).ln(),
574574
}
@@ -582,8 +582,8 @@ impl Float for f32 {
582582
/// - `INFINITY` if `self` is `INFINITY`
583583
/// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
584584
#[inline]
585-
fn acosh(&self) -> f32 {
586-
match *self {
585+
fn acosh(self) -> f32 {
586+
match self {
587587
x if x < 1.0 => Float::nan(),
588588
x => (x + ((x * x) - 1.0).sqrt()).ln(),
589589
}
@@ -600,19 +600,19 @@ impl Float for f32 {
600600
/// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
601601
/// (including `INFINITY` and `NEG_INFINITY`)
602602
#[inline]
603-
fn atanh(&self) -> f32 {
604-
0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
603+
fn atanh(self) -> f32 {
604+
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
605605
}
606606

607607
/// Converts to degrees, assuming the number is in radians
608608
#[inline]
609-
fn to_degrees(&self) -> f32 { *self * (180.0f32 / Float::pi()) }
609+
fn to_degrees(self) -> f32 { self * (180.0f32 / Float::pi()) }
610610

611611
/// Converts to radians, assuming the number is in degrees
612612
#[inline]
613-
fn to_radians(&self) -> f32 {
613+
fn to_radians(self) -> f32 {
614614
let value: f32 = Float::pi();
615-
*self * (value / 180.0f32)
615+
self * (value / 180.0f32)
616616
}
617617
}
618618

@@ -1162,7 +1162,7 @@ mod tests {
11621162
fn test_integer_decode() {
11631163
assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8));
11641164
assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8));
1165-
assert_eq!(2f32.powf(&100.0).integer_decode(), (8388608u64, 77i16, 1i8));
1165+
assert_eq!(2f32.powf(100.0).integer_decode(), (8388608u64, 77i16, 1i8));
11661166
assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8));
11671167
assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8));
11681168
assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8));

0 commit comments

Comments
 (0)