]> git.lizzy.rs Git - rust.git/commitdiff
auto merge of #12321 : bjz/rust/remove-real, r=alexcrichton
authorbors <bors@rust-lang.org>
Tue, 18 Feb 2014 06:16:51 +0000 (22:16 -0800)
committerbors <bors@rust-lang.org>
Tue, 18 Feb 2014 06:16:51 +0000 (22:16 -0800)
This is part of the effort to simplify `std::num`, as tracked in issue #10387. It is also a step towards a proper IEEE-754 trait (see #12281).

1  2 
src/etc/vim/syntax/rust.vim
src/libstd/num/mod.rs
src/libstd/prelude.rs

index 03c67276049ec32fefa8ac3e633b5109737554c2,371c78b2b6843aa64e000cd1e3715c2a48e020b8..511e0c9f74cc35be5a132f6fa8ab3a4c9fb99533
@@@ -85,7 -85,7 +85,7 @@@ syn keyword rustTrait Iterator DoubleEn
  syn keyword rustTrait OrdIterator MutableDoubleEndedIterator ExactSize
  
  syn keyword rustTrait Algebraic Trigonometric Exponential Hyperbolic
- syn keyword rustTrait Bitwise Bounded Integer Fractional Real RealExt
+ syn keyword rustTrait Bitwise Bounded Integer
  syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul CheckedDiv
  syn keyword rustTrait Orderable Signed Unsigned Round
  syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive
@@@ -95,9 -95,13 +95,9 @@@ syn keyword rustTrait Buffer Writer Rea
  syn keyword rustTrait Str StrVector StrSlice OwnedStr IntoMaybeOwned
  syn keyword rustTrait IterBytes
  syn keyword rustTrait ToStr IntoStr
 -syn keyword rustTrait CloneableTuple ImmutableTuple
  syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
  syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
  syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12
 -syn keyword rustTrait ImmutableTuple1 ImmutableTuple2 ImmutableTuple3 ImmutableTuple4
 -syn keyword rustTrait ImmutableTuple5 ImmutableTuple6 ImmutableTuple7 ImmutableTuple8
 -syn keyword rustTrait ImmutableTuple9 ImmutableTuple10 ImmutableTuple11 ImmutableTuple12
  syn keyword rustTrait ImmutableEqVector ImmutableTotalOrdVector ImmutableCloneableVector
  syn keyword rustTrait OwnedVector OwnedCloneableVector OwnedEqVector MutableVector
  syn keyword rustTrait Vector VectorVector CloneableVector ImmutableVector
diff --combined src/libstd/num/mod.rs
index 33690a5fddb0f5938cae488d39933ddf14c48c7b,a954e7673222f64bf0b4bb5fa4ebf00a0f5f6f10..026e7ebbd48d59aa003d9e7f02ad9a653ac065ff
@@@ -166,115 -166,6 +166,6 @@@ pub trait Round 
      fn fract(&self) -> Self;
  }
  
- /// Defines constants and methods common to real numbers
- pub trait Real: Signed
-               + Ord
-               + Round
-               + Div<Self,Self> {
-     // Common Constants
-     // FIXME (#5527): These should be associated constants
-     fn pi() -> Self;
-     fn two_pi() -> Self;
-     fn frac_pi_2() -> Self;
-     fn frac_pi_3() -> Self;
-     fn frac_pi_4() -> Self;
-     fn frac_pi_6() -> Self;
-     fn frac_pi_8() -> Self;
-     fn frac_1_pi() -> Self;
-     fn frac_2_pi() -> Self;
-     fn frac_2_sqrtpi() -> Self;
-     fn sqrt2() -> Self;
-     fn frac_1_sqrt2() -> Self;
-     fn e() -> Self;
-     fn log2_e() -> Self;
-     fn log10_e() -> Self;
-     fn ln_2() -> Self;
-     fn ln_10() -> Self;
-     // Fractional functions
-     /// Take the reciprocal (inverse) of a number, `1/x`.
-     fn recip(&self) -> Self;
-     // Algebraic functions
-     /// Raise a number to a power.
-     fn powf(&self, n: &Self) -> 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;
-     // Trigonometric functions
-     /// Computes the sine of a number (in radians).
-     fn sin(&self) -> Self;
-     /// Computes the cosine of a number (in radians).
-     fn cos(&self) -> Self;
-     /// Computes the tangent of a number (in radians).
-     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;
-     /// 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;
-     /// Computes the arctangent of a number. Return value is in radians in the
-     /// range [-pi/2, pi/2];
-     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;
-     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
-     /// `(sin(x), cos(x))`.
-     fn sin_cos(&self) -> (Self, Self);
-     // Exponential functions
-     /// 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 natural logarithm of the number.
-     fn ln(&self) -> Self;
-     /// Returns the logarithm of the number with respect to an arbitrary base.
-     fn log(&self, base: &Self) -> Self;
-     /// Returns the base 2 logarithm of the number.
-     fn log2(&self) -> Self;
-     /// Returns the base 10 logarithm of the number.
-     fn log10(&self) -> Self;
-     // Hyperbolic functions
-     /// Hyperbolic sine function.
-     fn sinh(&self) -> Self;
-     /// Hyperbolic cosine function.
-     fn cosh(&self) -> Self;
-     /// Hyperbolic tangent function.
-     fn tanh(&self) -> Self;
-     /// Inverse hyperbolic sine function.
-     fn asinh(&self) -> Self;
-     /// Inverse hyperbolic cosine function.
-     fn acosh(&self) -> Self;
-     /// Inverse hyperbolic tangent function.
-     fn atanh(&self) -> Self;
-     // Angular conversions
-     /// Convert radians to degrees.
-     fn to_degrees(&self) -> Self;
-     /// Convert degrees to radians.
-     fn to_radians(&self) -> Self;
- }
  /// Raises a value to the power of exp, using exponentiation by squaring.
  ///
  /// # Example
@@@ -300,67 -191,6 +191,6 @@@ pub fn pow<T: One + Mul<T, T>>(mut base
      }
  }
  
- /// Raise a number to a power.
- ///
- /// # Example
- ///
- /// ```rust
- /// use std::num;
- ///
- /// let sixteen: f64 = num::powf(2.0, 4.0);
- /// assert_eq!(sixteen, 16.0);
- /// ```
- #[inline(always)] pub fn powf<T: Real>(value: T, n: T) -> T { value.powf(&n) }
- /// Take the square root of a number.
- #[inline(always)] pub fn sqrt<T: Real>(value: T) -> T { value.sqrt() }
- /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
- #[inline(always)] pub fn rsqrt<T: Real>(value: T) -> T { value.rsqrt() }
- /// Take the cubic root of a number.
- #[inline(always)] pub fn cbrt<T: Real>(value: T) -> T { value.cbrt() }
- /// Calculate the length of the hypotenuse of a right-angle triangle given legs of length `x` and
- /// `y`.
- #[inline(always)] pub fn hypot<T: Real>(x: T, y: T) -> T { x.hypot(&y) }
- /// Sine function.
- #[inline(always)] pub fn sin<T: Real>(value: T) -> T { value.sin() }
- /// Cosine function.
- #[inline(always)] pub fn cos<T: Real>(value: T) -> T { value.cos() }
- /// Tangent function.
- #[inline(always)] pub fn tan<T: Real>(value: T) -> T { value.tan() }
- /// Compute the arcsine of the number.
- #[inline(always)] pub fn asin<T: Real>(value: T) -> T { value.asin() }
- /// Compute the arccosine of the number.
- #[inline(always)] pub fn acos<T: Real>(value: T) -> T { value.acos() }
- /// Compute the arctangent of the number.
- #[inline(always)] pub fn atan<T: Real>(value: T) -> T { value.atan() }
- /// Compute the arctangent with 2 arguments.
- #[inline(always)] pub fn atan2<T: Real>(x: T, y: T) -> T { x.atan2(&y) }
- /// Simultaneously computes the sine and cosine of the number.
- #[inline(always)] pub fn sin_cos<T: Real>(value: T) -> (T, T) { value.sin_cos() }
- /// Returns `e^(value)`, (the exponential function).
- #[inline(always)] pub fn exp<T: Real>(value: T) -> T { value.exp() }
- /// Returns 2 raised to the power of the number, `2^(value)`.
- #[inline(always)] pub fn exp2<T: Real>(value: T) -> T { value.exp2() }
- /// Returns the natural logarithm of the number.
- #[inline(always)] pub fn ln<T: Real>(value: T) -> T { value.ln() }
- /// Returns the logarithm of the number with respect to an arbitrary base.
- #[inline(always)] pub fn log<T: Real>(value: T, base: T) -> T { value.log(&base) }
- /// Returns the base 2 logarithm of the number.
- #[inline(always)] pub fn log2<T: Real>(value: T) -> T { value.log2() }
- /// Returns the base 10 logarithm of the number.
- #[inline(always)] pub fn log10<T: Real>(value: T) -> T { value.log10() }
- /// Hyperbolic sine function.
- #[inline(always)] pub fn sinh<T: Real>(value: T) -> T { value.sinh() }
- /// Hyperbolic cosine function.
- #[inline(always)] pub fn cosh<T: Real>(value: T) -> T { value.cosh() }
- /// Hyperbolic tangent function.
- #[inline(always)] pub fn tanh<T: Real>(value: T) -> T { value.tanh() }
- /// Inverse hyperbolic sine function.
- #[inline(always)] pub fn asinh<T: Real>(value: T) -> T { value.asinh() }
- /// Inverse hyperbolic cosine function.
- #[inline(always)] pub fn acosh<T: Real>(value: T) -> T { value.acosh() }
- /// Inverse hyperbolic tangent function.
- #[inline(always)] pub fn atanh<T: Real>(value: T) -> T { value.atanh() }
  pub trait Bounded {
      // FIXME (#5527): These should be associated constants
      fn min_value() -> Self;
@@@ -375,35 -205,18 +205,35 @@@ pub trait Bitwise: Bounde
                   + BitXor<Self,Self>
                   + Shl<Self,Self>
                   + Shr<Self,Self> {
 -    /// Returns the number of bits set in the number.
 +    /// Returns the number of ones in the binary representation of the number.
      ///
      /// # Example
      ///
      /// ```rust
      /// use std::num::Bitwise;
      ///
 -    /// let n = 0b0101000u16;
 -    /// assert_eq!(n.population_count(), 2);
 +    /// let n = 0b01001100u8;
 +    /// assert_eq!(n.count_ones(), 3);
 +    /// ```
 +    fn count_ones(&self) -> Self;
 +
 +    /// Returns the number of zeros in the binary representation of the number.
 +    ///
 +    /// # Example
 +    ///
 +    /// ```rust
 +    /// use std::num::Bitwise;
 +    ///
 +    /// let n = 0b01001100u8;
 +    /// assert_eq!(n.count_zeros(), 5);
      /// ```
 -    fn population_count(&self) -> Self;
 -    /// Returns the number of leading zeros in the number.
 +    #[inline]
 +    fn count_zeros(&self) -> Self {
 +        (!*self).count_ones()
 +    }
 +
 +    /// Returns the number of leading zeros in the in the binary representation
 +    /// of the number.
      ///
      /// # Example
      ///
      /// assert_eq!(n.leading_zeros(), 10);
      /// ```
      fn leading_zeros(&self) -> Self;
 -    /// Returns the number of trailing zeros in the number.
 +
 +    /// Returns the number of trailing zeros in the in the binary representation
 +    /// of the number.
      ///
      /// # Example
      ///
@@@ -492,8 -303,8 +322,8 @@@ pub enum FPCategory 
  }
  
  /// Primitive floating point numbers
- pub trait Float: Real
-                + Signed
+ pub trait Float: Signed
+                + Round
                 + Primitive {
      // FIXME (#5527): These should be associated constants
      fn nan() -> Self;
      fn next_after(&self, other: Self) -> Self;
  
      fn integer_decode(&self) -> (u64, i16, i8);
+     // Common Mathematical Constants
+     // FIXME (#5527): These should be associated constants
+     fn pi() -> Self;
+     fn two_pi() -> Self;
+     fn frac_pi_2() -> Self;
+     fn frac_pi_3() -> Self;
+     fn frac_pi_4() -> Self;
+     fn frac_pi_6() -> Self;
+     fn frac_pi_8() -> Self;
+     fn frac_1_pi() -> Self;
+     fn frac_2_pi() -> Self;
+     fn frac_2_sqrtpi() -> Self;
+     fn sqrt2() -> Self;
+     fn frac_1_sqrt2() -> Self;
+     fn e() -> Self;
+     fn log2_e() -> Self;
+     fn log10_e() -> Self;
+     fn ln_2() -> Self;
+     fn ln_10() -> Self;
+     // Fractional functions
+     /// Take the reciprocal (inverse) of a number, `1/x`.
+     fn recip(&self) -> Self;
+     // Algebraic functions
+     /// Raise a number to a power.
+     fn powf(&self, n: &Self) -> 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;
+     // Trigonometric functions
+     /// Computes the sine of a number (in radians).
+     fn sin(&self) -> Self;
+     /// Computes the cosine of a number (in radians).
+     fn cos(&self) -> Self;
+     /// Computes the tangent of a number (in radians).
+     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;
+     /// 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;
+     /// Computes the arctangent of a number. Return value is in radians in the
+     /// range [-pi/2, pi/2];
+     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;
+     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
+     /// `(sin(x), cos(x))`.
+     fn sin_cos(&self) -> (Self, Self);
+     // Exponential functions
+     /// 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 natural logarithm of the number.
+     fn ln(&self) -> Self;
+     /// Returns the logarithm of the number with respect to an arbitrary base.
+     fn log(&self, base: &Self) -> Self;
+     /// Returns the base 2 logarithm of the number.
+     fn log2(&self) -> Self;
+     /// Returns the base 10 logarithm of the number.
+     fn log10(&self) -> Self;
+     // Hyperbolic functions
+     /// Hyperbolic sine function.
+     fn sinh(&self) -> Self;
+     /// Hyperbolic cosine function.
+     fn cosh(&self) -> Self;
+     /// Hyperbolic tangent function.
+     fn tanh(&self) -> Self;
+     /// Inverse hyperbolic sine function.
+     fn asinh(&self) -> Self;
+     /// Inverse hyperbolic cosine function.
+     fn acosh(&self) -> Self;
+     /// Inverse hyperbolic tangent function.
+     fn atanh(&self) -> Self;
+     // Angular conversions
+     /// Convert radians to degrees.
+     fn to_degrees(&self) -> Self;
+     /// Convert degrees to radians.
+     fn to_radians(&self) -> Self;
  }
  
  /// Returns the exponential of the number, minus `1`, `exp(n) - 1`, in a way
  /// architectures) than a separate multiplication operation followed by an add.
  #[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
  
+ /// Raise a number to a power.
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// use std::num;
+ ///
+ /// let sixteen: f64 = num::powf(2.0, 4.0);
+ /// assert_eq!(sixteen, 16.0);
+ /// ```
+ #[inline(always)] pub fn powf<T: Float>(value: T, n: T) -> T { value.powf(&n) }
+ /// Take the square root of a number.
+ #[inline(always)] pub fn sqrt<T: Float>(value: T) -> T { value.sqrt() }
+ /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
+ #[inline(always)] pub fn rsqrt<T: Float>(value: T) -> T { value.rsqrt() }
+ /// Take the cubic root of a number.
+ #[inline(always)] pub fn cbrt<T: Float>(value: T) -> T { value.cbrt() }
+ /// Calculate the length of the hypotenuse of a right-angle triangle given legs
+ /// of length `x` and `y`.
+ #[inline(always)] pub fn hypot<T: Float>(x: T, y: T) -> T { x.hypot(&y) }
+ /// Sine function.
+ #[inline(always)] pub fn sin<T: Float>(value: T) -> T { value.sin() }
+ /// Cosine function.
+ #[inline(always)] pub fn cos<T: Float>(value: T) -> T { value.cos() }
+ /// Tangent function.
+ #[inline(always)] pub fn tan<T: Float>(value: T) -> T { value.tan() }
+ /// Compute the arcsine of the number.
+ #[inline(always)] pub fn asin<T: Float>(value: T) -> T { value.asin() }
+ /// Compute the arccosine of the number.
+ #[inline(always)] pub fn acos<T: Float>(value: T) -> T { value.acos() }
+ /// Compute the arctangent of the number.
+ #[inline(always)] pub fn atan<T: Float>(value: T) -> T { value.atan() }
+ /// Compute the arctangent with 2 arguments.
+ #[inline(always)] pub fn atan2<T: Float>(x: T, y: T) -> T { x.atan2(&y) }
+ /// Simultaneously computes the sine and cosine of the number.
+ #[inline(always)] pub fn sin_cos<T: Float>(value: T) -> (T, T) { value.sin_cos() }
+ /// Returns `e^(value)`, (the exponential function).
+ #[inline(always)] pub fn exp<T: Float>(value: T) -> T { value.exp() }
+ /// Returns 2 raised to the power of the number, `2^(value)`.
+ #[inline(always)] pub fn exp2<T: Float>(value: T) -> T { value.exp2() }
+ /// Returns the natural logarithm of the number.
+ #[inline(always)] pub fn ln<T: Float>(value: T) -> T { value.ln() }
+ /// Returns the logarithm of the number with respect to an arbitrary base.
+ #[inline(always)] pub fn log<T: Float>(value: T, base: T) -> T { value.log(&base) }
+ /// Returns the base 2 logarithm of the number.
+ #[inline(always)] pub fn log2<T: Float>(value: T) -> T { value.log2() }
+ /// Returns the base 10 logarithm of the number.
+ #[inline(always)] pub fn log10<T: Float>(value: T) -> T { value.log10() }
+ /// Hyperbolic sine function.
+ #[inline(always)] pub fn sinh<T: Float>(value: T) -> T { value.sinh() }
+ /// Hyperbolic cosine function.
+ #[inline(always)] pub fn cosh<T: Float>(value: T) -> T { value.cosh() }
+ /// Hyperbolic tangent function.
+ #[inline(always)] pub fn tanh<T: Float>(value: T) -> T { value.tanh() }
+ /// Inverse hyperbolic sine function.
+ #[inline(always)] pub fn asinh<T: Float>(value: T) -> T { value.asinh() }
+ /// Inverse hyperbolic cosine function.
+ #[inline(always)] pub fn acosh<T: Float>(value: T) -> T { value.acosh() }
+ /// Inverse hyperbolic tangent function.
+ #[inline(always)] pub fn atanh<T: Float>(value: T) -> T { value.atanh() }
  /// A generic trait for converting a value to a number.
  pub trait ToPrimitive {
      /// Converts the value of `self` to an `int`.
diff --combined src/libstd/prelude.rs
index bd21bb4e75444a5b5fd58ea0dfc3f8e51c1113c5,c355091cfb13e134a7db2be7fd0b90153cf3fd67..e2b627250432cc5d151ebc6bdba8bec08b889782
@@@ -58,7 -58,7 +58,7 @@@ pub use hash::Hash
  pub use iter::{FromIterator, Extendable};
  pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
  pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
- pub use num::{Integer, Real, Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
+ pub use num::{Integer, Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
  pub use num::{Signed, Unsigned, Round};
  pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
  pub use path::{GenericPath, Path, PosixPath, WindowsPath};
@@@ -67,6 -67,10 +67,6 @@@ pub use io::{Buffer, Writer, Reader, Se
  pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
  pub use to_bytes::IterBytes;
  pub use to_str::{ToStr, IntoStr};
 -pub use tuple::{CloneableTuple, ImmutableTuple};
 -pub use tuple::{ImmutableTuple1, ImmutableTuple2, ImmutableTuple3, ImmutableTuple4};
 -pub use tuple::{ImmutableTuple5, ImmutableTuple6, ImmutableTuple7, ImmutableTuple8};
 -pub use tuple::{ImmutableTuple9, ImmutableTuple10, ImmutableTuple11, ImmutableTuple12};
  pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
  pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};
  pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12};