Skip to content

Commit

Permalink
Reorder Float methods in trait definition and make consistent in impls
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Apr 19, 2014
1 parent 42450ef commit 2d9dfc6
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 293 deletions.
205 changes: 105 additions & 100 deletions src/libstd/num/f32.rs
Expand Up @@ -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) }
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -342,13 +330,15 @@ impl Float for f32 {
#[inline]
fn max_10_exp(_: Option<f32>) -> 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`
Expand All @@ -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) };
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) }
Expand Down

5 comments on commit 2d9dfc6

@bors
Copy link
Contributor

@bors bors commented on 2d9dfc6 Apr 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at brendanzab@2d9dfc6

@bors
Copy link
Contributor

@bors bors commented on 2d9dfc6 Apr 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/rust/float-api = 2d9dfc6 into auto

@bors
Copy link
Contributor

@bors bors commented on 2d9dfc6 Apr 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/rust/float-api = 2d9dfc6 merged ok, testing candidate = 30fe550

@bors
Copy link
Contributor

@bors bors commented on 2d9dfc6 Apr 23, 2014

@bors
Copy link
Contributor

@bors bors commented on 2d9dfc6 Apr 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 30fe550

Please sign in to comment.