diff --git a/src/doc/guide-tasks.md b/src/doc/guide-tasks.md index 5dd58ccb61d9c..f9483fb4d6b91 100644 --- a/src/doc/guide-tasks.md +++ b/src/doc/guide-tasks.md @@ -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 } @@ -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() { diff --git a/src/libnum/complex.rs b/src/libnum/complex.rs index 069dd2164f511..e0fdc8a363df5 100644 --- a/src/libnum/complex.rs +++ b/src/libnum/complex.rs @@ -82,7 +82,7 @@ impl Cmplx { /// Calculate |self| #[inline] pub fn norm(&self) -> T { - self.re.hypot(&self.im) + self.re.hypot(self.im) } } @@ -90,7 +90,7 @@ impl Cmplx { /// 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)` diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs index e6b63f23741dc..cff1fb30b567a 100644 --- a/src/libnum/rational.rs +++ b/src/libnum/rational.rs @@ -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. @@ -113,6 +113,40 @@ impl pub fn recip(&self) -> Ratio { Ratio::new_raw(self.denom.clone(), self.numer.clone()) } + + pub fn floor(&self) -> Ratio { + 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 { + 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 { + 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 { + Ratio::from_integer(self.numer / self.denom) + } + + pub fn fract(&self) -> Ratio { + Ratio::new_raw(self.numer % self.denom, self.denom.clone()) + } } impl Ratio { @@ -238,45 +272,6 @@ impl impl Num for Ratio {} -/* Utils */ -impl - Round for Ratio { - - fn floor(&self) -> Ratio { - 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 { - 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 { - 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 { - Ratio::from_integer(self.numer / self.denom) - } - - fn fract(&self) -> Ratio { - Ratio::new_raw(self.numer % self.denom, self.denom.clone()) - } -} - /* String conversions */ impl fmt::Show for Ratio { /// Renders as `numer/denom`. @@ -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] diff --git a/src/librand/distributions/gamma.rs b/src/librand/distributions/gamma.rs index dd249a1fbcac8..1bb2c35bce206 100644 --- a/src/librand/distributions/gamma.rs +++ b/src/librand/distributions/gamma.rs @@ -147,7 +147,7 @@ impl IndependentSample for GammaSmallShape { fn ind_sample(&self, rng: &mut R) -> f64 { let Open01(u) = rng.gen::>(); - self.large_shape.ind_sample(rng) * u.powf(&self.inv_shape) + self.large_shape.ind_sample(rng) * u.powf(self.inv_shape) } } impl IndependentSample for GammaLargeShape { diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 7cd6aaa631086..5da03898f05d4 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -14,6 +14,7 @@ use prelude::*; +use cast; use default::Default; use from_str::FromStr; use libc::{c_int}; @@ -213,12 +214,17 @@ impl Neg for f32 { impl Signed for f32 { /// Computes the absolute value. Returns `NAN` if the number is `NAN`. #[inline] - fn abs(&self) -> f32 { unsafe{intrinsics::fabsf32(*self)} } + fn abs(&self) -> f32 { + unsafe { intrinsics::fabsf32(*self) } + } - /// The positive difference of two numbers. Returns `0.0` if the number is less than or - /// equal to `other`, otherwise the difference between`self` and `other` is returned. + /// The positive difference of two numbers. Returns `0.0` if the number is + /// less than or equal to `other`, otherwise the difference between`self` + /// and `other` is returned. #[inline] - fn abs_sub(&self, other: &f32) -> f32 { unsafe{cmath::fdimf(*self, *other)} } + fn abs_sub(&self, other: &f32) -> f32 { + unsafe { cmath::fdimf(*self, *other) } + } /// # Returns /// @@ -227,7 +233,9 @@ impl Signed for f32 { /// - `NAN` if the number is NaN #[inline] fn signum(&self) -> f32 { - if self.is_nan() { NAN } else { unsafe{intrinsics::copysignf32(1.0, *self)} } + if self.is_nan() { NAN } else { + unsafe { intrinsics::copysignf32(1.0, *self) } + } } /// Returns `true` if the number is positive, including `+0.0` and `INFINITY` @@ -239,33 +247,6 @@ impl Signed for f32 { fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY } } -impl Round for f32 { - /// Round half-way cases toward `NEG_INFINITY` - #[inline] - fn floor(&self) -> f32 { unsafe{intrinsics::floorf32(*self)} } - - /// Round half-way cases toward `INFINITY` - #[inline] - fn ceil(&self) -> f32 { unsafe{intrinsics::ceilf32(*self)} } - - /// Round half-way cases away from `0.0` - #[inline] - fn round(&self) -> f32 { unsafe{intrinsics::roundf32(*self)} } - - /// The integer part of the number (rounds towards `0.0`) - #[inline] - fn trunc(&self) -> f32 { unsafe{intrinsics::truncf32(*self)} } - - /// The fractional part of the number, satisfying: - /// - /// ```rust - /// let x = 1.65f32; - /// assert!(x == x.trunc() + x.fract()) - /// ``` - #[inline] - fn fract(&self) -> f32 { *self - self.trunc() } -} - impl Bounded for f32 { #[inline] fn min_value() -> f32 { 1.17549435e-38 } @@ -277,18 +258,6 @@ impl Bounded for f32 { impl Primitive for f32 {} impl Float for f32 { - fn powi(&self, n: i32) -> f32 { unsafe{intrinsics::powif32(*self, n)} } - - #[inline] - fn max(self, other: f32) -> f32 { - unsafe { cmath::fmaxf(self, other) } - } - - #[inline] - fn min(self, other: f32) -> f32 { - unsafe { cmath::fminf(self, other) } - } - #[inline] fn nan() -> f32 { 0.0 / 0.0 } @@ -303,33 +272,34 @@ impl Float for f32 { /// Returns `true` if the number is NaN #[inline] - fn is_nan(&self) -> bool { *self != *self } + fn is_nan(self) -> bool { self != self } /// Returns `true` if the number is infinite #[inline] - fn is_infinite(&self) -> bool { - *self == Float::infinity() || *self == Float::neg_infinity() + fn is_infinite(self) -> bool { + self == Float::infinity() || self == Float::neg_infinity() } /// Returns `true` if the number is neither infinite or NaN #[inline] - fn is_finite(&self) -> bool { + fn is_finite(self) -> bool { !(self.is_nan() || self.is_infinite()) } /// Returns `true` if the number is neither zero, infinite, subnormal or NaN #[inline] - fn is_normal(&self) -> bool { + fn is_normal(self) -> bool { self.classify() == FPNormal } - /// Returns the floating point category of the number. If only one property is going to - /// be tested, it is generally faster to use the specific predicate instead. - fn classify(&self) -> FPCategory { + /// Returns the floating point category of the number. If only one property + /// is going to be tested, it is generally faster to use the specific + /// predicate instead. + fn classify(self) -> FPCategory { static EXP_MASK: u32 = 0x7f800000; static MAN_MASK: u32 = 0x007fffff; - let bits: u32 = unsafe {::cast::transmute(*self)}; + let bits: u32 = unsafe { cast::transmute(self) }; match (bits & MAN_MASK, bits & EXP_MASK) { (0, 0) => FPZero, (_, 0) => FPSubnormal, @@ -360,48 +330,30 @@ impl Float for f32 { #[inline] fn max_10_exp(_: Option) -> int { 38 } - /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp` + /// Constructs a floating point number by multiplying `x` by 2 raised to the + /// power of `exp` #[inline] - fn ldexp(x: f32, exp: int) -> f32 { unsafe{cmath::ldexpf(x, exp as c_int)} } + fn ldexp(x: f32, exp: int) -> f32 { + unsafe { cmath::ldexpf(x, exp as c_int) } + } - /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying: + /// Breaks the number into a normalized fraction and a base-2 exponent, + /// satisfying: /// /// - `self = x * pow(2, exp)` /// - `0.5 <= abs(x) < 1.0` #[inline] - fn frexp(&self) -> (f32, int) { + fn frexp(self) -> (f32, int) { unsafe { let mut exp = 0; - let x = cmath::frexpf(*self, &mut exp); + let x = cmath::frexpf(self, &mut exp); (x, exp as int) } } - /// Returns the exponential of the number, minus `1`, in a way that is accurate - /// even if the number is close to zero - #[inline] - fn exp_m1(&self) -> f32 { unsafe{cmath::expm1f(*self)} } - - /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately - /// than if the operations were performed separately - #[inline] - fn ln_1p(&self) -> f32 { unsafe{cmath::log1pf(*self)} } - - /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This - /// produces a more accurate result with better performance than a separate multiplication - /// operation followed by an add. - #[inline] - fn mul_add(&self, a: f32, b: f32) -> f32 { unsafe{intrinsics::fmaf32(*self, a, b)} } - - /// Returns the next representable floating-point value in the direction of `other` - #[inline] - fn next_after(&self, other: f32) -> f32 { unsafe{cmath::nextafterf(*self, other)} } - /// Returns the mantissa, exponent and sign as integers. - fn integer_decode(&self) -> (u64, i16, i8) { - let bits: u32 = unsafe { - ::cast::transmute(*self) - }; + fn integer_decode(self) -> (u64, i16, i8) { + let bits: u32 = unsafe { cast::transmute(self) }; let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; let mantissa = if exponent == 0 { @@ -414,45 +366,76 @@ impl Float for f32 { (mantissa as u64, exponent, sign) } - /// Archimedes' constant + /// Returns the next representable floating-point value in the direction of + /// `other`. #[inline] - fn pi() -> f32 { 3.14159265358979323846264338327950288 } + fn next_after(self, other: f32) -> f32 { + unsafe { cmath::nextafterf(self, other) } + } - /// 2.0 * pi + /// Round half-way cases toward `NEG_INFINITY` #[inline] - fn two_pi() -> f32 { 6.28318530717958647692528676655900576 } + fn floor(self) -> f32 { + unsafe { intrinsics::floorf32(self) } + } - /// pi / 2.0 + /// Round half-way cases toward `INFINITY` #[inline] - fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 } + fn ceil(self) -> f32 { + unsafe { intrinsics::ceilf32(self) } + } - /// pi / 3.0 + /// Round half-way cases away from `0.0` #[inline] - fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 } + fn round(self) -> f32 { + unsafe { intrinsics::roundf32(self) } + } - /// pi / 4.0 + /// The integer part of the number (rounds towards `0.0`) #[inline] - fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 } + fn trunc(self) -> f32 { + unsafe { intrinsics::truncf32(self) } + } - /// pi / 6.0 + /// The fractional part of the number, satisfying: + /// + /// ```rust + /// let x = 1.65f32; + /// assert!(x == x.trunc() + x.fract()) + /// ``` #[inline] - fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 } + fn fract(self) -> f32 { self - self.trunc() } - /// pi / 8.0 #[inline] - fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 } + fn max(self, other: f32) -> f32 { + unsafe { cmath::fmaxf(self, other) } + } - /// 1 .0/ pi #[inline] - fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 } + fn min(self, other: f32) -> f32 { + unsafe { cmath::fminf(self, other) } + } - /// 2.0 / pi + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding + /// error. This produces a more accurate result with better performance than + /// a separate multiplication operation followed by an add. #[inline] - fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 } + fn mul_add(self, a: f32, b: f32) -> f32 { + unsafe { intrinsics::fmaf32(self, a, b) } + } - /// 2.0 / sqrt(pi) + /// The reciprocal (multiplicative inverse) of the number #[inline] - fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 } + fn recip(self) -> f32 { 1.0 / self } + + fn powi(self, n: i32) -> f32 { + unsafe { intrinsics::powif32(self, n) } + } + + #[inline] + fn powf(self, n: f32) -> f32 { + unsafe { intrinsics::powf32(self, n) } + } /// sqrt(2.0) #[inline] @@ -462,104 +445,187 @@ impl Float for f32 { #[inline] fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 } - /// Euler's number #[inline] - fn e() -> f32 { 2.71828182845904523536028747135266250 } + fn sqrt(self) -> f32 { + unsafe { intrinsics::sqrtf32(self) } + } - /// log2(e) #[inline] - fn log2_e() -> f32 { 1.44269504088896340735992468100189214 } + fn rsqrt(self) -> f32 { self.sqrt().recip() } - /// log10(e) #[inline] - fn log10_e() -> f32 { 0.434294481903251827651128918916605082 } + fn cbrt(self) -> f32 { + unsafe { cmath::cbrtf(self) } + } - /// ln(2.0) #[inline] - fn ln_2() -> f32 { 0.693147180559945309417232121458176568 } + fn hypot(self, other: f32) -> f32 { + unsafe { cmath::hypotf(self, other) } + } - /// ln(10.0) + /// Archimedes' constant #[inline] - fn ln_10() -> f32 { 2.30258509299404568401799145468436421 } + fn pi() -> f32 { 3.14159265358979323846264338327950288 } - /// The reciprocal (multiplicative inverse) of the number + /// 2.0 * pi #[inline] - fn recip(&self) -> f32 { 1.0 / *self } + fn two_pi() -> f32 { 6.28318530717958647692528676655900576 } + /// pi / 2.0 #[inline] - fn powf(&self, n: &f32) -> f32 { unsafe{intrinsics::powf32(*self, *n)} } + fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 } + /// pi / 3.0 #[inline] - fn sqrt(&self) -> f32 { unsafe{intrinsics::sqrtf32(*self)} } + fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 } + /// pi / 4.0 #[inline] - fn rsqrt(&self) -> f32 { self.sqrt().recip() } + fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 } + /// pi / 6.0 #[inline] - fn cbrt(&self) -> f32 { unsafe{cmath::cbrtf(*self)} } + fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 } + /// pi / 8.0 #[inline] - fn hypot(&self, other: &f32) -> f32 { unsafe{cmath::hypotf(*self, *other)} } + fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 } + /// 1 .0/ pi #[inline] - fn sin(&self) -> f32 { unsafe{intrinsics::sinf32(*self)} } + fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 } + /// 2.0 / pi #[inline] - fn cos(&self) -> f32 { unsafe{intrinsics::cosf32(*self)} } + fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 } + /// 2.0 / sqrt(pi) #[inline] - fn tan(&self) -> f32 { unsafe{cmath::tanf(*self)} } + fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 } #[inline] - fn asin(&self) -> f32 { unsafe{cmath::asinf(*self)} } + fn sin(self) -> f32 { + unsafe { intrinsics::sinf32(self) } + } #[inline] - fn acos(&self) -> f32 { unsafe{cmath::acosf(*self)} } + fn cos(self) -> f32 { + unsafe { intrinsics::cosf32(self) } + } #[inline] - fn atan(&self) -> f32 { unsafe{cmath::atanf(*self)} } + fn tan(self) -> f32 { + unsafe { cmath::tanf(self) } + } #[inline] - fn atan2(&self, other: &f32) -> f32 { unsafe{cmath::atan2f(*self, *other)} } + fn asin(self) -> f32 { + unsafe { cmath::asinf(self) } + } + + #[inline] + fn acos(self) -> f32 { + unsafe { cmath::acosf(self) } + } + + #[inline] + fn atan(self) -> f32 { + unsafe { cmath::atanf(self) } + } + + #[inline] + fn atan2(self, other: f32) -> f32 { + unsafe { cmath::atan2f(self, other) } + } /// Simultaneously computes the sine and cosine of the number #[inline] - fn sin_cos(&self) -> (f32, f32) { + fn sin_cos(self) -> (f32, f32) { (self.sin(), self.cos()) } + /// Euler's number + #[inline] + fn e() -> f32 { 2.71828182845904523536028747135266250 } + + /// log2(e) + #[inline] + fn log2_e() -> f32 { 1.44269504088896340735992468100189214 } + + /// log10(e) + #[inline] + fn log10_e() -> f32 { 0.434294481903251827651128918916605082 } + + /// ln(2.0) + #[inline] + fn ln_2() -> f32 { 0.693147180559945309417232121458176568 } + + /// ln(10.0) + #[inline] + fn ln_10() -> f32 { 2.30258509299404568401799145468436421 } + /// Returns the exponential of the number #[inline] - fn exp(&self) -> f32 { unsafe{intrinsics::expf32(*self)} } + fn exp(self) -> f32 { + unsafe { intrinsics::expf32(self) } + } /// Returns 2 raised to the power of the number #[inline] - fn exp2(&self) -> f32 { unsafe{intrinsics::exp2f32(*self)} } + fn exp2(self) -> f32 { + unsafe { intrinsics::exp2f32(self) } + } + + /// Returns the exponential of the number, minus `1`, in a way that is + /// accurate even if the number is close to zero + #[inline] + fn exp_m1(self) -> f32 { + unsafe { cmath::expm1f(self) } + } /// Returns the natural logarithm of the number #[inline] - fn ln(&self) -> f32 { unsafe{intrinsics::logf32(*self)} } + fn ln(self) -> f32 { + unsafe { intrinsics::logf32(self) } + } /// Returns the logarithm of the number with respect to an arbitrary base #[inline] - fn log(&self, base: &f32) -> f32 { self.ln() / base.ln() } + fn log(self, base: f32) -> f32 { self.ln() / base.ln() } /// Returns the base 2 logarithm of the number #[inline] - fn log2(&self) -> f32 { unsafe{intrinsics::log2f32(*self)} } + fn log2(self) -> f32 { + unsafe { intrinsics::log2f32(self) } + } /// Returns the base 10 logarithm of the number #[inline] - fn log10(&self) -> f32 { unsafe{intrinsics::log10f32(*self)} } + fn log10(self) -> f32 { + unsafe { intrinsics::log10f32(self) } + } + + /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more + /// accurately than if the operations were performed separately + #[inline] + fn ln_1p(self) -> f32 { + unsafe { cmath::log1pf(self) } + } #[inline] - fn sinh(&self) -> f32 { unsafe{cmath::sinhf(*self)} } + fn sinh(self) -> f32 { + unsafe { cmath::sinhf(self) } + } #[inline] - fn cosh(&self) -> f32 { unsafe{cmath::coshf(*self)} } + fn cosh(self) -> f32 { + unsafe { cmath::coshf(self) } + } #[inline] - fn tanh(&self) -> f32 { unsafe{cmath::tanhf(*self)} } + fn tanh(self) -> f32 { + unsafe { cmath::tanhf(self) } + } /// Inverse hyperbolic sine /// @@ -569,8 +635,8 @@ impl Float for f32 { /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY` /// - `NAN` if `self` is `NAN` #[inline] - fn asinh(&self) -> f32 { - match *self { + fn asinh(self) -> f32 { + match self { NEG_INFINITY => NEG_INFINITY, x => (x + ((x * x) + 1.0).sqrt()).ln(), } @@ -584,8 +650,8 @@ impl Float for f32 { /// - `INFINITY` if `self` is `INFINITY` /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`) #[inline] - fn acosh(&self) -> f32 { - match *self { + fn acosh(self) -> f32 { + match self { x if x < 1.0 => Float::nan(), x => (x + ((x * x) - 1.0).sqrt()).ln(), } @@ -602,19 +668,19 @@ impl Float for f32 { /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0` /// (including `INFINITY` and `NEG_INFINITY`) #[inline] - fn atanh(&self) -> f32 { - 0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p() + fn atanh(self) -> f32 { + 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() } /// Converts to degrees, assuming the number is in radians #[inline] - fn to_degrees(&self) -> f32 { *self * (180.0f32 / Float::pi()) } + fn to_degrees(self) -> f32 { self * (180.0f32 / Float::pi()) } /// Converts to radians, assuming the number is in degrees #[inline] - fn to_radians(&self) -> f32 { + fn to_radians(self) -> f32 { let value: f32 = Float::pi(); - *self * (value / 180.0f32) + self * (value / 180.0f32) } } @@ -1164,7 +1230,7 @@ mod tests { fn test_integer_decode() { assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8)); assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8)); - assert_eq!(2f32.powf(&100.0).integer_decode(), (8388608u64, 77i16, 1i8)); + assert_eq!(2f32.powf(100.0).integer_decode(), (8388608u64, 77i16, 1i8)); assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8)); assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8)); assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8)); diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 8b52a6747f478..a2b63968569b4 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -14,6 +14,7 @@ use prelude::*; +use cast; use default::Default; use from_str::FromStr; use libc::{c_int}; @@ -221,12 +222,16 @@ impl Neg for f64 { impl Signed for f64 { /// Computes the absolute value. Returns `NAN` if the number is `NAN`. #[inline] - fn abs(&self) -> f64 { unsafe{intrinsics::fabsf64(*self)} } + fn abs(&self) -> f64 { + unsafe { intrinsics::fabsf64(*self) } + } /// The positive difference of two numbers. Returns `0.0` if the number is less than or /// equal to `other`, otherwise the difference between`self` and `other` is returned. #[inline] - fn abs_sub(&self, other: &f64) -> f64 { unsafe{cmath::fdim(*self, *other)} } + fn abs_sub(&self, other: &f64) -> f64 { + unsafe { cmath::fdim(*self, *other) } + } /// # Returns /// @@ -235,7 +240,9 @@ impl Signed for f64 { /// - `NAN` if the number is NaN #[inline] fn signum(&self) -> f64 { - if self.is_nan() { NAN } else { unsafe{intrinsics::copysignf64(1.0, *self)} } + if self.is_nan() { NAN } else { + unsafe { intrinsics::copysignf64(1.0, *self) } + } } /// Returns `true` if the number is positive, including `+0.0` and `INFINITY` @@ -247,33 +254,6 @@ impl Signed for f64 { fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY } } -impl Round for f64 { - /// Round half-way cases toward `NEG_INFINITY` - #[inline] - fn floor(&self) -> f64 { unsafe{intrinsics::floorf64(*self)} } - - /// Round half-way cases toward `INFINITY` - #[inline] - fn ceil(&self) -> f64 { unsafe{intrinsics::ceilf64(*self)} } - - /// Round half-way cases away from `0.0` - #[inline] - fn round(&self) -> f64 { unsafe{intrinsics::roundf64(*self)} } - - /// The integer part of the number (rounds towards `0.0`) - #[inline] - fn trunc(&self) -> f64 { unsafe{intrinsics::truncf64(*self)} } - - /// The fractional part of the number, satisfying: - /// - /// ```rust - /// let x = 1.65f64; - /// assert!(x == x.trunc() + x.fract()) - /// ``` - #[inline] - fn fract(&self) -> f64 { *self - self.trunc() } -} - impl Bounded for f64 { #[inline] fn min_value() -> f64 { 2.2250738585072014e-308 } @@ -285,16 +265,6 @@ impl Bounded for f64 { impl Primitive for f64 {} impl Float for f64 { - #[inline] - fn max(self, other: f64) -> f64 { - unsafe { cmath::fmax(self, other) } - } - - #[inline] - fn min(self, other: f64) -> f64 { - unsafe { cmath::fmin(self, other) } - } - #[inline] fn nan() -> f64 { 0.0 / 0.0 } @@ -309,33 +279,34 @@ impl Float for f64 { /// Returns `true` if the number is NaN #[inline] - fn is_nan(&self) -> bool { *self != *self } + fn is_nan(self) -> bool { self != self } /// Returns `true` if the number is infinite #[inline] - fn is_infinite(&self) -> bool { - *self == Float::infinity() || *self == Float::neg_infinity() + fn is_infinite(self) -> bool { + self == Float::infinity() || self == Float::neg_infinity() } /// Returns `true` if the number is neither infinite or NaN #[inline] - fn is_finite(&self) -> bool { + fn is_finite(self) -> bool { !(self.is_nan() || self.is_infinite()) } /// Returns `true` if the number is neither zero, infinite, subnormal or NaN #[inline] - fn is_normal(&self) -> bool { + fn is_normal(self) -> bool { self.classify() == FPNormal } - /// Returns the floating point category of the number. If only one property is going to - /// be tested, it is generally faster to use the specific predicate instead. - fn classify(&self) -> FPCategory { + /// Returns the floating point category of the number. If only one property + /// is going to be tested, it is generally faster to use the specific + /// predicate instead. + fn classify(self) -> FPCategory { static EXP_MASK: u64 = 0x7ff0000000000000; static MAN_MASK: u64 = 0x000fffffffffffff; - let bits: u64 = unsafe {::cast::transmute(*self)}; + let bits: u64 = unsafe { cast::transmute(self) }; match (bits & MAN_MASK, bits & EXP_MASK) { (0, 0) => FPZero, (_, 0) => FPSubnormal, @@ -366,48 +337,30 @@ impl Float for f64 { #[inline] fn max_10_exp(_: Option) -> int { 308 } - /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp` + /// Constructs a floating point number by multiplying `x` by 2 raised to the + /// power of `exp` #[inline] - fn ldexp(x: f64, exp: int) -> f64 { unsafe{cmath::ldexp(x, exp as c_int)} } + fn ldexp(x: f64, exp: int) -> f64 { + unsafe { cmath::ldexp(x, exp as c_int) } + } - /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying: + /// Breaks the number into a normalized fraction and a base-2 exponent, + /// satisfying: /// /// - `self = x * pow(2, exp)` /// - `0.5 <= abs(x) < 1.0` #[inline] - fn frexp(&self) -> (f64, int) { + fn frexp(self) -> (f64, int) { unsafe { let mut exp = 0; - let x = cmath::frexp(*self, &mut exp); + let x = cmath::frexp(self, &mut exp); (x, exp as int) } } - /// Returns the exponential of the number, minus `1`, in a way that is accurate - /// even if the number is close to zero - #[inline] - fn exp_m1(&self) -> f64 { unsafe{cmath::expm1(*self)} } - - /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately - /// than if the operations were performed separately - #[inline] - fn ln_1p(&self) -> f64 { unsafe{cmath::log1p(*self)} } - - /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This - /// produces a more accurate result with better performance than a separate multiplication - /// operation followed by an add. - #[inline] - fn mul_add(&self, a: f64, b: f64) -> f64 { unsafe{intrinsics::fmaf64(*self, a, b)} } - - /// Returns the next representable floating-point value in the direction of `other` - #[inline] - fn next_after(&self, other: f64) -> f64 { unsafe{cmath::nextafter(*self, other)} } - /// Returns the mantissa, exponent and sign as integers. - fn integer_decode(&self) -> (u64, i16, i8) { - let bits: u64 = unsafe { - ::cast::transmute(*self) - }; + fn integer_decode(self) -> (u64, i16, i8) { + let bits: u64 = unsafe { cast::transmute(self) }; let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16; let mantissa = if exponent == 0 { @@ -420,45 +373,77 @@ impl Float for f64 { (mantissa, exponent, sign) } - /// Archimedes' constant + /// Returns the next representable floating-point value in the direction of + /// `other`. #[inline] - fn pi() -> f64 { 3.14159265358979323846264338327950288 } + fn next_after(self, other: f64) -> f64 { + unsafe { cmath::nextafter(self, other) } + } - /// 2.0 * pi + /// Round half-way cases toward `NEG_INFINITY` #[inline] - fn two_pi() -> f64 { 6.28318530717958647692528676655900576 } + fn floor(self) -> f64 { + unsafe { intrinsics::floorf64(self) } + } - /// pi / 2.0 + /// Round half-way cases toward `INFINITY` #[inline] - fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 } + fn ceil(self) -> f64 { + unsafe { intrinsics::ceilf64(self) } + } - /// pi / 3.0 + /// Round half-way cases away from `0.0` #[inline] - fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 } + fn round(self) -> f64 { + unsafe { intrinsics::roundf64(self) } + } - /// pi / 4.0 + /// The integer part of the number (rounds towards `0.0`) #[inline] - fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 } + fn trunc(self) -> f64 { + unsafe { intrinsics::truncf64(self) } + } - /// pi / 6.0 + /// The fractional part of the number, satisfying: + /// + /// ```rust + /// let x = 1.65f64; + /// assert!(x == x.trunc() + x.fract()) + /// ``` #[inline] - fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 } + fn fract(self) -> f64 { self - self.trunc() } - /// pi / 8.0 #[inline] - fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 } + fn max(self, other: f64) -> f64 { + unsafe { cmath::fmax(self, other) } + } - /// 1.0 / pi #[inline] - fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 } + fn min(self, other: f64) -> f64 { + unsafe { cmath::fmin(self, other) } + } - /// 2.0 / pi + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding + /// error. This produces a more accurate result with better performance than + /// a separate multiplication operation followed by an add. #[inline] - fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 } + fn mul_add(self, a: f64, b: f64) -> f64 { + unsafe { intrinsics::fmaf64(self, a, b) } + } - /// 2.0 / sqrt(pi) + /// The reciprocal (multiplicative inverse) of the number #[inline] - fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 } + fn recip(self) -> f64 { 1.0 / self } + + #[inline] + fn powf(self, n: f64) -> f64 { + unsafe { intrinsics::powf64(self, n) } + } + + #[inline] + fn powi(self, n: i32) -> f64 { + unsafe { intrinsics::powif64(self, n) } + } /// sqrt(2.0) #[inline] @@ -468,107 +453,187 @@ impl Float for f64 { #[inline] fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 } - /// Euler's number #[inline] - fn e() -> f64 { 2.71828182845904523536028747135266250 } + fn sqrt(self) -> f64 { + unsafe { intrinsics::sqrtf64(self) } + } - /// log2(e) #[inline] - fn log2_e() -> f64 { 1.44269504088896340735992468100189214 } + fn rsqrt(self) -> f64 { self.sqrt().recip() } - /// log10(e) #[inline] - fn log10_e() -> f64 { 0.434294481903251827651128918916605082 } + fn cbrt(self) -> f64 { + unsafe { cmath::cbrt(self) } + } - /// ln(2.0) #[inline] - fn ln_2() -> f64 { 0.693147180559945309417232121458176568 } + fn hypot(self, other: f64) -> f64 { + unsafe { cmath::hypot(self, other) } + } - /// ln(10.0) + /// Archimedes' constant #[inline] - fn ln_10() -> f64 { 2.30258509299404568401799145468436421 } + fn pi() -> f64 { 3.14159265358979323846264338327950288 } - /// The reciprocal (multiplicative inverse) of the number + /// 2.0 * pi #[inline] - fn recip(&self) -> f64 { 1.0 / *self } + fn two_pi() -> f64 { 6.28318530717958647692528676655900576 } + /// pi / 2.0 #[inline] - fn powf(&self, n: &f64) -> f64 { unsafe{intrinsics::powf64(*self, *n)} } + fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 } + /// pi / 3.0 #[inline] - fn powi(&self, n: i32) -> f64 { unsafe{intrinsics::powif64(*self, n)} } + fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 } + /// pi / 4.0 #[inline] - fn sqrt(&self) -> f64 { unsafe{intrinsics::sqrtf64(*self)} } + fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 } + /// pi / 6.0 #[inline] - fn rsqrt(&self) -> f64 { self.sqrt().recip() } + fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 } + /// pi / 8.0 + #[inline] + fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 } + + /// 1.0 / pi #[inline] - fn cbrt(&self) -> f64 { unsafe{cmath::cbrt(*self)} } + fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 } + + /// 2.0 / pi + #[inline] + fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 } + /// 2.0 / sqrt(pi) #[inline] - fn hypot(&self, other: &f64) -> f64 { unsafe{cmath::hypot(*self, *other)} } + fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 } #[inline] - fn sin(&self) -> f64 { unsafe{intrinsics::sinf64(*self)} } + fn sin(self) -> f64 { + unsafe { intrinsics::sinf64(self) } + } #[inline] - fn cos(&self) -> f64 { unsafe{intrinsics::cosf64(*self)} } + fn cos(self) -> f64 { + unsafe { intrinsics::cosf64(self) } + } #[inline] - fn tan(&self) -> f64 { unsafe{cmath::tan(*self)} } + fn tan(self) -> f64 { + unsafe { cmath::tan(self) } + } #[inline] - fn asin(&self) -> f64 { unsafe{cmath::asin(*self)} } + fn asin(self) -> f64 { + unsafe { cmath::asin(self) } + } #[inline] - fn acos(&self) -> f64 { unsafe{cmath::acos(*self)} } + fn acos(self) -> f64 { + unsafe { cmath::acos(self) } + } #[inline] - fn atan(&self) -> f64 { unsafe{cmath::atan(*self)} } + fn atan(self) -> f64 { + unsafe { cmath::atan(self) } + } #[inline] - fn atan2(&self, other: &f64) -> f64 { unsafe{cmath::atan2(*self, *other)} } + fn atan2(self, other: f64) -> f64 { + unsafe { cmath::atan2(self, other) } + } /// Simultaneously computes the sine and cosine of the number #[inline] - fn sin_cos(&self) -> (f64, f64) { + fn sin_cos(self) -> (f64, f64) { (self.sin(), self.cos()) } + /// Euler's number + #[inline] + fn e() -> f64 { 2.71828182845904523536028747135266250 } + + /// log2(e) + #[inline] + fn log2_e() -> f64 { 1.44269504088896340735992468100189214 } + + /// log10(e) + #[inline] + fn log10_e() -> f64 { 0.434294481903251827651128918916605082 } + + /// ln(2.0) + #[inline] + fn ln_2() -> f64 { 0.693147180559945309417232121458176568 } + + /// ln(10.0) + #[inline] + fn ln_10() -> f64 { 2.30258509299404568401799145468436421 } + /// Returns the exponential of the number #[inline] - fn exp(&self) -> f64 { unsafe{intrinsics::expf64(*self)} } + fn exp(self) -> f64 { + unsafe { intrinsics::expf64(self) } + } /// Returns 2 raised to the power of the number #[inline] - fn exp2(&self) -> f64 { unsafe{intrinsics::exp2f64(*self)} } + fn exp2(self) -> f64 { + unsafe { intrinsics::exp2f64(self) } + } + + /// Returns the exponential of the number, minus `1`, in a way that is + /// accurate even if the number is close to zero + #[inline] + fn exp_m1(self) -> f64 { + unsafe { cmath::expm1(self) } + } /// Returns the natural logarithm of the number #[inline] - fn ln(&self) -> f64 { unsafe{intrinsics::logf64(*self)} } + fn ln(self) -> f64 { + unsafe { intrinsics::logf64(self) } + } /// Returns the logarithm of the number with respect to an arbitrary base #[inline] - fn log(&self, base: &f64) -> f64 { self.ln() / base.ln() } + fn log(self, base: f64) -> f64 { self.ln() / base.ln() } /// Returns the base 2 logarithm of the number #[inline] - fn log2(&self) -> f64 { unsafe{intrinsics::log2f64(*self)} } + fn log2(self) -> f64 { + unsafe { intrinsics::log2f64(self) } + } /// Returns the base 10 logarithm of the number #[inline] - fn log10(&self) -> f64 { unsafe{intrinsics::log10f64(*self)} } + fn log10(self) -> f64 { + unsafe { intrinsics::log10f64(self) } + } + + /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more + /// accurately than if the operations were performed separately + #[inline] + fn ln_1p(self) -> f64 { + unsafe { cmath::log1p(self) } + } #[inline] - fn sinh(&self) -> f64 { unsafe{cmath::sinh(*self)} } + fn sinh(self) -> f64 { + unsafe { cmath::sinh(self) } + } #[inline] - fn cosh(&self) -> f64 { unsafe{cmath::cosh(*self)} } + fn cosh(self) -> f64 { + unsafe { cmath::cosh(self) } + } #[inline] - fn tanh(&self) -> f64 { unsafe{cmath::tanh(*self)} } + fn tanh(self) -> f64 { + unsafe { cmath::tanh(self) } + } /// Inverse hyperbolic sine /// @@ -578,8 +643,8 @@ impl Float for f64 { /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY` /// - `NAN` if `self` is `NAN` #[inline] - fn asinh(&self) -> f64 { - match *self { + fn asinh(self) -> f64 { + match self { NEG_INFINITY => NEG_INFINITY, x => (x + ((x * x) + 1.0).sqrt()).ln(), } @@ -593,8 +658,8 @@ impl Float for f64 { /// - `INFINITY` if `self` is `INFINITY` /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`) #[inline] - fn acosh(&self) -> f64 { - match *self { + fn acosh(self) -> f64 { + match self { x if x < 1.0 => Float::nan(), x => (x + ((x * x) - 1.0).sqrt()).ln(), } @@ -611,19 +676,19 @@ impl Float for f64 { /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0` /// (including `INFINITY` and `NEG_INFINITY`) #[inline] - fn atanh(&self) -> f64 { - 0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p() + fn atanh(self) -> f64 { + 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() } /// Converts to degrees, assuming the number is in radians #[inline] - fn to_degrees(&self) -> f64 { *self * (180.0f64 / Float::pi()) } + fn to_degrees(self) -> f64 { self * (180.0f64 / Float::pi()) } /// Converts to radians, assuming the number is in degrees #[inline] - fn to_radians(&self) -> f64 { + fn to_radians(self) -> f64 { let value: f64 = Float::pi(); - *self * (value / 180.0) + self * (value / 180.0) } } @@ -1167,7 +1232,7 @@ mod tests { fn test_integer_decode() { assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906u64, -51i16, 1i8)); assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931u64, -39i16, -1i8)); - assert_eq!(2f64.powf(&100.0).integer_decode(), (4503599627370496u64, 48i16, 1i8)); + assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496u64, 48i16, 1i8)); assert_eq!(0f64.integer_decode(), (0u64, -1075i16, 1i8)); assert_eq!((-0f64).integer_decode(), (0u64, -1075i16, -1i8)); assert_eq!(INFINITY.integer_decode(), (4503599627370496u64, 972i16, 1i8)); diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 12befed743a52..75cf02034b992 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -162,25 +162,6 @@ pub fn abs_sub(x: T, y: T) -> T { /// A trait for values which cannot be negative pub trait Unsigned: Num {} -/// A collection of rounding operations. -pub trait Round { - /// Return the largest integer less than or equal to a number. - fn floor(&self) -> Self; - - /// Return the smallest integer greater than or equal to a number. - fn ceil(&self) -> Self; - - /// Return the nearest integer to a number. Round half-way cases away from - /// `0.0`. - fn round(&self) -> Self; - - /// Return the integer part of a number. - fn trunc(&self) -> Self; - - /// Return the fractional part of a number. - fn fract(&self) -> Self; -} - /// Raises a value to the power of exp, using exponentiation by squaring. /// /// # Example @@ -347,217 +328,199 @@ pub enum FPCategory { // // FIXME(#8888): Several of these functions have a parameter named // `unused_self`. Removing it requires #8888 to be fixed. -pub trait Float: Signed + Round + Primitive { - /// Returns the maximum of the two numbers. - fn max(self, other: Self) -> Self; - /// Returns the minimum of the two numbers. - fn min(self, other: Self) -> Self; - +pub trait Float: Signed + Primitive { /// Returns the NaN value. fn nan() -> Self; - /// Returns the infinite value. fn infinity() -> Self; - /// Returns the negative infinite value. fn neg_infinity() -> Self; - /// Returns -0.0. fn neg_zero() -> Self; /// Returns true if this value is NaN and false otherwise. - fn is_nan(&self) -> bool; - - /// Returns true if this value is positive infinity or negative infinity and false otherwise. - fn is_infinite(&self) -> bool; - + fn is_nan(self) -> bool; + /// Returns true if this value is positive infinity or negative infinity and + /// false otherwise. + fn is_infinite(self) -> bool; /// Returns true if this number is neither infinite nor NaN. - fn is_finite(&self) -> bool; - + fn is_finite(self) -> bool; /// Returns true if this number is neither zero, infinite, denormal, or NaN. - fn is_normal(&self) -> bool; - + fn is_normal(self) -> bool; /// Returns the category that this number falls into. - fn classify(&self) -> FPCategory; + fn classify(self) -> FPCategory; /// Returns the number of binary digits of mantissa that this type supports. fn mantissa_digits(unused_self: Option) -> uint; - /// Returns the number of binary digits of exponent that this type supports. fn digits(unused_self: Option) -> uint; - /// Returns the smallest positive number that this type can represent. fn epsilon() -> Self; - /// Returns the minimum binary exponent that this type can represent. fn min_exp(unused_self: Option) -> int; - /// Returns the maximum binary exponent that this type can represent. fn max_exp(unused_self: Option) -> int; - /// Returns the minimum base-10 exponent that this type can represent. fn min_10_exp(unused_self: Option) -> int; - /// Returns the maximum base-10 exponent that this type can represent. fn max_10_exp(unused_self: Option) -> int; - /// Constructs a floating point number created by multiplying `x` by 2 raised to the power of - /// `exp`. + /// Constructs a floating point number created by multiplying `x` by 2 + /// raised to the power of `exp`. fn ldexp(x: Self, exp: int) -> Self; - - /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying: + /// Breaks the number into a normalized fraction and a base-2 exponent, + /// satisfying: /// /// * `self = x * pow(2, exp)` /// /// * `0.5 <= abs(x) < 1.0` - fn frexp(&self) -> (Self, int); + fn frexp(self) -> (Self, int); + /// Returns the mantissa, exponent and sign as integers, respectively. + fn integer_decode(self) -> (u64, i16, i8); + + /// Returns the next representable floating-point value in the direction of + /// `other`. + fn next_after(self, other: Self) -> Self; + + /// Return the largest integer less than or equal to a number. + fn floor(self) -> Self; + /// Return the smallest integer greater than or equal to a number. + fn ceil(self) -> Self; + /// Return the nearest integer to a number. Round half-way cases away from + /// `0.0`. + fn round(self) -> Self; + /// Return the integer part of a number. + fn trunc(self) -> Self; + /// Return the fractional part of a number. + fn fract(self) -> Self; - /// Returns the exponential of the number, minus 1, in a way that is accurate even if the - /// number is close to zero. - fn exp_m1(&self) -> Self; + /// Returns the maximum of the two numbers. + fn max(self, other: Self) -> Self; + /// Returns the minimum of the two numbers. + fn min(self, other: Self) -> Self; - /// Returns the natural logarithm of the number plus 1 (`ln(1+n)`) more accurately than if the - /// operations were performed separately. - fn ln_1p(&self) -> Self; + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding + /// error. This produces a more accurate result with better performance than + /// a separate multiplication operation followed by an add. + fn mul_add(self, a: Self, b: Self) -> Self; + /// Take the reciprocal (inverse) of a number, `1/x`. + fn recip(self) -> Self; - /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This produces a - /// more accurate result with better performance than a separate multiplication operation - /// followed by an add. - fn mul_add(&self, a: Self, b: Self) -> Self; + /// Raise a number to an integer power. + /// + /// Using this function is generally faster than using `powf` + fn powi(self, n: i32) -> Self; + /// Raise a number to a floating point power. + fn powf(self, n: Self) -> Self; - /// Returns the next representable floating-point value in the direction of `other`. - fn next_after(&self, other: Self) -> Self; + /// sqrt(2.0). + fn sqrt2() -> Self; + /// 1.0 / sqrt(2.0). + fn frac_1_sqrt2() -> Self; - /// Returns the mantissa, exponent and sign as integers, respectively. - fn integer_decode(&self) -> (u64, i16, i8); + /// Take the square root of a number. + fn sqrt(self) -> Self; + /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`. + fn rsqrt(self) -> Self; + /// Take the cubic root of a number. + fn cbrt(self) -> Self; + /// Calculate the length of the hypotenuse of a right-angle triangle given + /// legs of length `x` and `y`. + fn hypot(self, other: Self) -> Self; /// Archimedes' constant. fn pi() -> Self; - /// 2.0 * pi. fn two_pi() -> Self; - /// pi / 2.0. fn frac_pi_2() -> Self; - /// pi / 3.0. fn frac_pi_3() -> Self; - /// pi / 4.0. fn frac_pi_4() -> Self; - /// pi / 6.0. fn frac_pi_6() -> Self; - /// pi / 8.0. fn frac_pi_8() -> Self; - /// 1.0 / pi. fn frac_1_pi() -> Self; - /// 2.0 / pi. fn frac_2_pi() -> Self; - /// 2.0 / sqrt(pi). fn frac_2_sqrtpi() -> Self; - /// sqrt(2.0). - fn sqrt2() -> Self; - - /// 1.0 / sqrt(2.0). - fn frac_1_sqrt2() -> Self; - - /// Euler's number. - fn e() -> Self; - - /// log2(e). - fn log2_e() -> Self; - - /// log10(e). - fn log10_e() -> Self; - - /// ln(2.0). - fn ln_2() -> Self; - - /// ln(10.0). - fn ln_10() -> Self; - - /// Take the reciprocal (inverse) of a number, `1/x`. - fn recip(&self) -> Self; - - /// Raise a number to a power. - fn powf(&self, n: &Self) -> Self; - - /// Raise a number to an integer power. - /// - /// Using this function is generally faster than using `powf` - fn powi(&self, n: i32) -> Self; - - /// Take the square root of a number. - fn sqrt(&self) -> Self; - /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`. - fn rsqrt(&self) -> Self; - /// Take the cubic root of a number. - fn cbrt(&self) -> Self; - /// Calculate the length of the hypotenuse of a right-angle triangle given - /// legs of length `x` and `y`. - fn hypot(&self, other: &Self) -> Self; - /// Computes the sine of a number (in radians). - fn sin(&self) -> Self; + fn sin(self) -> Self; /// Computes the cosine of a number (in radians). - fn cos(&self) -> Self; + fn cos(self) -> Self; /// Computes the tangent of a number (in radians). - fn tan(&self) -> Self; + fn tan(self) -> Self; /// Computes the arcsine of a number. Return value is in radians in /// the range [-pi/2, pi/2] or NaN if the number is outside the range /// [-1, 1]. - fn asin(&self) -> Self; + fn asin(self) -> Self; /// Computes the arccosine of a number. Return value is in radians in /// the range [0, pi] or NaN if the number is outside the range /// [-1, 1]. - fn acos(&self) -> Self; + fn acos(self) -> Self; /// Computes the arctangent of a number. Return value is in radians in the /// range [-pi/2, pi/2]; - fn atan(&self) -> Self; + fn atan(self) -> Self; /// Computes the four quadrant arctangent of a number, `y`, and another /// number `x`. Return value is in radians in the range [-pi, pi]. - fn atan2(&self, other: &Self) -> Self; + fn atan2(self, other: Self) -> Self; /// Simultaneously computes the sine and cosine of the number, `x`. Returns /// `(sin(x), cos(x))`. - fn sin_cos(&self) -> (Self, Self); + fn sin_cos(self) -> (Self, Self); + + /// Euler's number. + fn e() -> Self; + /// log2(e). + fn log2_e() -> Self; + /// log10(e). + fn log10_e() -> Self; + /// ln(2.0). + fn ln_2() -> Self; + /// ln(10.0). + fn ln_10() -> Self; /// Returns `e^(self)`, (the exponential function). - fn exp(&self) -> Self; + fn exp(self) -> Self; /// Returns 2 raised to the power of the number, `2^(self)`. - fn exp2(&self) -> Self; + fn exp2(self) -> Self; + /// Returns the exponential of the number, minus 1, in a way that is + /// accurate even if the number is close to zero. + fn exp_m1(self) -> Self; /// Returns the natural logarithm of the number. - fn ln(&self) -> Self; + fn ln(self) -> Self; /// Returns the logarithm of the number with respect to an arbitrary base. - fn log(&self, base: &Self) -> Self; + fn log(self, base: Self) -> Self; /// Returns the base 2 logarithm of the number. - fn log2(&self) -> Self; + fn log2(self) -> Self; /// Returns the base 10 logarithm of the number. - fn log10(&self) -> Self; + fn log10(self) -> Self; + /// Returns the natural logarithm of the number plus 1 (`ln(1+n)`) more + /// accurately than if the operations were performed separately. + fn ln_1p(self) -> Self; /// Hyperbolic sine function. - fn sinh(&self) -> Self; + fn sinh(self) -> Self; /// Hyperbolic cosine function. - fn cosh(&self) -> Self; + fn cosh(self) -> Self; /// Hyperbolic tangent function. - fn tanh(&self) -> Self; + fn tanh(self) -> Self; /// Inverse hyperbolic sine function. - fn asinh(&self) -> Self; + fn asinh(self) -> Self; /// Inverse hyperbolic cosine function. - fn acosh(&self) -> Self; + fn acosh(self) -> Self; /// Inverse hyperbolic tangent function. - fn atanh(&self) -> Self; + fn atanh(self) -> Self; /// Convert radians to degrees. - fn to_degrees(&self) -> Self; + fn to_degrees(self) -> Self; /// Convert degrees to radians. - fn to_radians(&self) -> Self; + fn to_radians(self) -> Self; } /// A generic trait for converting a value to a number. diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index ffcb129d63572..bb2fd2a4e257e 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -15,7 +15,7 @@ use clone::Clone; use container::Container; use iter::Iterator; use num::{NumCast, Zero, One, cast, Int}; -use num::{Round, Float, FPNaN, FPInfinite, ToPrimitive}; +use num::{Float, FPNaN, FPInfinite, ToPrimitive}; use num; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use option::{None, Option, Some}; @@ -258,7 +258,7 @@ pub fn int_to_str_bytes_common(num: T, radix: uint, sign: SignFormat, f: * - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict * between digit and exponent sign `'p'`. */ -pub fn float_to_str_bytes_common+Neg+Rem+Mul>( num: T, radix: uint, negative_zero: bool, sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_upper: bool @@ -310,7 +310,7 @@ pub fn float_to_str_bytes_common unreachable!() }; - (num / exp_base.powf(&exp), cast::(exp).unwrap()) + (num / exp_base.powf(exp), cast::(exp).unwrap()) } } }; @@ -491,7 +491,7 @@ pub fn float_to_str_bytes_common+Neg+Rem+Mul>( num: T, radix: uint, negative_zero: bool, sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_capital: bool diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index a44b23c42494e..724c4ca72ad0e 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -45,7 +45,7 @@ pub use iter::{FromIterator, Extendable}; pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator}; pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize}; pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul}; -pub use num::{Signed, Unsigned, Round}; +pub use num::{Signed, Unsigned}; pub use num::{Primitive, Int, Float, ToPrimitive, FromPrimitive}; pub use path::{GenericPath, Path, PosixPath, WindowsPath}; pub use ptr::RawPtr; diff --git a/src/libtest/stats.rs b/src/libtest/stats.rs index 1341b8d230f0b..d55fcc660266b 100644 --- a/src/libtest/stats.rs +++ b/src/libtest/stats.rs @@ -352,8 +352,8 @@ pub fn write_boxplot(w: &mut io::Writer, s: &Summary, let (q1,q2,q3) = s.quartiles; // the .abs() handles the case where numbers are negative - let lomag = (10.0_f64).powf(&(s.min.abs().log10().floor())); - let himag = (10.0_f64).powf(&(s.max.abs().log10().floor())); + let lomag = 10.0_f64.powf(s.min.abs().log10().floor()); + let himag = 10.0_f64.powf(s.max.abs().log10().floor()); // need to consider when the limit is zero let lo = if lomag == 0.0 {