From fe472020347d8eeb727f3a31e9cdc7268bb579f6 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 18 Apr 2014 12:48:48 +1000 Subject: [PATCH 1/4] Merge the Round trait into the Float trait Move the rounding functions into the `std::num::Float` trait and then remove `std::num::Round`. This continues the flattening of the numeric traits tracked in #10387. The aim is to make `std::num` very simple and tied to the built in types, leaving the definition of more complex numeric towers to third-party libraries. [breaking-change] --- src/libnum/rational.rs | 75 ++++++++++++++++++--------------------- src/libstd/num/f32.rs | 52 +++++++++++++-------------- src/libstd/num/f64.rs | 52 +++++++++++++-------------- src/libstd/num/mod.rs | 37 +++++++++---------- src/libstd/num/strconv.rs | 6 ++-- src/libstd/prelude.rs | 2 +- 6 files changed, 106 insertions(+), 118 deletions(-) diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs index e6b63f23741dc..8f2efc8626bfb 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`. diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 7cd6aaa631086..893897e661afc 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -239,33 +239,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 } @@ -414,6 +387,31 @@ impl Float for f32 { (mantissa as u64, exponent, sign) } + /// 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() } + /// Archimedes' constant #[inline] fn pi() -> f32 { 3.14159265358979323846264338327950288 } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 8b52a6747f478..cf6fadd38aa44 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -247,33 +247,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 } @@ -420,6 +393,31 @@ impl Float for f64 { (mantissa, exponent, sign) } + /// 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() } + /// Archimedes' constant #[inline] fn pi() -> f64 { 3.14159265358979323846264338327950288 } diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 12befed743a52..b3e15a9708674 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,7 +328,7 @@ 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 { +pub trait Float: Signed + Primitive { /// Returns the maximum of the two numbers. fn max(self, other: Self) -> Self; /// Returns the minimum of the two numbers. @@ -431,6 +412,22 @@ pub trait Float: Signed + Round + Primitive { /// Returns the mantissa, exponent and sign as integers, respectively. fn integer_decode(&self) -> (u64, i16, i8); + /// 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; + /// Archimedes' constant. fn pi() -> Self; diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index ffcb129d63572..73dfbdd088e9a 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 @@ -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; From bed70a42ecf0747f924c813b3b375d5fd364ffc3 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 18 Apr 2014 13:49:37 +1000 Subject: [PATCH 2/4] 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] --- src/doc/guide-tasks.md | 4 +- src/libnum/complex.rs | 4 +- src/libnum/rational.rs | 12 ++-- src/librand/distributions/gamma.rs | 2 +- src/libstd/num/f32.rs | 108 ++++++++++++++--------------- src/libstd/num/f64.rs | 108 ++++++++++++++--------------- src/libstd/num/mod.rs | 90 ++++++++++++------------ src/libstd/num/strconv.rs | 2 +- src/libtest/stats.rs | 4 +- 9 files changed, 167 insertions(+), 167 deletions(-) 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 8f2efc8626bfb..cff1fb30b567a 100644 --- a/src/libnum/rational.rs +++ b/src/libnum/rational.rs @@ -631,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 893897e661afc..2436ed1e95e97 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -250,7 +250,7 @@ impl Bounded for f32 { impl Primitive for f32 {} impl Float for f32 { - fn powi(&self, n: i32) -> f32 { unsafe{intrinsics::powif32(*self, n)} } + fn powi(self, n: i32) -> f32 { unsafe{intrinsics::powif32(self, n)} } #[inline] fn max(self, other: f32) -> f32 { @@ -276,33 +276,33 @@ 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 { + 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, @@ -342,10 +342,10 @@ impl Float for f32 { /// - `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) } } @@ -353,27 +353,27 @@ impl Float for f32 { /// 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)} } + 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)} } + 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)} } + 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)} } + 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) { + fn integer_decode(self) -> (u64, i16, i8) { let bits: u32 = unsafe { - ::cast::transmute(*self) + ::cast::transmute(self) }; let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; @@ -389,19 +389,19 @@ impl Float for f32 { /// Round half-way cases toward `NEG_INFINITY` #[inline] - fn floor(&self) -> f32 { unsafe{intrinsics::floorf32(*self)} } + fn floor(self) -> f32 { unsafe{intrinsics::floorf32(self)} } /// Round half-way cases toward `INFINITY` #[inline] - fn ceil(&self) -> f32 { unsafe{intrinsics::ceilf32(*self)} } + 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)} } + 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)} } + fn trunc(self) -> f32 { unsafe{intrinsics::truncf32(self)} } /// The fractional part of the number, satisfying: /// @@ -410,7 +410,7 @@ impl Float for f32 { /// assert!(x == x.trunc() + x.fract()) /// ``` #[inline] - fn fract(&self) -> f32 { *self - self.trunc() } + fn fract(self) -> f32 { self - self.trunc() } /// Archimedes' constant #[inline] @@ -482,82 +482,82 @@ impl Float for f32 { /// The reciprocal (multiplicative inverse) of the number #[inline] - fn recip(&self) -> f32 { 1.0 / *self } + fn recip(self) -> f32 { 1.0 / self } #[inline] - fn powf(&self, n: &f32) -> f32 { unsafe{intrinsics::powf32(*self, *n)} } + fn powf(self, n: f32) -> f32 { unsafe{intrinsics::powf32(self, n)} } #[inline] - fn sqrt(&self) -> f32 { unsafe{intrinsics::sqrtf32(*self)} } + fn sqrt(self) -> f32 { unsafe{intrinsics::sqrtf32(self)} } #[inline] - fn rsqrt(&self) -> f32 { self.sqrt().recip() } + fn rsqrt(self) -> f32 { self.sqrt().recip() } #[inline] - fn cbrt(&self) -> f32 { unsafe{cmath::cbrtf(*self)} } + fn cbrt(self) -> f32 { unsafe{cmath::cbrtf(self)} } #[inline] - fn hypot(&self, other: &f32) -> f32 { unsafe{cmath::hypotf(*self, *other)} } + fn hypot(self, other: f32) -> f32 { unsafe{cmath::hypotf(self, other)} } #[inline] - fn sin(&self) -> f32 { unsafe{intrinsics::sinf32(*self)} } + fn sin(self) -> f32 { unsafe{intrinsics::sinf32(self)} } #[inline] - fn cos(&self) -> f32 { unsafe{intrinsics::cosf32(*self)} } + fn cos(self) -> f32 { unsafe{intrinsics::cosf32(self)} } #[inline] - fn tan(&self) -> f32 { unsafe{cmath::tanf(*self)} } + fn tan(self) -> f32 { unsafe{cmath::tanf(self)} } #[inline] - fn asin(&self) -> f32 { unsafe{cmath::asinf(*self)} } + fn asin(self) -> f32 { unsafe{cmath::asinf(self)} } #[inline] - fn acos(&self) -> f32 { unsafe{cmath::acosf(*self)} } + fn acos(self) -> f32 { unsafe{cmath::acosf(self)} } #[inline] - fn atan(&self) -> f32 { unsafe{cmath::atanf(*self)} } + fn atan(self) -> f32 { unsafe{cmath::atanf(self)} } #[inline] - fn atan2(&self, other: &f32) -> f32 { unsafe{cmath::atan2f(*self, *other)} } + 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()) } /// 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 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)} } #[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 /// @@ -567,8 +567,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(), } @@ -582,8 +582,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(), } @@ -600,19 +600,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) } } @@ -1162,7 +1162,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 cf6fadd38aa44..3fb5d793db04e 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -282,33 +282,33 @@ 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 { + 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, @@ -348,10 +348,10 @@ impl Float for f64 { /// - `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) } } @@ -359,27 +359,27 @@ impl Float for f64 { /// 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)} } + 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)} } + 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)} } + 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)} } + 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) { + fn integer_decode(self) -> (u64, i16, i8) { let bits: u64 = unsafe { - ::cast::transmute(*self) + ::cast::transmute(self) }; let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16; @@ -395,19 +395,19 @@ impl Float for f64 { /// Round half-way cases toward `NEG_INFINITY` #[inline] - fn floor(&self) -> f64 { unsafe{intrinsics::floorf64(*self)} } + fn floor(self) -> f64 { unsafe{intrinsics::floorf64(self)} } /// Round half-way cases toward `INFINITY` #[inline] - fn ceil(&self) -> f64 { unsafe{intrinsics::ceilf64(*self)} } + 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)} } + 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)} } + fn trunc(self) -> f64 { unsafe{intrinsics::truncf64(self)} } /// The fractional part of the number, satisfying: /// @@ -416,7 +416,7 @@ impl Float for f64 { /// assert!(x == x.trunc() + x.fract()) /// ``` #[inline] - fn fract(&self) -> f64 { *self - self.trunc() } + fn fract(self) -> f64 { self - self.trunc() } /// Archimedes' constant #[inline] @@ -488,85 +488,85 @@ impl Float for f64 { /// The reciprocal (multiplicative inverse) of the number #[inline] - fn recip(&self) -> f64 { 1.0 / *self } + fn recip(self) -> f64 { 1.0 / self } #[inline] - fn powf(&self, n: &f64) -> f64 { unsafe{intrinsics::powf64(*self, *n)} } + fn powf(self, n: f64) -> f64 { unsafe{intrinsics::powf64(self, n)} } #[inline] - fn powi(&self, n: i32) -> f64 { unsafe{intrinsics::powif64(*self, n)} } + fn powi(self, n: i32) -> f64 { unsafe{intrinsics::powif64(self, n)} } #[inline] - fn sqrt(&self) -> f64 { unsafe{intrinsics::sqrtf64(*self)} } + fn sqrt(self) -> f64 { unsafe{intrinsics::sqrtf64(self)} } #[inline] - fn rsqrt(&self) -> f64 { self.sqrt().recip() } + fn rsqrt(self) -> f64 { self.sqrt().recip() } #[inline] - fn cbrt(&self) -> f64 { unsafe{cmath::cbrt(*self)} } + fn cbrt(self) -> f64 { unsafe{cmath::cbrt(self)} } #[inline] - fn hypot(&self, other: &f64) -> f64 { unsafe{cmath::hypot(*self, *other)} } + fn hypot(self, other: f64) -> f64 { unsafe{cmath::hypot(self, other)} } #[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()) } /// 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 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)} } #[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 /// @@ -576,8 +576,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(), } @@ -591,8 +591,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(), } @@ -609,19 +609,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) } } @@ -1165,7 +1165,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 b3e15a9708674..d825b1c2f01b6 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -347,19 +347,19 @@ pub trait Float: Signed + Primitive { fn neg_zero() -> Self; /// Returns true if this value is NaN and false otherwise. - fn is_nan(&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; + 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; @@ -391,42 +391,42 @@ pub trait Float: Signed + Primitive { /// * `self = x * pow(2, exp)` /// /// * `0.5 <= abs(x) < 1.0` - fn frexp(&self) -> (Self, int); + fn frexp(self) -> (Self, int); /// 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; + fn exp_m1(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; + 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; + fn mul_add(self, a: Self, b: Self) -> Self; /// Returns the next representable floating-point value in the direction of `other`. - fn next_after(&self, other: Self) -> Self; + fn next_after(self, other: Self) -> Self; /// Returns the mantissa, exponent and sign as integers, respectively. - fn integer_decode(&self) -> (u64, i16, i8); + fn integer_decode(self) -> (u64, i16, i8); /// Return the largest integer less than or equal to a number. - fn floor(&self) -> Self; + fn floor(self) -> Self; /// Return the smallest integer greater than or equal to a number. - fn ceil(&self) -> Self; + fn ceil(self) -> Self; /// Return the nearest integer to a number. Round half-way cases away from /// `0.0`. - fn round(&self) -> Self; + fn round(self) -> Self; /// Return the integer part of a number. - fn trunc(&self) -> Self; + fn trunc(self) -> Self; /// Return the fractional part of a number. - fn fract(&self) -> Self; + fn fract(self) -> Self; /// Archimedes' constant. fn pi() -> Self; @@ -480,81 +480,81 @@ pub trait Float: Signed + Primitive { fn ln_10() -> Self; /// Take the reciprocal (inverse) of a number, `1/x`. - fn recip(&self) -> Self; + fn recip(self) -> Self; /// Raise a number to a power. - fn powf(&self, n: &Self) -> Self; + 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; + fn powi(self, n: i32) -> Self; /// Take the square root of a number. - fn sqrt(&self) -> Self; + fn sqrt(self) -> Self; /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`. - fn rsqrt(&self) -> Self; + fn rsqrt(self) -> Self; /// Take the cubic root of a number. - fn cbrt(&self) -> Self; + 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; + 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); /// 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 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; /// 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 73dfbdd088e9a..bb2fd2a4e257e 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -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()) } } }; 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 { From 42450ef02231f60f7ed41d78d04968b78f155404 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 19 Apr 2014 01:27:51 +1000 Subject: [PATCH 3/4] Fix formatting in float implementations --- src/libstd/num/f32.rs | 135 +++++++++++++++++++++++++++++++----------- src/libstd/num/f64.rs | 135 +++++++++++++++++++++++++++++++----------- 2 files changed, 198 insertions(+), 72 deletions(-) diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 2436ed1e95e97..7525aa80a3974 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,16 @@ 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. #[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 +232,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` @@ -250,7 +257,9 @@ impl Bounded for f32 { impl Primitive for f32 {} impl Float for f32 { - fn powi(self, n: i32) -> f32 { unsafe{intrinsics::powif32(self, n)} } + fn powi(self, n: i32) -> f32 { + unsafe { intrinsics::powif32(self, n) } + } #[inline] fn max(self, other: f32) -> f32 { @@ -302,7 +311,7 @@ impl Float for f32 { 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, @@ -335,7 +344,9 @@ impl Float for f32 { /// 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: /// @@ -353,28 +364,34 @@ impl Float for f32 { /// 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)} } + 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)} } + 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)} } + 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)} } + 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) - }; + 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 { @@ -389,19 +406,27 @@ impl Float for f32 { /// Round half-way cases toward `NEG_INFINITY` #[inline] - fn floor(self) -> f32 { unsafe{intrinsics::floorf32(self)} } + fn floor(self) -> f32 { + unsafe { intrinsics::floorf32(self) } + } /// Round half-way cases toward `INFINITY` #[inline] - fn ceil(self) -> f32 { unsafe{intrinsics::ceilf32(self)} } + 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)} } + 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)} } + fn trunc(self) -> f32 { + unsafe { intrinsics::truncf32(self) } + } /// The fractional part of the number, satisfying: /// @@ -485,40 +510,62 @@ impl Float for f32 { fn recip(self) -> f32 { 1.0 / self } #[inline] - fn powf(self, n: f32) -> f32 { unsafe{intrinsics::powf32(self, n)} } + fn powf(self, n: f32) -> f32 { + unsafe { intrinsics::powf32(self, n) } + } #[inline] - fn sqrt(self) -> f32 { unsafe{intrinsics::sqrtf32(self)} } + fn sqrt(self) -> f32 { + unsafe { intrinsics::sqrtf32(self) } + } #[inline] fn rsqrt(self) -> f32 { self.sqrt().recip() } #[inline] - fn cbrt(self) -> f32 { unsafe{cmath::cbrtf(self)} } + fn cbrt(self) -> f32 { + unsafe { cmath::cbrtf(self) } + } #[inline] - fn hypot(self, other: f32) -> f32 { unsafe{cmath::hypotf(self, other)} } + fn hypot(self, other: f32) -> f32 { + unsafe { cmath::hypotf(self, other) } + } #[inline] - fn sin(self) -> f32 { unsafe{intrinsics::sinf32(self)} } + fn sin(self) -> f32 { + unsafe { intrinsics::sinf32(self) } + } #[inline] - fn cos(self) -> f32 { unsafe{intrinsics::cosf32(self)} } + fn cos(self) -> f32 { + unsafe { intrinsics::cosf32(self) } + } #[inline] - fn tan(self) -> f32 { unsafe{cmath::tanf(self)} } + fn tan(self) -> f32 { + unsafe { cmath::tanf(self) } + } #[inline] - fn asin(self) -> f32 { unsafe{cmath::asinf(self)} } + fn asin(self) -> f32 { + unsafe { cmath::asinf(self) } + } #[inline] - fn acos(self) -> f32 { unsafe{cmath::acosf(self)} } + fn acos(self) -> f32 { + unsafe { cmath::acosf(self) } + } #[inline] - fn atan(self) -> f32 { unsafe{cmath::atanf(self)} } + fn atan(self) -> f32 { + unsafe { cmath::atanf(self) } + } #[inline] - fn atan2(self, other: f32) -> f32 { unsafe{cmath::atan2f(self, other)} } + fn atan2(self, other: f32) -> f32 { + unsafe { cmath::atan2f(self, other) } + } /// Simultaneously computes the sine and cosine of the number #[inline] @@ -528,15 +575,21 @@ impl Float for f32 { /// 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 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] @@ -544,20 +597,30 @@ impl Float for f32 { /// 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) } + } #[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 /// diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 3fb5d793db04e..7d0ad1029e7fb 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` @@ -308,7 +315,7 @@ impl Float for f64 { 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, @@ -341,7 +348,9 @@ impl Float for f64 { /// 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: /// @@ -359,28 +368,34 @@ impl Float for f64 { /// 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)} } + 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)} } + 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)} } + 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)} } + 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) - }; + 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 { @@ -395,19 +410,27 @@ impl Float for f64 { /// Round half-way cases toward `NEG_INFINITY` #[inline] - fn floor(self) -> f64 { unsafe{intrinsics::floorf64(self)} } + fn floor(self) -> f64 { + unsafe { intrinsics::floorf64(self) } + } /// Round half-way cases toward `INFINITY` #[inline] - fn ceil(self) -> f64 { unsafe{intrinsics::ceilf64(self)} } + 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)} } + 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)} } + fn trunc(self) -> f64 { + unsafe { intrinsics::truncf64(self) } + } /// The fractional part of the number, satisfying: /// @@ -491,43 +514,67 @@ impl Float for f64 { fn recip(self) -> f64 { 1.0 / self } #[inline] - fn powf(self, n: f64) -> f64 { unsafe{intrinsics::powf64(self, n)} } + fn powf(self, n: f64) -> f64 { + unsafe { intrinsics::powf64(self, n) } + } #[inline] - fn powi(self, n: i32) -> f64 { unsafe{intrinsics::powif64(self, n)} } + fn powi(self, n: i32) -> f64 { + unsafe { intrinsics::powif64(self, n) } + } #[inline] - fn sqrt(self) -> f64 { unsafe{intrinsics::sqrtf64(self)} } + fn sqrt(self) -> f64 { + unsafe { intrinsics::sqrtf64(self) } + } #[inline] fn rsqrt(self) -> f64 { self.sqrt().recip() } #[inline] - fn cbrt(self) -> f64 { unsafe{cmath::cbrt(self)} } + fn cbrt(self) -> f64 { + unsafe { cmath::cbrt(self) } + } #[inline] - fn hypot(self, other: f64) -> f64 { unsafe{cmath::hypot(self, other)} } + fn hypot(self, other: f64) -> f64 { + unsafe { cmath::hypot(self, other) } + } #[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] @@ -537,15 +584,21 @@ impl Float for f64 { /// 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 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] @@ -553,20 +606,30 @@ impl Float for f64 { /// 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) } + } #[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 /// From 2d9dfc64792d14ba46b345b7e7f2913c6a9b28ca Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 19 Apr 2014 02:15:09 +1000 Subject: [PATCH 4/4] Reorder Float methods in trait definition and make consistent in impls --- src/libstd/num/f32.rs | 205 +++++++++++++++++++++--------------------- src/libstd/num/f64.rs | 200 +++++++++++++++++++++-------------------- src/libstd/num/mod.rs | 156 +++++++++++++------------------- 3 files changed, 268 insertions(+), 293 deletions(-) diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 7525aa80a3974..5da03898f05d4 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -218,8 +218,9 @@ impl Signed for 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) } @@ -257,20 +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 } @@ -305,8 +292,9 @@ impl Float for f32 { 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. + /// 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; @@ -342,13 +330,15 @@ 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) } } - /// 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` @@ -361,34 +351,6 @@ impl Float for f32 { } } - /// 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) }; @@ -404,6 +366,13 @@ impl Float for f32 { (mantissa as u64, exponent, sign) } + /// 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) } + } + /// Round half-way cases toward `NEG_INFINITY` #[inline] fn floor(self) -> f32 { @@ -437,100 +406,102 @@ impl Float for f32 { #[inline] fn fract(self) -> f32 { self - self.trunc() } - /// Archimedes' constant - #[inline] - fn pi() -> f32 { 3.14159265358979323846264338327950288 } - - /// 2.0 * pi #[inline] - fn two_pi() -> f32 { 6.28318530717958647692528676655900576 } + fn max(self, other: f32) -> f32 { + unsafe { cmath::fmaxf(self, other) } + } - /// pi / 2.0 #[inline] - fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 } + fn min(self, other: f32) -> f32 { + unsafe { cmath::fminf(self, other) } + } - /// pi / 3.0 + /// 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_pi_3() -> f32 { 1.04719755119659774615421446109316763 } + fn mul_add(self, a: f32, b: f32) -> f32 { + unsafe { intrinsics::fmaf32(self, a, b) } + } - /// pi / 4.0 + /// The reciprocal (multiplicative inverse) of the number #[inline] - fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 } + fn recip(self) -> f32 { 1.0 / self } - /// pi / 6.0 - #[inline] - fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 } + fn powi(self, n: i32) -> f32 { + unsafe { intrinsics::powif32(self, n) } + } - /// pi / 8.0 #[inline] - fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 } + fn powf(self, n: f32) -> f32 { + unsafe { intrinsics::powf32(self, n) } + } - /// 1 .0/ pi + /// sqrt(2.0) #[inline] - fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 } + fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 } - /// 2.0 / pi + /// 1.0 / sqrt(2.0) #[inline] - fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 } + fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 } - /// 2.0 / sqrt(pi) #[inline] - fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 } + fn sqrt(self) -> f32 { + unsafe { intrinsics::sqrtf32(self) } + } - /// sqrt(2.0) #[inline] - fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 } + fn rsqrt(self) -> f32 { self.sqrt().recip() } - /// 1.0 / sqrt(2.0) #[inline] - fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 } + fn cbrt(self) -> f32 { + unsafe { cmath::cbrtf(self) } + } - /// Euler's number #[inline] - fn e() -> f32 { 2.71828182845904523536028747135266250 } + fn hypot(self, other: f32) -> f32 { + unsafe { cmath::hypotf(self, other) } + } - /// log2(e) + /// Archimedes' constant #[inline] - fn log2_e() -> f32 { 1.44269504088896340735992468100189214 } + fn pi() -> f32 { 3.14159265358979323846264338327950288 } - /// log10(e) + /// 2.0 * pi #[inline] - fn log10_e() -> f32 { 0.434294481903251827651128918916605082 } + fn two_pi() -> f32 { 6.28318530717958647692528676655900576 } - /// ln(2.0) + /// pi / 2.0 #[inline] - fn ln_2() -> f32 { 0.693147180559945309417232121458176568 } + fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 } - /// ln(10.0) + /// pi / 3.0 #[inline] - fn ln_10() -> f32 { 2.30258509299404568401799145468436421 } + fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 } - /// The reciprocal (multiplicative inverse) of the number + /// pi / 4.0 #[inline] - fn recip(self) -> f32 { 1.0 / self } + fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 } + /// pi / 6.0 #[inline] - fn powf(self, n: f32) -> f32 { - unsafe { intrinsics::powf32(self, n) } - } + fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 } + /// pi / 8.0 #[inline] - fn sqrt(self) -> f32 { - unsafe { intrinsics::sqrtf32(self) } - } + fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 } + /// 1 .0/ pi #[inline] - fn rsqrt(self) -> f32 { self.sqrt().recip() } + fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 } + /// 2.0 / pi #[inline] - fn cbrt(self) -> f32 { - unsafe { cmath::cbrtf(self) } - } + fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 } + /// 2.0 / sqrt(pi) #[inline] - fn hypot(self, other: f32) -> f32 { - unsafe { cmath::hypotf(self, other) } - } + fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 } #[inline] fn sin(self) -> f32 { @@ -573,6 +544,26 @@ impl Float for 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 { @@ -585,6 +576,13 @@ impl Float for 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 { @@ -607,6 +605,13 @@ impl Float for 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) } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 7d0ad1029e7fb..a2b63968569b4 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -265,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,8 +299,9 @@ impl Float for f64 { 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. + /// 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; @@ -346,13 +337,15 @@ 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) } } - /// 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` @@ -365,34 +358,6 @@ impl Float for f64 { } } - /// 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) }; @@ -408,6 +373,13 @@ impl Float for f64 { (mantissa, exponent, sign) } + /// 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) } + } + /// Round half-way cases toward `NEG_INFINITY` #[inline] fn floor(self) -> f64 { @@ -441,45 +413,37 @@ impl Float for f64 { #[inline] fn fract(self) -> f64 { self - self.trunc() } - /// Archimedes' constant - #[inline] - fn pi() -> f64 { 3.14159265358979323846264338327950288 } - - /// 2.0 * pi - #[inline] - fn two_pi() -> f64 { 6.28318530717958647692528676655900576 } - - /// pi / 2.0 - #[inline] - fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 } - - /// pi / 3.0 #[inline] - fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 } - - /// pi / 4.0 - #[inline] - fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 } + fn max(self, other: f64) -> f64 { + unsafe { cmath::fmax(self, other) } + } - /// pi / 6.0 #[inline] - fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 } + fn min(self, other: f64) -> f64 { + unsafe { cmath::fmin(self, other) } + } - /// pi / 8.0 + /// 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_pi_8() -> f64 { 0.39269908169872415480783042290993786 } + fn mul_add(self, a: f64, b: f64) -> f64 { + unsafe { intrinsics::fmaf64(self, a, b) } + } - /// 1.0 / pi + /// The reciprocal (multiplicative inverse) of the number #[inline] - fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 } + fn recip(self) -> f64 { 1.0 / self } - /// 2.0 / pi #[inline] - fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 } + fn powf(self, n: f64) -> f64 { + unsafe { intrinsics::powf64(self, n) } + } - /// 2.0 / sqrt(pi) #[inline] - fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 } + fn powi(self, n: i32) -> f64 { + unsafe { intrinsics::powif64(self, n) } + } /// sqrt(2.0) #[inline] @@ -489,57 +453,63 @@ 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 cbrt(self) -> f64 { - unsafe { cmath::cbrt(self) } - } + fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 } + /// 1.0 / pi #[inline] - fn hypot(self, other: f64) -> f64 { - unsafe { cmath::hypot(self, other) } - } + fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 } + + /// 2.0 / pi + #[inline] + fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 } + + /// 2.0 / sqrt(pi) + #[inline] + fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 } #[inline] fn sin(self) -> f64 { @@ -582,6 +552,26 @@ impl Float for 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 { @@ -594,6 +584,13 @@ impl Float for 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 { @@ -616,6 +613,13 @@ impl Float for 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) } diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index d825b1c2f01b6..75cf02034b992 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -329,177 +329,126 @@ 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 + 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; - /// 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. + /// 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; - /// Returns true if this number is neither zero, infinite, denormal, or NaN. fn is_normal(self) -> bool; - /// Returns the category that this number falls into. 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); - - /// 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 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; - - /// Returns the next representable floating-point value in the direction of `other`. - fn next_after(self, other: Self) -> Self; - /// 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 maximum of the two numbers. + fn max(self, other: Self) -> Self; + /// Returns the minimum of the two numbers. + fn min(self, other: 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; + + /// 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; + + /// sqrt(2.0). + fn sqrt2() -> Self; + /// 1.0 / sqrt(2.0). + fn frac_1_sqrt2() -> 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; + /// 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; /// Computes the cosine of a number (in radians). @@ -525,10 +474,24 @@ pub trait Float: Signed + Primitive { /// `(sin(x), cos(x))`. 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; /// Returns 2 raised to the power of the number, `2^(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; /// Returns the logarithm of the number with respect to an arbitrary base. @@ -537,6 +500,9 @@ pub trait Float: Signed + Primitive { fn log2(self) -> Self; /// Returns the base 10 logarithm of the number. 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;