diff options
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 4 | ||||
| -rw-r--r-- | src/libstd/collections/hash/table.rs | 2 | ||||
| -rw-r--r-- | src/libstd/env.rs | 6 | ||||
| -rw-r--r-- | src/libstd/ffi/mod.rs | 8 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 49 | ||||
| -rw-r--r-- | src/libstd/io/error.rs | 7 | ||||
| -rw-r--r-- | src/libstd/lib.rs | 2 | ||||
| -rw-r--r-- | src/libstd/net/mod.rs | 21 | ||||
| -rw-r--r-- | src/libstd/num/f32.rs | 656 | ||||
| -rw-r--r-- | src/libstd/num/f64.rs | 641 | ||||
| -rw-r--r-- | src/libstd/num/mod.rs | 1641 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 556 | ||||
| -rw-r--r-- | src/libstd/num/uint_macros.rs | 21 | ||||
| -rw-r--r-- | src/libstd/path.rs | 34 | ||||
| -rw-r--r-- | src/libstd/rand/reader.rs | 13 | ||||
| -rw-r--r-- | src/libstd/sync/condvar.rs | 8 | ||||
| -rw-r--r-- | src/libstd/sys/common/wtf8.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/unix/condvar.rs | 7 | ||||
| -rw-r--r-- | src/libstd/sys/unix/mod.rs | 24 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process2.rs | 16 | ||||
| -rw-r--r-- | src/libstd/sys/windows/fs2.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/windows/mod.rs | 8 | ||||
| -rw-r--r-- | src/libstd/sys/windows/net.rs | 15 | ||||
| -rw-r--r-- | src/libstd/sys/windows/process2.rs | 4 | ||||
| -rw-r--r-- | src/libstd/thread/mod.rs | 16 | ||||
| -rw-r--r-- | src/libstd/time/duration.rs | 9 |
26 files changed, 346 insertions, 3426 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 33e609702c4..9ba90c470f8 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1620,7 +1620,7 @@ mod test_map { use super::HashMap; use super::Entry::{Occupied, Vacant}; - use iter::{range_inclusive, range_step_inclusive, repeat}; + use iter::{range_inclusive, repeat}; use cell::RefCell; use rand::{thread_rng, Rng}; @@ -1856,7 +1856,7 @@ mod test_map { } // remove backwards - for i in range_step_inclusive(1000, 1, -1) { + for i in (1..1001).rev() { assert!(m.remove(&i).is_some()); for j in range_inclusive(i, 1000) { diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 65ebf8515e6..3a63e2ab59c 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -17,7 +17,7 @@ use iter::{Iterator, ExactSizeIterator}; use marker::{Copy, Send, Sync, Sized, self}; use mem::{min_align_of, size_of}; use mem; -use num::wrapping::{OverflowingOps, WrappingOps}; +use num::wrapping::OverflowingOps; use ops::{Deref, DerefMut, Drop}; use option::Option; use option::Option::{Some, None}; diff --git a/src/libstd/env.rs b/src/libstd/env.rs index c66be35f3f6..114d0dd79a0 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -771,7 +771,7 @@ mod tests { } fn eq(a: Option<OsString>, b: Option<&str>) { - assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::from_str).map(|s| &*s)); + assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s)); } #[test] @@ -894,7 +894,7 @@ mod tests { fn join_paths_unix() { fn test_eq(input: &[&str], output: &str) -> bool { &*join_paths(input.iter().cloned()).unwrap() == - OsStr::from_str(output) + OsStr::new(output) } assert!(test_eq(&[], "")); @@ -910,7 +910,7 @@ mod tests { fn join_paths_windows() { fn test_eq(input: &[&str], output: &str) -> bool { &*join_paths(input.iter().cloned()).unwrap() == - OsStr::from_str(output) + OsStr::new(output) } assert!(test_eq(&[], "")); diff --git a/src/libstd/ffi/mod.rs b/src/libstd/ffi/mod.rs index 99becb67a5a..dfe706e0773 100644 --- a/src/libstd/ffi/mod.rs +++ b/src/libstd/ffi/mod.rs @@ -20,11 +20,3 @@ pub use self::os_str::{OsString, OsStr}; mod c_str; mod os_str; - -// FIXME (#21670): these should be defined in the os_str module -/// Freely convertible to an `&OsStr` slice. -#[unstable(feature = "std_misc")] -pub trait AsOsStr { - /// Converts to an `&OsStr` slice. - fn as_os_str(&self) -> &OsStr; -} diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 08b41915d91..97bf33335b0 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -46,7 +46,6 @@ use vec::Vec; use sys::os_str::{Buf, Slice}; use sys_common::{AsInner, IntoInner, FromInner}; -use super::AsOsStr; /// Owned, mutable OS strings. #[derive(Clone)] @@ -226,14 +225,6 @@ impl OsStr { s.as_ref() } - /// Coerces directly from a `&str` slice to a `&OsStr` slice. - #[stable(feature = "rust1", since = "1.0.0")] - #[deprecated(since = "1.0.0", - reason = "use `OsStr::new` instead")] - pub fn from_str(s: &str) -> &OsStr { - unsafe { mem::transmute(Slice::from_str(s)) } - } - /// Yields a `&str` slice if the `OsStr` is valid unicode. /// /// This conversion may entail doing a check for UTF-8 validity. @@ -379,46 +370,6 @@ impl ToOwned for OsStr { } #[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl<'a, T: AsOsStr + ?Sized> AsOsStr for &'a T { - fn as_os_str(&self) -> &OsStr { - (*self).as_os_str() - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl AsOsStr for OsStr { - fn as_os_str(&self) -> &OsStr { - self - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl AsOsStr for OsString { - fn as_os_str(&self) -> &OsStr { - &self[..] - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl AsOsStr for str { - fn as_os_str(&self) -> &OsStr { - unsafe { mem::transmute(Slice::from_str(self)) } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl AsOsStr for String { - fn as_os_str(&self) -> &OsStr { - unsafe { mem::transmute(Slice::from_str(self)) } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl AsRef<OsStr> for OsStr { fn as_ref(&self) -> &OsStr { self diff --git a/src/libstd/io/error.rs b/src/libstd/io/error.rs index 959c15fcfd6..97c5a29d308 100644 --- a/src/libstd/io/error.rs +++ b/src/libstd/io/error.rs @@ -172,13 +172,6 @@ impl Error { Error { repr: Repr::Os(code) } } - /// Creates a new instance of an `Error` from a particular OS error code. - #[unstable(feature = "io", reason = "deprecated")] - #[deprecated(since = "1.0.0", reason = "renamed to from_raw_os_error")] - pub fn from_os_error(code: i32) -> Error { - Error { repr: Repr::Os(code) } - } - /// Returns the OS error that this error represents (if any). /// /// If this `Error` was constructed via `last_os_error` then this function diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 4452a4457ac..96665259a4d 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -128,6 +128,8 @@ #![feature(std_misc)] #![feature(slice_patterns)] #![feature(debug_builders)] +#![feature(zero_one)] +#![cfg_attr(test, feature(float_from_str_radix))] #![cfg_attr(test, feature(test, rustc_private, std_misc))] // Don't link to std. We are std. diff --git a/src/libstd/net/mod.rs b/src/libstd/net/mod.rs index a152b98822a..3bfc764e540 100644 --- a/src/libstd/net/mod.rs +++ b/src/libstd/net/mod.rs @@ -18,8 +18,6 @@ use prelude::v1::*; use io::{self, Error, ErrorKind}; -#[allow(deprecated)] // Int -use num::Int; use sys_common::net2 as net_imp; pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope}; @@ -55,10 +53,21 @@ pub enum Shutdown { Both, } -#[allow(deprecated)] // Int -fn hton<I: Int>(i: I) -> I { i.to_be() } -#[allow(deprecated)] // Int -fn ntoh<I: Int>(i: I) -> I { Int::from_be(i) } +#[doc(hidden)] +trait NetInt { + fn from_be(i: Self) -> Self; + fn to_be(&self) -> Self; +} +macro_rules! doit { + ($($t:ident)*) => ($(impl NetInt for $t { + fn from_be(i: Self) -> Self { <$t>::from_be(i) } + fn to_be(&self) -> Self { <$t>::to_be(*self) } + })*) +} +doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize } + +fn hton<I: NetInt>(i: I) -> I { i.to_be() } +fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) } fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T> where F: FnMut(&SocketAddr) -> io::Result<T> diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 736f6d2f4f4..0efc04ef83c 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -17,18 +17,14 @@ use prelude::v1::*; +use core::num; use intrinsics; use libc::c_int; -use num::{Float, FpCategory}; -use num::strconv; -use num::strconv::ExponentFormat::{ExpNone, ExpDec}; -use num::strconv::SignificantDigits::{DigAll, DigMax, DigExact}; -use num::strconv::SignFormat::SignNeg; - -use core::num; +use num::{FpCategory, ParseFloatError}; +use sys_common::FromInner; -pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE}; -pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP}; +pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON}; +pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP}; pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY}; pub use core::f32::{MIN, MIN_POSITIVE, MAX}; pub use core::f32::consts; @@ -74,294 +70,16 @@ mod cmath { } } -#[stable(feature = "rust1", since = "1.0.0")] -#[allow(deprecated)] -impl Float for f32 { - #[inline] - fn nan() -> f32 { num::Float::nan() } - #[inline] - fn infinity() -> f32 { num::Float::infinity() } - #[inline] - fn neg_infinity() -> f32 { num::Float::neg_infinity() } - #[inline] - fn zero() -> f32 { num::Float::zero() } - #[inline] - fn neg_zero() -> f32 { num::Float::neg_zero() } - #[inline] - fn one() -> f32 { num::Float::one() } - - #[allow(deprecated)] - #[inline] - fn mantissa_digits(unused_self: Option<f32>) -> usize { - num::Float::mantissa_digits(unused_self) - } - #[allow(deprecated)] - #[inline] - fn digits(unused_self: Option<f32>) -> usize { num::Float::digits(unused_self) } - #[allow(deprecated)] - #[inline] - fn epsilon() -> f32 { num::Float::epsilon() } - #[allow(deprecated)] - #[inline] - fn min_exp(unused_self: Option<f32>) -> isize { num::Float::min_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn max_exp(unused_self: Option<f32>) -> isize { num::Float::max_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn min_10_exp(unused_self: Option<f32>) -> isize { num::Float::min_10_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn max_10_exp(unused_self: Option<f32>) -> isize { num::Float::max_10_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn min_value() -> f32 { num::Float::min_value() } - #[allow(deprecated)] - #[inline] - fn min_pos_value(unused_self: Option<f32>) -> f32 { num::Float::min_pos_value(unused_self) } - #[allow(deprecated)] - #[inline] - fn max_value() -> f32 { num::Float::max_value() } - - #[inline] - fn is_nan(self) -> bool { num::Float::is_nan(self) } - #[inline] - fn is_infinite(self) -> bool { num::Float::is_infinite(self) } - #[inline] - fn is_finite(self) -> bool { num::Float::is_finite(self) } - #[inline] - fn is_normal(self) -> bool { num::Float::is_normal(self) } - #[inline] - fn classify(self) -> FpCategory { num::Float::classify(self) } - - #[inline] - fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) } - - #[inline] - fn floor(self) -> f32 { num::Float::floor(self) } - #[inline] - fn ceil(self) -> f32 { num::Float::ceil(self) } - #[inline] - fn round(self) -> f32 { num::Float::round(self) } - #[inline] - fn trunc(self) -> f32 { num::Float::trunc(self) } - #[inline] - fn fract(self) -> f32 { num::Float::fract(self) } - - #[inline] - fn abs(self) -> f32 { num::Float::abs(self) } - #[inline] - fn signum(self) -> f32 { num::Float::signum(self) } - #[inline] - fn is_positive(self) -> bool { num::Float::is_positive(self) } - #[inline] - fn is_negative(self) -> bool { num::Float::is_negative(self) } - - #[inline] - fn mul_add(self, a: f32, b: f32) -> f32 { num::Float::mul_add(self, a, b) } - #[inline] - fn recip(self) -> f32 { num::Float::recip(self) } - - #[inline] - fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) } - #[inline] - fn powf(self, n: f32) -> f32 { num::Float::powf(self, n) } - - #[inline] - fn sqrt(self) -> f32 { num::Float::sqrt(self) } - #[inline] - fn rsqrt(self) -> f32 { num::Float::rsqrt(self) } - - #[inline] - fn exp(self) -> f32 { num::Float::exp(self) } - #[inline] - fn exp2(self) -> f32 { num::Float::exp2(self) } - #[inline] - fn ln(self) -> f32 { num::Float::ln(self) } - #[inline] - fn log(self, base: f32) -> f32 { num::Float::log(self, base) } - #[inline] - fn log2(self) -> f32 { num::Float::log2(self) } - #[inline] - fn log10(self) -> f32 { num::Float::log10(self) } - #[inline] - fn to_degrees(self) -> f32 { num::Float::to_degrees(self) } - #[inline] - fn to_radians(self) -> f32 { num::Float::to_radians(self) } - - /// Constructs a floating point number by multiplying `x` by 2 raised to the - /// power of `exp` - #[inline] - fn ldexp(self, exp: isize) -> f32 { - unsafe { cmath::ldexpf(self, exp as c_int) } - } - - /// Breaks the number into a normalized fraction and a base-2 exponent, - /// satisfying: - /// - /// - `self = x * pow(2, exp)` - /// - `0.5 <= abs(x) < 1.0` - #[inline] - fn frexp(self) -> (f32, isize) { - unsafe { - let mut exp = 0; - let x = cmath::frexpf(self, &mut exp); - (x, exp as isize) - } - } - - /// 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) } - } - - #[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 abs_sub(self, other: f32) -> f32 { - unsafe { cmath::fdimf(self, other) } - } - - #[inline] - fn cbrt(self) -> f32 { - unsafe { cmath::cbrtf(self) } - } - - #[inline] - fn hypot(self, other: f32) -> f32 { - unsafe { cmath::hypotf(self, other) } - } - - #[inline] - fn sin(self) -> f32 { - unsafe { intrinsics::sinf32(self) } - } - - #[inline] - fn cos(self) -> f32 { - unsafe { intrinsics::cosf32(self) } - } - - #[inline] - fn tan(self) -> f32 { - unsafe { cmath::tanf(self) } - } - - #[inline] - fn asin(self) -> f32 { - unsafe { cmath::asinf(self) } - } - - #[inline] - fn acos(self) -> f32 { - unsafe { cmath::acosf(self) } - } - - #[inline] - fn atan(self) -> f32 { - unsafe { cmath::atanf(self) } - } - - #[inline] - fn atan2(self, other: f32) -> f32 { - unsafe { cmath::atan2f(self, other) } - } - - /// Simultaneously computes the sine and cosine of the number - #[inline] - fn sin_cos(self) -> (f32, f32) { - (self.sin(), self.cos()) - } - - /// 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) } - } - - #[inline] - fn sinh(self) -> f32 { - unsafe { cmath::sinhf(self) } - } - - #[inline] - fn cosh(self) -> f32 { - unsafe { cmath::coshf(self) } - } - - #[inline] - fn tanh(self) -> f32 { - unsafe { cmath::tanhf(self) } - } - - /// Inverse hyperbolic sine - /// - /// # Returns - /// - /// - on success, the inverse hyperbolic sine of `self` will be returned - /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY` - /// - `NAN` if `self` is `NAN` - #[inline] - fn asinh(self) -> f32 { - match self { - NEG_INFINITY => NEG_INFINITY, - x => (x + ((x * x) + 1.0).sqrt()).ln(), - } - } - - /// Inverse hyperbolic cosine - /// - /// # Returns - /// - /// - on success, the inverse hyperbolic cosine of `self` will be returned - /// - `INFINITY` if `self` is `INFINITY` - /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`) - #[inline] - fn acosh(self) -> f32 { - match self { - x if x < 1.0 => Float::nan(), - x => (x + ((x * x) - 1.0).sqrt()).ln(), - } - } - - /// Inverse hyperbolic tangent - /// - /// # Returns - /// - /// - on success, the inverse hyperbolic tangent of `self` will be returned - /// - `self` if `self` is `0.0` or `-0.0` - /// - `INFINITY` if `self` is `1.0` - /// - `NEG_INFINITY` if `self` is `-1.0` - /// - `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() - } -} - #[cfg(not(test))] #[lang = "f32"] #[stable(feature = "rust1", since = "1.0.0")] impl f32 { + /// Parses a float as with a given radix + #[unstable(feature = "float_from_str_radix", reason = "recently moved API")] + pub fn from_str_radix(s: &str, radix: u32) -> Result<f32, ParseFloatError> { + num::Float::from_str_radix(s, radix).map_err(FromInner::from_inner) + } + /// Returns `true` if this value is `NaN` and false otherwise. /// /// ``` @@ -617,11 +335,6 @@ impl f32 { #[inline] pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) } - #[stable(feature = "rust1", since = "1.0.0")] - #[deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")] - #[inline] - pub fn is_positive(self) -> bool { num::Float::is_positive(self) } - /// Returns `true` if `self`'s sign is negative, including `-0.0` /// and `NEG_INFINITY`. /// @@ -641,11 +354,6 @@ impl f32 { #[inline] pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) } - #[stable(feature = "rust1", since = "1.0.0")] - #[deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")] - #[inline] - pub fn is_negative(self) -> bool { num::Float::is_negative(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. @@ -729,24 +437,6 @@ impl f32 { #[inline] pub fn sqrt(self) -> f32 { num::Float::sqrt(self) } - /// Takes the reciprocal (inverse) square root of a number, `1/sqrt(x)`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::f32; - /// - /// let f = 4.0f32; - /// - /// let abs_difference = (f.rsqrt() - 0.5).abs(); - /// - /// assert!(abs_difference <= f32::EPSILON); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - #[deprecated(since = "1.0.0", reason = "use self.sqrt().recip() instead")] - #[inline] - pub fn rsqrt(self) -> f32 { num::Float::rsqrt(self) } - /// Returns `e^(self)`, (the exponential function). /// /// ``` @@ -1339,7 +1029,7 @@ impl f32 { #[inline] pub fn acosh(self) -> f32 { match self { - x if x < 1.0 => Float::nan(), + x if x < 1.0 => ::f32::NAN, x => (x + ((x * x) - 1.0).sqrt()).ln(), } } @@ -1363,116 +1053,9 @@ impl f32 { } } -// -// Section: String Conversions -// - -/// Converts a float to a string -/// -/// # Arguments -/// -/// * num - The float value -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -#[deprecated(since = "1.0.0", reason = "use the ToString trait instead")] -pub fn to_string(num: f32) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigAll, ExpNone, false); - r -} - -/// Converts a float to a string in hexadecimal format -/// -/// # Arguments -/// -/// * num - The float value -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -#[deprecated(since = "1.0.0", reason = "use format! instead")] -pub fn to_str_hex(num: f32) -> String { - let (r, _) = strconv::float_to_str_common( - num, 16, true, SignNeg, DigAll, ExpNone, false); - r -} - -/// Converts a float to a string in a given radix, and a flag indicating -/// whether it's a special value -/// -/// # Arguments -/// -/// * num - The float value -/// * radix - The base to use -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -#[deprecated(since = "1.0.0", reason = "use format! instead")] -pub fn to_str_radix_special(num: f32, rdx: u32) -> (String, bool) { - strconv::float_to_str_common(num, rdx, true, SignNeg, DigAll, ExpNone, false) -} - -/// Converts a float to a string with exactly the number of -/// provided significant digits -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of significant digits -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_exact(num: f32, dig: usize) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigExact(dig), ExpNone, false); - r -} - -/// Converts a float to a string with a maximum number of -/// significant digits -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of significant digits -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_digits(num: f32, dig: usize) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigMax(dig), ExpNone, false); - r -} - -/// Converts a float to a string using the exponential notation with exactly the number of -/// provided digits after the decimal point in the significand -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of digits after the decimal point -/// * upper - Use `E` instead of `e` for the exponent sign -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_exp_exact(num: f32, dig: usize, upper: bool) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigExact(dig), ExpDec, upper); - r -} - -/// Converts a float to a string using the exponential notation with the maximum number of -/// digits after the decimal point in the significand -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of digits after the decimal point -/// * upper - Use `E` instead of `e` for the exponent sign -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_exp_digits(num: f32, dig: usize, upper: bool) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigMax(dig), ExpDec, upper); - r -} - #[cfg(test)] mod tests { + use f32; use f32::*; use num::*; use num::FpCategory as Fp; @@ -1496,7 +1079,7 @@ mod tests { #[test] fn test_nan() { - let nan: f32 = Float::nan(); + let nan: f32 = f32::NAN; assert!(nan.is_nan()); assert!(!nan.is_infinite()); assert!(!nan.is_finite()); @@ -1508,7 +1091,7 @@ mod tests { #[test] fn test_infinity() { - let inf: f32 = Float::infinity(); + let inf: f32 = f32::INFINITY; assert!(inf.is_infinite()); assert!(!inf.is_finite()); assert!(inf.is_sign_positive()); @@ -1520,7 +1103,7 @@ mod tests { #[test] fn test_neg_infinity() { - let neg_inf: f32 = Float::neg_infinity(); + let neg_inf: f32 = f32::NEG_INFINITY; assert!(neg_inf.is_infinite()); assert!(!neg_inf.is_finite()); assert!(!neg_inf.is_sign_positive()); @@ -1532,7 +1115,7 @@ mod tests { #[test] fn test_zero() { - let zero: f32 = Float::zero(); + let zero: f32 = 0.0f32; assert_eq!(0.0, zero); assert!(!zero.is_infinite()); assert!(zero.is_finite()); @@ -1545,7 +1128,7 @@ mod tests { #[test] fn test_neg_zero() { - let neg_zero: f32 = Float::neg_zero(); + let neg_zero: f32 = -0.0; assert_eq!(0.0, neg_zero); assert!(!neg_zero.is_infinite()); assert!(neg_zero.is_finite()); @@ -1558,7 +1141,7 @@ mod tests { #[test] fn test_one() { - let one: f32 = Float::one(); + let one: f32 = 1.0f32; assert_eq!(1.0, one); assert!(!one.is_infinite()); assert!(one.is_finite()); @@ -1571,9 +1154,9 @@ mod tests { #[test] fn test_is_nan() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert!(nan.is_nan()); assert!(!0.0f32.is_nan()); assert!(!5.3f32.is_nan()); @@ -1584,9 +1167,9 @@ mod tests { #[test] fn test_is_infinite() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert!(!nan.is_infinite()); assert!(inf.is_infinite()); assert!(neg_inf.is_infinite()); @@ -1597,9 +1180,9 @@ mod tests { #[test] fn test_is_finite() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert!(!nan.is_finite()); assert!(!inf.is_finite()); assert!(!neg_inf.is_finite()); @@ -1610,11 +1193,11 @@ mod tests { #[test] fn test_is_normal() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let zero: f32 = Float::zero(); - let neg_zero: f32 = Float::neg_zero(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let zero: f32 = 0.0f32; + let neg_zero: f32 = -0.0; assert!(!nan.is_normal()); assert!(!inf.is_normal()); assert!(!neg_inf.is_normal()); @@ -1627,11 +1210,11 @@ mod tests { #[test] fn test_classify() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let zero: f32 = Float::zero(); - let neg_zero: f32 = Float::neg_zero(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let zero: f32 = 0.0f32; + let neg_zero: f32 = -0.0; assert_eq!(nan.classify(), Fp::Nan); assert_eq!(inf.classify(), Fp::Infinite); assert_eq!(neg_inf.classify(), Fp::Infinite); @@ -1774,9 +1357,9 @@ mod tests { #[test] fn test_mul_add() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05); assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65); assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2); @@ -1790,9 +1373,9 @@ mod tests { #[test] fn test_recip() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(1.0f32.recip(), 1.0); assert_eq!(2.0f32.recip(), 0.5); assert_eq!((-0.4f32).recip(), -2.5); @@ -1804,9 +1387,9 @@ mod tests { #[test] fn test_powi() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(1.0f32.powi(1), 1.0); assert_approx_eq!((-3.1f32).powi(2), 9.61); assert_approx_eq!(5.9f32.powi(-2), 0.028727); @@ -1818,9 +1401,9 @@ mod tests { #[test] fn test_powf() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(1.0f32.powf(1.0), 1.0); assert_approx_eq!(3.4f32.powf(4.5), 246.408218); assert_approx_eq!(2.7f32.powf(-3.2), 0.041652); @@ -1844,29 +1427,14 @@ mod tests { } #[test] - fn test_rsqrt() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - assert!(nan.rsqrt().is_nan()); - assert_eq!(inf.rsqrt(), 0.0); - assert!(neg_inf.rsqrt().is_nan()); - assert!((-1.0f32).rsqrt().is_nan()); - assert_eq!((-0.0f32).rsqrt(), neg_inf); - assert_eq!(0.0f32.rsqrt(), inf); - assert_eq!(1.0f32.rsqrt(), 1.0); - assert_eq!(4.0f32.rsqrt(), 0.5); - } - - #[test] fn test_exp() { assert_eq!(1.0, 0.0f32.exp()); assert_approx_eq!(2.718282, 1.0f32.exp()); assert_approx_eq!(148.413162, 5.0f32.exp()); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let nan: f32 = Float::nan(); + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let nan: f32 = f32::NAN; assert_eq!(inf, inf.exp()); assert_eq!(0.0, neg_inf.exp()); assert!(nan.exp().is_nan()); @@ -1877,9 +1445,9 @@ mod tests { assert_eq!(32.0, 5.0f32.exp2()); assert_eq!(1.0, 0.0f32.exp2()); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let nan: f32 = Float::nan(); + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let nan: f32 = f32::NAN; assert_eq!(inf, inf.exp2()); assert_eq!(0.0, neg_inf.exp2()); assert!(nan.exp2().is_nan()); @@ -1887,9 +1455,9 @@ mod tests { #[test] fn test_ln() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_approx_eq!(1.0f32.exp().ln(), 1.0); assert!(nan.ln().is_nan()); assert_eq!(inf.ln(), inf); @@ -1902,12 +1470,12 @@ mod tests { #[test] fn test_log() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(10.0f32.log(10.0), 1.0); assert_approx_eq!(2.3f32.log(3.5), 0.664858); - assert_eq!(1.0f32.exp().log(1.0.exp()), 1.0); + assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0); assert!(1.0f32.log(1.0).is_nan()); assert!(1.0f32.log(-13.9).is_nan()); assert!(nan.log(2.3).is_nan()); @@ -1920,9 +1488,9 @@ mod tests { #[test] fn test_log2() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_approx_eq!(10.0f32.log2(), 3.321928); assert_approx_eq!(2.3f32.log2(), 1.201634); assert_approx_eq!(1.0f32.exp().log2(), 1.442695); @@ -1936,9 +1504,9 @@ mod tests { #[test] fn test_log10() { - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(10.0f32.log10(), 1.0); assert_approx_eq!(2.3f32.log10(), 0.361728); assert_approx_eq!(1.0f32.exp().log10(), 0.434294); @@ -1954,9 +1522,9 @@ mod tests { #[test] fn test_to_degrees() { let pi: f32 = consts::PI; - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(0.0f32.to_degrees(), 0.0); assert_approx_eq!((-5.8f32).to_degrees(), -332.315521); assert_eq!(pi.to_degrees(), 180.0); @@ -1968,9 +1536,9 @@ mod tests { #[test] fn test_to_radians() { let pi: f32 = consts::PI; - let nan: f32 = Float::nan(); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); + let nan: f32 = f32::NAN; + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; assert_eq!(0.0f32.to_radians(), 0.0); assert_approx_eq!(154.6f32.to_radians(), 2.698279); assert_approx_eq!((-332.31f32).to_radians(), -5.799903); @@ -1984,40 +1552,40 @@ mod tests { fn test_ldexp() { // We have to use from_str until base-2 exponents // are supported in floating-point literals - let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap(); - let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap(); - let f3: f32 = FromStrRadix::from_str_radix("1.Cp-12", 16).unwrap(); - assert_eq!(1f32.ldexp(-123), f1); - assert_eq!(1f32.ldexp(-111), f2); - assert_eq!(Float::ldexp(1.75f32, -12), f3); + let f1: f32 = f32::from_str_radix("1p-123", 16).unwrap(); + let f2: f32 = f32::from_str_radix("1p-111", 16).unwrap(); + let f3: f32 = f32::from_str_radix("1.Cp-12", 16).unwrap(); + assert_eq!(f32::ldexp(1f32, -123), f1); + assert_eq!(f32::ldexp(1f32, -111), f2); + assert_eq!(f32::ldexp(1.75f32, -12), f3); - assert_eq!(Float::ldexp(0f32, -123), 0f32); - assert_eq!(Float::ldexp(-0f32, -123), -0f32); + assert_eq!(f32::ldexp(0f32, -123), 0f32); + assert_eq!(f32::ldexp(-0f32, -123), -0f32); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let nan: f32 = Float::nan(); - assert_eq!(Float::ldexp(inf, -123), inf); - assert_eq!(Float::ldexp(neg_inf, -123), neg_inf); - assert!(Float::ldexp(nan, -123).is_nan()); + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let nan: f32 = f32::NAN; + assert_eq!(f32::ldexp(inf, -123), inf); + assert_eq!(f32::ldexp(neg_inf, -123), neg_inf); + assert!(f32::ldexp(nan, -123).is_nan()); } #[test] fn test_frexp() { // We have to use from_str until base-2 exponents // are supported in floating-point literals - let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap(); - let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap(); - let f3: f32 = FromStrRadix::from_str_radix("1.Cp-123", 16).unwrap(); + let f1: f32 = f32::from_str_radix("1p-123", 16).unwrap(); + let f2: f32 = f32::from_str_radix("1p-111", 16).unwrap(); + let f3: f32 = f32::from_str_radix("1.Cp-123", 16).unwrap(); let (x1, exp1) = f1.frexp(); let (x2, exp2) = f2.frexp(); let (x3, exp3) = f3.frexp(); assert_eq!((x1, exp1), (0.5f32, -122)); assert_eq!((x2, exp2), (0.5f32, -110)); assert_eq!((x3, exp3), (0.875f32, -122)); - assert_eq!(Float::ldexp(x1, exp1), f1); - assert_eq!(Float::ldexp(x2, exp2), f2); - assert_eq!(Float::ldexp(x3, exp3), f3); + assert_eq!(f32::ldexp(x1, exp1), f1); + assert_eq!(f32::ldexp(x2, exp2), f2); + assert_eq!(f32::ldexp(x3, exp3), f3); assert_eq!(0f32.frexp(), (0f32, 0)); assert_eq!((-0f32).frexp(), (-0f32, 0)); @@ -2025,9 +1593,9 @@ mod tests { #[test] #[cfg_attr(windows, ignore)] // FIXME #8755 fn test_frexp_nowin() { - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let nan: f32 = Float::nan(); + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let nan: f32 = f32::NAN; assert_eq!(match inf.frexp() { (x, _) => x }, inf); assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf); assert!(match nan.frexp() { (x, _) => x.is_nan() }) @@ -2056,9 +1624,9 @@ mod tests { assert_eq!(0.0f32.asinh(), 0.0f32); assert_eq!((-0.0f32).asinh(), -0.0f32); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let nan: f32 = Float::nan(); + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let nan: f32 = f32::NAN; assert_eq!(inf.asinh(), inf); assert_eq!(neg_inf.asinh(), neg_inf); assert!(nan.asinh().is_nan()); @@ -2071,9 +1639,9 @@ mod tests { assert_eq!(1.0f32.acosh(), 0.0f32); assert!(0.999f32.acosh().is_nan()); - let inf: f32 = Float::infinity(); - let neg_inf: f32 = Float::neg_infinity(); - let nan: f32 = Float::nan(); + let inf: f32 = f32::INFINITY; + let neg_inf: f32 = f32::NEG_INFINITY; + let nan: f32 = f32::NAN; assert_eq!(inf.acosh(), inf); assert!(neg_inf.acosh().is_nan()); assert!(nan.acosh().is_nan()); @@ -2086,17 +1654,17 @@ mod tests { assert_eq!(0.0f32.atanh(), 0.0f32); assert_eq!((-0.0f32).atanh(), -0.0f32); - let inf32: f32 = Float::infinity(); - let neg_inf32: f32 = Float::neg_infinity(); + let inf32: f32 = f32::INFINITY; + let neg_inf32: f32 = f32::NEG_INFINITY; assert_eq!(1.0f32.atanh(), inf32); assert_eq!((-1.0f32).atanh(), neg_inf32); assert!(2f64.atanh().atanh().is_nan()); assert!((-2f64).atanh().atanh().is_nan()); - let inf64: f32 = Float::infinity(); - let neg_inf64: f32 = Float::neg_infinity(); - let nan32: f32 = Float::nan(); + let inf64: f32 = f32::INFINITY; + let neg_inf64: f32 = f32::NEG_INFINITY; + let nan32: f32 = f32::NAN; assert!(inf64.atanh().is_nan()); assert!(neg_inf64.atanh().is_nan()); assert!(nan32.atanh().is_nan()); @@ -2118,9 +1686,9 @@ mod tests { let frac_pi_8: f32 = consts::FRAC_PI_8; let frac_1_pi: f32 = consts::FRAC_1_PI; let frac_2_pi: f32 = consts::FRAC_2_PI; - let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRTPI; - let sqrt2: f32 = consts::SQRT2; - let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT2; + let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI; + let sqrt2: f32 = consts::SQRT_2; + let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2; let e: f32 = consts::E; let log2_e: f32 = consts::LOG2_E; let log10_e: f32 = consts::LOG10_E; diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index bb9067eca13..e1497f3958d 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -16,18 +16,14 @@ use prelude::v1::*; +use core::num; use intrinsics; use libc::c_int; -use num::{Float, FpCategory}; -use num::strconv; -use num::strconv::ExponentFormat::{ExpNone, ExpDec}; -use num::strconv::SignificantDigits::{DigAll, DigMax, DigExact}; -use num::strconv::SignFormat::SignNeg; - -use core::num; +use num::{FpCategory, ParseFloatError}; +use sys_common::FromInner; -pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE}; -pub use core::f64::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP}; +pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON}; +pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP}; pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY}; pub use core::f64::{MIN, MIN_POSITIVE, MAX}; pub use core::f64::consts; @@ -82,295 +78,16 @@ mod cmath { } } -#[stable(feature = "rust1", since = "1.0.0")] -#[allow(deprecated)] -impl Float for f64 { - // inlined methods from `num::Float` - #[inline] - fn nan() -> f64 { num::Float::nan() } - #[inline] - fn infinity() -> f64 { num::Float::infinity() } - #[inline] - fn neg_infinity() -> f64 { num::Float::neg_infinity() } - #[inline] - fn zero() -> f64 { num::Float::zero() } - #[inline] - fn neg_zero() -> f64 { num::Float::neg_zero() } - #[inline] - fn one() -> f64 { num::Float::one() } - - - #[allow(deprecated)] - #[inline] - fn mantissa_digits(unused_self: Option<f64>) -> usize { - num::Float::mantissa_digits(unused_self) - } - #[allow(deprecated)] - #[inline] - fn digits(unused_self: Option<f64>) -> usize { num::Float::digits(unused_self) } - #[allow(deprecated)] - #[inline] - fn epsilon() -> f64 { num::Float::epsilon() } - #[allow(deprecated)] - #[inline] - fn min_exp(unused_self: Option<f64>) -> isize { num::Float::min_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn max_exp(unused_self: Option<f64>) -> isize { num::Float::max_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn min_10_exp(unused_self: Option<f64>) -> isize { num::Float::min_10_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn max_10_exp(unused_self: Option<f64>) -> isize { num::Float::max_10_exp(unused_self) } - #[allow(deprecated)] - #[inline] - fn min_value() -> f64 { num::Float::min_value() } - #[allow(deprecated)] - #[inline] - fn min_pos_value(unused_self: Option<f64>) -> f64 { num::Float::min_pos_value(unused_self) } - #[allow(deprecated)] - #[inline] - fn max_value() -> f64 { num::Float::max_value() } - - #[inline] - fn is_nan(self) -> bool { num::Float::is_nan(self) } - #[inline] - fn is_infinite(self) -> bool { num::Float::is_infinite(self) } - #[inline] - fn is_finite(self) -> bool { num::Float::is_finite(self) } - #[inline] - fn is_normal(self) -> bool { num::Float::is_normal(self) } - #[inline] - fn classify(self) -> FpCategory { num::Float::classify(self) } - - #[inline] - fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) } - - #[inline] - fn floor(self) -> f64 { num::Float::floor(self) } - #[inline] - fn ceil(self) -> f64 { num::Float::ceil(self) } - #[inline] - fn round(self) -> f64 { num::Float::round(self) } - #[inline] - fn trunc(self) -> f64 { num::Float::trunc(self) } - #[inline] - fn fract(self) -> f64 { num::Float::fract(self) } - - #[inline] - fn abs(self) -> f64 { num::Float::abs(self) } - #[inline] - fn signum(self) -> f64 { num::Float::signum(self) } - #[inline] - fn is_positive(self) -> bool { num::Float::is_positive(self) } - #[inline] - fn is_negative(self) -> bool { num::Float::is_negative(self) } - - #[inline] - fn mul_add(self, a: f64, b: f64) -> f64 { num::Float::mul_add(self, a, b) } - #[inline] - fn recip(self) -> f64 { num::Float::recip(self) } - - #[inline] - fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) } - #[inline] - fn powf(self, n: f64) -> f64 { num::Float::powf(self, n) } - - #[inline] - fn sqrt(self) -> f64 { num::Float::sqrt(self) } - #[inline] - fn rsqrt(self) -> f64 { num::Float::rsqrt(self) } - - #[inline] - fn exp(self) -> f64 { num::Float::exp(self) } - #[inline] - fn exp2(self) -> f64 { num::Float::exp2(self) } - #[inline] - fn ln(self) -> f64 { num::Float::ln(self) } - #[inline] - fn log(self, base: f64) -> f64 { num::Float::log(self, base) } - #[inline] - fn log2(self) -> f64 { num::Float::log2(self) } - #[inline] - fn log10(self) -> f64 { num::Float::log10(self) } - - #[inline] - fn to_degrees(self) -> f64 { num::Float::to_degrees(self) } - #[inline] - fn to_radians(self) -> f64 { num::Float::to_radians(self) } - - #[inline] - fn ldexp(self, exp: isize) -> f64 { - unsafe { cmath::ldexp(self, exp as c_int) } - } - - /// Breaks the number into a normalized fraction and a base-2 exponent, - /// satisfying: - /// - /// - `self = x * pow(2, exp)` - /// - `0.5 <= abs(x) < 1.0` - #[inline] - fn frexp(self) -> (f64, isize) { - unsafe { - let mut exp = 0; - let x = cmath::frexp(self, &mut exp); - (x, exp as isize) - } - } - - /// 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) } - } - - #[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 abs_sub(self, other: f64) -> f64 { - unsafe { cmath::fdim(self, other) } - } - - #[inline] - fn cbrt(self) -> f64 { - unsafe { cmath::cbrt(self) } - } - - #[inline] - fn hypot(self, other: f64) -> f64 { - unsafe { cmath::hypot(self, other) } - } - - #[inline] - fn sin(self) -> f64 { - unsafe { intrinsics::sinf64(self) } - } - - #[inline] - fn cos(self) -> f64 { - unsafe { intrinsics::cosf64(self) } - } - - #[inline] - fn tan(self) -> f64 { - unsafe { cmath::tan(self) } - } - - #[inline] - fn asin(self) -> f64 { - unsafe { cmath::asin(self) } - } - - #[inline] - fn acos(self) -> f64 { - unsafe { cmath::acos(self) } - } - - #[inline] - fn atan(self) -> f64 { - unsafe { cmath::atan(self) } - } - - #[inline] - 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) { - (self.sin(), self.cos()) - } - - /// 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) } - } - - #[inline] - fn sinh(self) -> f64 { - unsafe { cmath::sinh(self) } - } - - #[inline] - fn cosh(self) -> f64 { - unsafe { cmath::cosh(self) } - } - - #[inline] - fn tanh(self) -> f64 { - unsafe { cmath::tanh(self) } - } - - /// Inverse hyperbolic sine - /// - /// # Returns - /// - /// - on success, the inverse hyperbolic sine of `self` will be returned - /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY` - /// - `NAN` if `self` is `NAN` - #[inline] - fn asinh(self) -> f64 { - match self { - NEG_INFINITY => NEG_INFINITY, - x => (x + ((x * x) + 1.0).sqrt()).ln(), - } - } - - /// Inverse hyperbolic cosine - /// - /// # Returns - /// - /// - on success, the inverse hyperbolic cosine of `self` will be returned - /// - `INFINITY` if `self` is `INFINITY` - /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`) - #[inline] - fn acosh(self) -> f64 { - match self { - x if x < 1.0 => Float::nan(), - x => (x + ((x * x) - 1.0).sqrt()).ln(), - } - } - - /// Inverse hyperbolic tangent - /// - /// # Returns - /// - /// - on success, the inverse hyperbolic tangent of `self` will be returned - /// - `self` if `self` is `0.0` or `-0.0` - /// - `INFINITY` if `self` is `1.0` - /// - `NEG_INFINITY` if `self` is `-1.0` - /// - `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() - } -} - #[cfg(not(test))] #[lang = "f64"] #[stable(feature = "rust1", since = "1.0.0")] impl f64 { + /// Parses a float as with a given radix + #[unstable(feature = "float_from_str_radix", reason = "recently moved API")] + pub fn from_str_radix(s: &str, radix: u32) -> Result<f64, ParseFloatError> { + num::Float::from_str_radix(s, radix).map_err(FromInner::from_inner) + } + /// Returns `true` if this value is `NaN` and false otherwise. /// /// ``` @@ -726,22 +443,6 @@ impl f64 { #[inline] pub fn sqrt(self) -> f64 { num::Float::sqrt(self) } - /// Takes the reciprocal (inverse) square root of a number, `1/sqrt(x)`. - /// - /// ``` - /// # #![feature(std_misc)] - /// let f = 4.0_f64; - /// - /// let abs_difference = (f.rsqrt() - 0.5).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - #[deprecated(since = "1.0.0", reason = "use self.sqrt().recip() instead")] - #[inline] - pub fn rsqrt(self) -> f64 { num::Float::rsqrt(self) } - /// Returns `e^(self)`, (the exponential function). /// /// ``` @@ -1304,7 +1005,7 @@ impl f64 { #[inline] pub fn acosh(self) -> f64 { match self { - x if x < 1.0 => Float::nan(), + x if x < 1.0 => NAN, x => (x + ((x * x) - 1.0).sqrt()).ln(), } } @@ -1328,116 +1029,9 @@ impl f64 { } } -// -// Section: String Conversions -// - -/// Converts a float to a string -/// -/// # Arguments -/// -/// * num - The float value -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -#[deprecated(since = "1.0.0", reason = "use the ToString trait instead")] -pub fn to_string(num: f64) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigAll, ExpNone, false); - r -} - -/// Converts a float to a string in hexadecimal format -/// -/// # Arguments -/// -/// * num - The float value -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -#[deprecated(since = "1.0.0", reason = "use format! instead")] -pub fn to_str_hex(num: f64) -> String { - let (r, _) = strconv::float_to_str_common( - num, 16, true, SignNeg, DigAll, ExpNone, false); - r -} - -/// Converts a float to a string in a given radix, and a flag indicating -/// whether it's a special value -/// -/// # Arguments -/// -/// * num - The float value -/// * radix - The base to use -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -#[deprecated(since = "1.0.0", reason = "use format! instead")] -pub fn to_str_radix_special(num: f64, rdx: u32) -> (String, bool) { - strconv::float_to_str_common(num, rdx, true, SignNeg, DigAll, ExpNone, false) -} - -/// Converts a float to a string with exactly the number of -/// provided significant digits -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of significant digits -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_exact(num: f64, dig: usize) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigExact(dig), ExpNone, false); - r -} - -/// Converts a float to a string with a maximum number of -/// significant digits -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of significant digits -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_digits(num: f64, dig: usize) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigMax(dig), ExpNone, false); - r -} - -/// Converts a float to a string using the exponential notation with exactly the number of -/// provided digits after the decimal point in the significand -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of digits after the decimal point -/// * upper - Use `E` instead of `e` for the exponent sign -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_exp_exact(num: f64, dig: usize, upper: bool) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigExact(dig), ExpDec, upper); - r -} - -/// Converts a float to a string using the exponential notation with the maximum number of -/// digits after the decimal point in the significand -/// -/// # Arguments -/// -/// * num - The float value -/// * digits - The number of digits after the decimal point -/// * upper - Use `E` instead of `e` for the exponent sign -#[inline] -#[unstable(feature = "std_misc", reason = "may be removed or relocated")] -pub fn to_str_exp_digits(num: f64, dig: usize, upper: bool) -> String { - let (r, _) = strconv::float_to_str_common( - num, 10, true, SignNeg, DigMax(dig), ExpDec, upper); - r -} - #[cfg(test)] mod tests { + use f64; use f64::*; use num::*; use num::FpCategory as Fp; @@ -1461,7 +1055,7 @@ mod tests { #[test] fn test_nan() { - let nan: f64 = Float::nan(); + let nan: f64 = NAN; assert!(nan.is_nan()); assert!(!nan.is_infinite()); assert!(!nan.is_finite()); @@ -1473,7 +1067,7 @@ mod tests { #[test] fn test_infinity() { - let inf: f64 = Float::infinity(); + let inf: f64 = INFINITY; assert!(inf.is_infinite()); assert!(!inf.is_finite()); assert!(inf.is_sign_positive()); @@ -1485,7 +1079,7 @@ mod tests { #[test] fn test_neg_infinity() { - let neg_inf: f64 = Float::neg_infinity(); + let neg_inf: f64 = NEG_INFINITY; assert!(neg_inf.is_infinite()); assert!(!neg_inf.is_finite()); assert!(!neg_inf.is_sign_positive()); @@ -1497,7 +1091,7 @@ mod tests { #[test] fn test_zero() { - let zero: f64 = Float::zero(); + let zero: f64 = 0.0f64; assert_eq!(0.0, zero); assert!(!zero.is_infinite()); assert!(zero.is_finite()); @@ -1510,7 +1104,7 @@ mod tests { #[test] fn test_neg_zero() { - let neg_zero: f64 = Float::neg_zero(); + let neg_zero: f64 = -0.0; assert_eq!(0.0, neg_zero); assert!(!neg_zero.is_infinite()); assert!(neg_zero.is_finite()); @@ -1523,7 +1117,7 @@ mod tests { #[test] fn test_one() { - let one: f64 = Float::one(); + let one: f64 = 1.0f64; assert_eq!(1.0, one); assert!(!one.is_infinite()); assert!(one.is_finite()); @@ -1536,9 +1130,9 @@ mod tests { #[test] fn test_is_nan() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert!(nan.is_nan()); assert!(!0.0f64.is_nan()); assert!(!5.3f64.is_nan()); @@ -1549,9 +1143,9 @@ mod tests { #[test] fn test_is_infinite() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert!(!nan.is_infinite()); assert!(inf.is_infinite()); assert!(neg_inf.is_infinite()); @@ -1562,9 +1156,9 @@ mod tests { #[test] fn test_is_finite() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert!(!nan.is_finite()); assert!(!inf.is_finite()); assert!(!neg_inf.is_finite()); @@ -1575,11 +1169,11 @@ mod tests { #[test] fn test_is_normal() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let zero: f64 = Float::zero(); - let neg_zero: f64 = Float::neg_zero(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let zero: f64 = 0.0f64; + let neg_zero: f64 = -0.0; assert!(!nan.is_normal()); assert!(!inf.is_normal()); assert!(!neg_inf.is_normal()); @@ -1592,11 +1186,11 @@ mod tests { #[test] fn test_classify() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let zero: f64 = Float::zero(); - let neg_zero: f64 = Float::neg_zero(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let zero: f64 = 0.0f64; + let neg_zero: f64 = -0.0; assert_eq!(nan.classify(), Fp::Nan); assert_eq!(inf.classify(), Fp::Infinite); assert_eq!(neg_inf.classify(), Fp::Infinite); @@ -1738,9 +1332,9 @@ mod tests { #[test] fn test_mul_add() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05); assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65); assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2); @@ -1754,9 +1348,9 @@ mod tests { #[test] fn test_recip() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(1.0f64.recip(), 1.0); assert_eq!(2.0f64.recip(), 0.5); assert_eq!((-0.4f64).recip(), -2.5); @@ -1768,9 +1362,9 @@ mod tests { #[test] fn test_powi() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(1.0f64.powi(1), 1.0); assert_approx_eq!((-3.1f64).powi(2), 9.61); assert_approx_eq!(5.9f64.powi(-2), 0.028727); @@ -1782,9 +1376,9 @@ mod tests { #[test] fn test_powf() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(1.0f64.powf(1.0), 1.0); assert_approx_eq!(3.4f64.powf(4.5), 246.408183); assert_approx_eq!(2.7f64.powf(-3.2), 0.041652); @@ -1808,29 +1402,14 @@ mod tests { } #[test] - fn test_rsqrt() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - assert!(nan.rsqrt().is_nan()); - assert_eq!(inf.rsqrt(), 0.0); - assert!(neg_inf.rsqrt().is_nan()); - assert!((-1.0f64).rsqrt().is_nan()); - assert_eq!((-0.0f64).rsqrt(), neg_inf); - assert_eq!(0.0f64.rsqrt(), inf); - assert_eq!(1.0f64.rsqrt(), 1.0); - assert_eq!(4.0f64.rsqrt(), 0.5); - } - - #[test] fn test_exp() { assert_eq!(1.0, 0.0f64.exp()); assert_approx_eq!(2.718282, 1.0f64.exp()); assert_approx_eq!(148.413159, 5.0f64.exp()); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; assert_eq!(inf, inf.exp()); assert_eq!(0.0, neg_inf.exp()); assert!(nan.exp().is_nan()); @@ -1841,9 +1420,9 @@ mod tests { assert_eq!(32.0, 5.0f64.exp2()); assert_eq!(1.0, 0.0f64.exp2()); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; assert_eq!(inf, inf.exp2()); assert_eq!(0.0, neg_inf.exp2()); assert!(nan.exp2().is_nan()); @@ -1851,9 +1430,9 @@ mod tests { #[test] fn test_ln() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_approx_eq!(1.0f64.exp().ln(), 1.0); assert!(nan.ln().is_nan()); assert_eq!(inf.ln(), inf); @@ -1866,12 +1445,12 @@ mod tests { #[test] fn test_log() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(10.0f64.log(10.0), 1.0); assert_approx_eq!(2.3f64.log(3.5), 0.664858); - assert_eq!(1.0f64.exp().log(1.0.exp()), 1.0); + assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0); assert!(1.0f64.log(1.0).is_nan()); assert!(1.0f64.log(-13.9).is_nan()); assert!(nan.log(2.3).is_nan()); @@ -1884,9 +1463,9 @@ mod tests { #[test] fn test_log2() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_approx_eq!(10.0f64.log2(), 3.321928); assert_approx_eq!(2.3f64.log2(), 1.201634); assert_approx_eq!(1.0f64.exp().log2(), 1.442695); @@ -1900,9 +1479,9 @@ mod tests { #[test] fn test_log10() { - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(10.0f64.log10(), 1.0); assert_approx_eq!(2.3f64.log10(), 0.361728); assert_approx_eq!(1.0f64.exp().log10(), 0.434294); @@ -1918,9 +1497,9 @@ mod tests { #[test] fn test_to_degrees() { let pi: f64 = consts::PI; - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(0.0f64.to_degrees(), 0.0); assert_approx_eq!((-5.8f64).to_degrees(), -332.315521); assert_eq!(pi.to_degrees(), 180.0); @@ -1932,9 +1511,9 @@ mod tests { #[test] fn test_to_radians() { let pi: f64 = consts::PI; - let nan: f64 = Float::nan(); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); + let nan: f64 = NAN; + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; assert_eq!(0.0f64.to_radians(), 0.0); assert_approx_eq!(154.6f64.to_radians(), 2.698279); assert_approx_eq!((-332.31f64).to_radians(), -5.799903); @@ -1948,40 +1527,40 @@ mod tests { fn test_ldexp() { // We have to use from_str until base-2 exponents // are supported in floating-point literals - let f1: f64 = FromStrRadix::from_str_radix("1p-123", 16).unwrap(); - let f2: f64 = FromStrRadix::from_str_radix("1p-111", 16).unwrap(); - let f3: f64 = FromStrRadix::from_str_radix("1.Cp-12", 16).unwrap(); - assert_eq!(1f64.ldexp(-123), f1); - assert_eq!(1f64.ldexp(-111), f2); - assert_eq!(Float::ldexp(1.75f64, -12), f3); + let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap(); + let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap(); + let f3: f64 = f64::from_str_radix("1.Cp-12", 16).unwrap(); + assert_eq!(f64::ldexp(1f64, -123), f1); + assert_eq!(f64::ldexp(1f64, -111), f2); + assert_eq!(f64::ldexp(1.75f64, -12), f3); - assert_eq!(Float::ldexp(0f64, -123), 0f64); - assert_eq!(Float::ldexp(-0f64, -123), -0f64); + assert_eq!(f64::ldexp(0f64, -123), 0f64); + assert_eq!(f64::ldexp(-0f64, -123), -0f64); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); - assert_eq!(Float::ldexp(inf, -123), inf); - assert_eq!(Float::ldexp(neg_inf, -123), neg_inf); - assert!(Float::ldexp(nan, -123).is_nan()); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; + assert_eq!(f64::ldexp(inf, -123), inf); + assert_eq!(f64::ldexp(neg_inf, -123), neg_inf); + assert!(f64::ldexp(nan, -123).is_nan()); } #[test] fn test_frexp() { // We have to use from_str until base-2 exponents // are supported in floating-point literals - let f1: f64 = FromStrRadix::from_str_radix("1p-123", 16).unwrap(); - let f2: f64 = FromStrRadix::from_str_radix("1p-111", 16).unwrap(); - let f3: f64 = FromStrRadix::from_str_radix("1.Cp-123", 16).unwrap(); + let f1: f64 = f64::from_str_radix("1p-123", 16).unwrap(); + let f2: f64 = f64::from_str_radix("1p-111", 16).unwrap(); + let f3: f64 = f64::from_str_radix("1.Cp-123", 16).unwrap(); let (x1, exp1) = f1.frexp(); let (x2, exp2) = f2.frexp(); let (x3, exp3) = f3.frexp(); assert_eq!((x1, exp1), (0.5f64, -122)); assert_eq!((x2, exp2), (0.5f64, -110)); assert_eq!((x3, exp3), (0.875f64, -122)); - assert_eq!(Float::ldexp(x1, exp1), f1); - assert_eq!(Float::ldexp(x2, exp2), f2); - assert_eq!(Float::ldexp(x3, exp3), f3); + assert_eq!(f64::ldexp(x1, exp1), f1); + assert_eq!(f64::ldexp(x2, exp2), f2); + assert_eq!(f64::ldexp(x3, exp3), f3); assert_eq!(0f64.frexp(), (0f64, 0)); assert_eq!((-0f64).frexp(), (-0f64, 0)); @@ -1989,9 +1568,9 @@ mod tests { #[test] #[cfg_attr(windows, ignore)] // FIXME #8755 fn test_frexp_nowin() { - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; assert_eq!(match inf.frexp() { (x, _) => x }, inf); assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf); assert!(match nan.frexp() { (x, _) => x.is_nan() }) @@ -2020,9 +1599,9 @@ mod tests { assert_eq!(0.0f64.asinh(), 0.0f64); assert_eq!((-0.0f64).asinh(), -0.0f64); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; assert_eq!(inf.asinh(), inf); assert_eq!(neg_inf.asinh(), neg_inf); assert!(nan.asinh().is_nan()); @@ -2035,9 +1614,9 @@ mod tests { assert_eq!(1.0f64.acosh(), 0.0f64); assert!(0.999f64.acosh().is_nan()); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; assert_eq!(inf.acosh(), inf); assert!(neg_inf.acosh().is_nan()); assert!(nan.acosh().is_nan()); @@ -2050,9 +1629,9 @@ mod tests { assert_eq!(0.0f64.atanh(), 0.0f64); assert_eq!((-0.0f64).atanh(), -0.0f64); - let inf: f64 = Float::infinity(); - let neg_inf: f64 = Float::neg_infinity(); - let nan: f64 = Float::nan(); + let inf: f64 = INFINITY; + let neg_inf: f64 = NEG_INFINITY; + let nan: f64 = NAN; assert_eq!(1.0f64.atanh(), inf); assert_eq!((-1.0f64).atanh(), neg_inf); assert!(2f64.atanh().atanh().is_nan()); @@ -2076,9 +1655,9 @@ mod tests { let frac_pi_8: f64 = consts::FRAC_PI_8; let frac_1_pi: f64 = consts::FRAC_1_PI; let frac_2_pi: f64 = consts::FRAC_2_PI; - let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRTPI; - let sqrt2: f64 = consts::SQRT2; - let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT2; + let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI; + let sqrt2: f64 = consts::SQRT_2; + let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2; let e: f64 = consts::E; let log2_e: f64 = consts::LOG2_E; let log10_e: f64 = consts::LOG10_E; diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index e0b9c720dbb..cd26be013c4 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -15,1117 +15,27 @@ #![stable(feature = "rust1", since = "1.0.0")] #![allow(missing_docs)] -#![allow(deprecated)] -#[cfg(test)] use fmt::Debug; -use ops::{Add, Sub, Mul, Div, Rem, Neg}; +use fmt; +use core::num; -use marker::Copy; -use clone::Clone; -use cmp::{PartialOrd, PartialEq}; - -pub use core::num::{Int, SignedInt, Zero, One}; -pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive}; -pub use core::num::{from_int, from_i8, from_i16, from_i32, from_i64}; -pub use core::num::{from_uint, from_u8, from_u16, from_u32, from_u64}; -pub use core::num::{from_f32, from_f64}; -pub use core::num::{FromStrRadix, from_str_radix}; -pub use core::num::{FpCategory, ParseIntError, ParseFloatError}; +pub use core::num::{Zero, One}; +pub use core::num::{FpCategory, ParseIntError}; pub use core::num::{wrapping, Wrapping}; -use option::Option; - -#[unstable(feature = "std_misc", reason = "likely to be removed")] -pub mod strconv; - -/// Mathematical operations on primitive floating point numbers. -#[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", - reason = "replaced by inherent methods; use rust-lang/num for generics")] -pub trait Float - : Copy + Clone - + NumCast - + PartialOrd - + PartialEq - + Neg<Output=Self> - + Add<Output=Self> - + Sub<Output=Self> - + Mul<Output=Self> - + Div<Output=Self> - + Rem<Output=Self> -{ - // inlined methods from `num::Float` - /// Returns the `NaN` value. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let nan: f32 = Float::nan(); - /// - /// assert!(nan.is_nan()); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn nan() -> Self; - /// Returns the infinite value. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f32; - /// - /// let infinity: f32 = Float::infinity(); - /// - /// assert!(infinity.is_infinite()); - /// assert!(!infinity.is_finite()); - /// assert!(infinity > f32::MAX); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn infinity() -> Self; - /// Returns the negative infinite value. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f32; - /// - /// let neg_infinity: f32 = Float::neg_infinity(); - /// - /// assert!(neg_infinity.is_infinite()); - /// assert!(!neg_infinity.is_finite()); - /// assert!(neg_infinity < f32::MIN); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn neg_infinity() -> Self; - /// Returns `0.0`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let inf: f32 = Float::infinity(); - /// let zero: f32 = Float::zero(); - /// let neg_zero: f32 = Float::neg_zero(); - /// - /// assert_eq!(zero, neg_zero); - /// assert_eq!(7.0f32/inf, zero); - /// assert_eq!(zero * 10.0, zero); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn zero() -> Self; - /// Returns `-0.0`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let inf: f32 = Float::infinity(); - /// let zero: f32 = Float::zero(); - /// let neg_zero: f32 = Float::neg_zero(); - /// - /// assert_eq!(zero, neg_zero); - /// assert_eq!(7.0f32/inf, zero); - /// assert_eq!(zero * 10.0, zero); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn neg_zero() -> Self; - /// Returns `1.0`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let one: f32 = Float::one(); - /// - /// assert_eq!(one, 1.0f32); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn one() -> Self; - - // FIXME (#5527): These should be associated constants - - /// Deprecated: use `std::f32::MANTISSA_DIGITS` or `std::f64::MANTISSA_DIGITS` - /// instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::MANTISSA_DIGITS` or \ - `std::f64::MANTISSA_DIGITS` as appropriate")] - fn mantissa_digits(unused_self: Option<Self>) -> usize; - /// Deprecated: use `std::f32::DIGITS` or `std::f64::DIGITS` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::DIGITS` or `std::f64::DIGITS` as appropriate")] - fn digits(unused_self: Option<Self>) -> usize; - /// Deprecated: use `std::f32::EPSILON` or `std::f64::EPSILON` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::EPSILON` or `std::f64::EPSILON` as appropriate")] - fn epsilon() -> Self; - /// Deprecated: use `std::f32::MIN_EXP` or `std::f64::MIN_EXP` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::MIN_EXP` or `std::f64::MIN_EXP` as appropriate")] - fn min_exp(unused_self: Option<Self>) -> isize; - /// Deprecated: use `std::f32::MAX_EXP` or `std::f64::MAX_EXP` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::MAX_EXP` or `std::f64::MAX_EXP` as appropriate")] - fn max_exp(unused_self: Option<Self>) -> isize; - /// Deprecated: use `std::f32::MIN_10_EXP` or `std::f64::MIN_10_EXP` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::MIN_10_EXP` or `std::f64::MIN_10_EXP` as appropriate")] - fn min_10_exp(unused_self: Option<Self>) -> isize; - /// Deprecated: use `std::f32::MAX_10_EXP` or `std::f64::MAX_10_EXP` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", - reason = "use `std::f32::MAX_10_EXP` or `std::f64::MAX_10_EXP` as appropriate")] - fn max_10_exp(unused_self: Option<Self>) -> isize; - - /// Returns the smallest finite value that this type can represent. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f64; - /// - /// let x: f64 = Float::min_value(); - /// - /// assert_eq!(x, f64::MIN); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn min_value() -> Self; - /// Returns the smallest normalized positive number that this type can represent. - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn min_pos_value(unused_self: Option<Self>) -> Self; - /// Returns the largest finite value that this type can represent. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f64; - /// - /// let x: f64 = Float::max_value(); - /// assert_eq!(x, f64::MAX); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn max_value() -> Self; - /// Returns `true` if this value is `NaN` and false otherwise. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f64; - /// - /// let nan = f64::NAN; - /// let f = 7.0; - /// - /// assert!(nan.is_nan()); - /// assert!(!f.is_nan()); - /// ``` - #[unstable(feature = "std_misc", reason = "position is undecided")] - fn is_nan(self) -> bool; - /// Returns `true` if this value is positive infinity or negative infinity and - /// false otherwise. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f32; - /// - /// let f = 7.0f32; - /// let inf: f32 = Float::infinity(); - /// let neg_inf: f32 = Float::neg_infinity(); - /// let nan: f32 = f32::NAN; - /// - /// assert!(!f.is_infinite()); - /// assert!(!nan.is_infinite()); - /// - /// assert!(inf.is_infinite()); - /// assert!(neg_inf.is_infinite()); - /// ``` - #[unstable(feature = "std_misc", reason = "position is undecided")] - fn is_infinite(self) -> bool; - /// Returns `true` if this number is neither infinite nor `NaN`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f32; - /// - /// let f = 7.0f32; - /// let inf: f32 = Float::infinity(); - /// let neg_inf: f32 = Float::neg_infinity(); - /// let nan: f32 = f32::NAN; - /// - /// assert!(f.is_finite()); - /// - /// assert!(!nan.is_finite()); - /// assert!(!inf.is_finite()); - /// assert!(!neg_inf.is_finite()); - /// ``` - #[unstable(feature = "std_misc", reason = "position is undecided")] - fn is_finite(self) -> bool; - - /// Returns `true` if the number is neither zero, infinite, - /// [subnormal][subnormal], or `NaN`. - /// - /// ``` - /// use std::num::Float; - /// use std::f32; - /// - /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32 - /// let max = f32::MAX; - /// let lower_than_min = 1.0e-40_f32; - /// let zero = 0.0f32; - /// - /// assert!(min.is_normal()); - /// assert!(max.is_normal()); - /// - /// assert!(!zero.is_normal()); - /// assert!(!f32::NAN.is_normal()); - /// assert!(!f32::INFINITY.is_normal()); - /// // Values between `0` and `min` are Subnormal. - /// assert!(!lower_than_min.is_normal()); - /// ``` - /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number - #[unstable(feature = "std_misc", reason = "position is undecided")] - fn is_normal(self) -> bool; - - /// 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. - /// - /// ``` - /// use std::num::{Float, FpCategory}; - /// use std::f32; - /// - /// let num = 12.4f32; - /// let inf = f32::INFINITY; - /// - /// assert_eq!(num.classify(), FpCategory::Normal); - /// assert_eq!(inf.classify(), FpCategory::Infinite); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn classify(self) -> FpCategory; - - /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. - /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. - /// The floating point encoding is documented in the [Reference][floating-point]. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let num = 2.0f32; - /// - /// // (8388608, -22, 1) - /// let (mantissa, exponent, sign) = num.integer_decode(); - /// let sign_f = sign as f32; - /// let mantissa_f = mantissa as f32; - /// let exponent_f = num.powf(exponent as f32); - /// - /// // 1 * 8388608 * 2^(-22) == 2 - /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - /// [floating-point]: ../../../../../reference.html#machine-types - #[unstable(feature = "std_misc", reason = "signature is undecided")] - fn integer_decode(self) -> (u64, i16, i8); - - /// Returns the largest integer less than or equal to a number. - /// - /// ``` - /// use std::num::Float; - /// - /// let f = 3.99; - /// let g = 3.0; - /// - /// assert_eq!(f.floor(), 3.0); - /// assert_eq!(g.floor(), 3.0); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn floor(self) -> Self; - /// Returns the smallest integer greater than or equal to a number. - /// - /// ``` - /// use std::num::Float; - /// - /// let f = 3.01; - /// let g = 4.0; - /// - /// assert_eq!(f.ceil(), 4.0); - /// assert_eq!(g.ceil(), 4.0); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn ceil(self) -> Self; - /// Returns the nearest integer to a number. Round half-way cases away from - /// `0.0`. - /// - /// ``` - /// use std::num::Float; - /// - /// let f = 3.3; - /// let g = -3.3; - /// - /// assert_eq!(f.round(), 3.0); - /// assert_eq!(g.round(), -3.0); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn round(self) -> Self; - /// Returns the integer part of a number. - /// - /// ``` - /// use std::num::Float; - /// - /// let f = 3.3; - /// let g = -3.7; - /// - /// assert_eq!(f.trunc(), 3.0); - /// assert_eq!(g.trunc(), -3.0); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn trunc(self) -> Self; - /// Returns the fractional part of a number. - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 3.5; - /// let y = -3.5; - /// let abs_difference_x = (x.fract() - 0.5).abs(); - /// let abs_difference_y = (y.fract() - (-0.5)).abs(); - /// - /// assert!(abs_difference_x < 1e-10); - /// assert!(abs_difference_y < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn fract(self) -> Self; - /// Computes the absolute value of `self`. Returns `Float::nan()` if the - /// number is `Float::nan()`. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let x = 3.5; - /// let y = -3.5; - /// - /// let abs_difference_x = (x.abs() - x).abs(); - /// let abs_difference_y = (y.abs() - (-y)).abs(); - /// - /// assert!(abs_difference_x < 1e-10); - /// assert!(abs_difference_y < 1e-10); - /// - /// assert!(f64::NAN.abs().is_nan()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn abs(self) -> Self; - /// Returns a number that represents the sign of `self`. - /// - /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` - /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` - /// - `Float::nan()` if the number is `Float::nan()` - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let f = 3.5; - /// - /// assert_eq!(f.signum(), 1.0); - /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0); - /// - /// assert!(f64::NAN.signum().is_nan()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn signum(self) -> Self; - /// Returns `true` if `self` is positive, including `+0.0` and - /// `Float::infinity()`. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let nan: f64 = f64::NAN; - /// - /// let f = 7.0; - /// let g = -7.0; - /// - /// assert!(f.is_positive()); - /// assert!(!g.is_positive()); - /// // Requires both tests to determine if is `NaN` - /// assert!(!nan.is_positive() && !nan.is_negative()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn is_positive(self) -> bool; - /// Returns `true` if `self` is negative, including `-0.0` and - /// `Float::neg_infinity()`. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let nan = f64::NAN; - /// - /// let f = 7.0; - /// let g = -7.0; - /// - /// assert!(!f.is_negative()); - /// assert!(g.is_negative()); - /// // Requires both tests to determine if is `NaN`. - /// assert!(!nan.is_positive() && !nan.is_negative()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn is_negative(self) -> bool; - - /// 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. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let m = 10.0; - /// let x = 4.0; - /// let b = 60.0; - /// - /// // 100.0 - /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn mul_add(self, a: Self, b: Self) -> Self; - /// Takes the reciprocal (inverse) of a number, `1/x`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 2.0; - /// let abs_difference = (x.recip() - (1.0/x)).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn recip(self) -> Self; - - /// Raises a number to an integer power. - /// - /// Using this function is generally faster than using `powf` - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 2.0; - /// let abs_difference = (x.powi(2) - x*x).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn powi(self, n: i32) -> Self; - /// Raises a number to a floating point power. - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 2.0; - /// let abs_difference = (x.powf(2.0) - x*x).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn powf(self, n: Self) -> Self; - /// Takes the square root of a number. - /// - /// Returns NaN if `self` is a negative number. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let positive = 4.0; - /// let negative = -4.0; - /// - /// let abs_difference = (positive.sqrt() - 2.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// assert!(negative.sqrt().is_nan()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn sqrt(self) -> Self; - - /// Takes the reciprocal (inverse) square root of a number, `1/sqrt(x)`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let f = 4.0; - /// - /// let abs_difference = (f.rsqrt() - 0.5).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn rsqrt(self) -> Self; - - /// Returns `e^(self)`, (the exponential function). - /// - /// ``` - /// use std::num::Float; - /// - /// let one = 1.0; - /// // e^1 - /// let e = one.exp(); - /// - /// // ln(e) - 1 == 0 - /// let abs_difference = (e.ln() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn exp(self) -> Self; - /// Returns `2^(self)`. - /// - /// ``` - /// use std::num::Float; - /// - /// let f = 2.0; - /// - /// // 2^2 - 4 == 0 - /// let abs_difference = (f.exp2() - 4.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn exp2(self) -> Self; - /// Returns the natural logarithm of the number. - /// - /// ``` - /// use std::num::Float; - /// - /// let one = 1.0; - /// // e^1 - /// let e = one.exp(); - /// - /// // ln(e) - 1 == 0 - /// let abs_difference = (e.ln() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn ln(self) -> Self; - /// Returns the logarithm of the number with respect to an arbitrary base. - /// - /// ``` - /// use std::num::Float; - /// - /// let ten = 10.0; - /// let two = 2.0; - /// - /// // log10(10) - 1 == 0 - /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs(); - /// - /// // log2(2) - 1 == 0 - /// let abs_difference_2 = (two.log(2.0) - 1.0).abs(); - /// - /// assert!(abs_difference_10 < 1e-10); - /// assert!(abs_difference_2 < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn log(self, base: Self) -> Self; - /// Returns the base 2 logarithm of the number. - /// - /// ``` - /// use std::num::Float; - /// - /// let two = 2.0; - /// - /// // log2(2) - 1 == 0 - /// let abs_difference = (two.log2() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn log2(self) -> Self; - /// Returns the base 10 logarithm of the number. - /// - /// ``` - /// use std::num::Float; - /// - /// let ten = 10.0; - /// - /// // log10(10) - 1 == 0 - /// let abs_difference = (ten.log10() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn log10(self) -> Self; - - /// Converts radians to degrees. - /// - /// ``` - /// use std::num::Float; - /// use std::f64::consts; - /// - /// let angle = consts::PI; - /// - /// let abs_difference = (angle.to_degrees() - 180.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", reason = "desirability is unclear")] - fn to_degrees(self) -> Self; - /// Converts degrees to radians. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// use std::f64::consts; - /// - /// let angle = 180.0; - /// - /// let abs_difference = (angle.to_radians() - consts::PI).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", reason = "desirability is unclear")] - fn to_radians(self) -> Self; - /// Constructs a floating point number of `x*2^exp`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// // 3*2^2 - 12 == 0 - /// let abs_difference = (Float::ldexp(3.0, 2) - 12.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "pending integer conventions")] - fn ldexp(self, exp: isize) -> Self; - /// Breaks the number into a normalized fraction and a base-2 exponent, - /// satisfying: - /// - /// * `self = x * 2^exp` - /// * `0.5 <= abs(x) < 1.0` - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 4.0; - /// - /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0 - /// let f = x.frexp(); - /// let abs_difference_0 = (f.0 - 0.5).abs(); - /// let abs_difference_1 = (f.1 as f64 - 3.0).abs(); - /// - /// assert!(abs_difference_0 < 1e-10); - /// assert!(abs_difference_1 < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "pending integer conventions")] - fn frexp(self) -> (Self, isize); - /// Returns the next representable floating-point value in the direction of - /// `other`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 1.0f32; - /// - /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs(); - /// - /// assert!(abs_diff < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn next_after(self, other: Self) -> Self; - - /// Returns the maximum of the two numbers. - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 1.0; - /// let y = 2.0; - /// - /// assert_eq!(x.max(y), y); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn max(self, other: Self) -> Self; - /// Returns the minimum of the two numbers. - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 1.0; - /// let y = 2.0; - /// - /// assert_eq!(x.min(y), x); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn min(self, other: Self) -> Self; - - /// The positive difference of two numbers. - /// - /// * If `self <= other`: `0:0` - /// * Else: `self - other` - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 3.0; - /// let y = -3.0; - /// - /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs(); - /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs(); - /// - /// assert!(abs_difference_x < 1e-10); - /// assert!(abs_difference_y < 1e-10); - /// ``` - #[unstable(feature = "std_misc", reason = "may be renamed")] - fn abs_sub(self, other: Self) -> Self; - /// Takes the cubic root of a number. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 8.0; - /// - /// // x^(1/3) - 2 == 0 - /// let abs_difference = (x.cbrt() - 2.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", reason = "may be renamed")] - fn cbrt(self) -> Self; - /// Calculates the length of the hypotenuse of a right-angle triangle given - /// legs of length `x` and `y`. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 2.0; - /// let y = 3.0; - /// - /// // sqrt(x^2 + y^2) - /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", - reason = "unsure about its place in the world")] - fn hypot(self, other: Self) -> Self; - - /// Computes the sine of a number (in radians). - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let x = f64::consts::PI/2.0; - /// - /// let abs_difference = (x.sin() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn sin(self) -> Self; - /// Computes the cosine of a number (in radians). - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let x = 2.0*f64::consts::PI; - /// - /// let abs_difference = (x.cos() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn cos(self) -> Self; - /// Computes the tangent of a number (in radians). - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let x = f64::consts::PI/4.0; - /// let abs_difference = (x.tan() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-14); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - 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]. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let f = f64::consts::PI / 2.0; - /// - /// // asin(sin(pi/2)) - /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - 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]. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let f = f64::consts::PI / 4.0; - /// - /// // acos(cos(pi/4)) - /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn acos(self) -> Self; - /// Computes the arctangent of a number. Return value is in radians in the - /// range [-pi/2, pi/2]; - /// - /// ``` - /// use std::num::Float; - /// - /// let f = 1.0; - /// - /// // atan(tan(1)) - /// let abs_difference = (f.tan().atan() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn atan(self) -> Self; - /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`). - /// - /// * `x = 0`, `y = 0`: `0` - /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]` - /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]` - /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)` - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let pi = f64::consts::PI; - /// // All angles from horizontal right (+x) - /// // 45 deg counter-clockwise - /// let x1 = 3.0; - /// let y1 = -3.0; - /// - /// // 135 deg clockwise - /// let x2 = -3.0; - /// let y2 = 3.0; - /// - /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs(); - /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs(); - /// - /// assert!(abs_difference_1 < 1e-10); - /// assert!(abs_difference_2 < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn atan2(self, other: Self) -> Self; - /// Simultaneously computes the sine and cosine of the number, `x`. Returns - /// `(sin(x), cos(x))`. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let x = f64::consts::PI/4.0; - /// let f = x.sin_cos(); - /// - /// let abs_difference_0 = (f.0 - x.sin()).abs(); - /// let abs_difference_1 = (f.1 - x.cos()).abs(); - /// - /// assert!(abs_difference_0 < 1e-10); - /// assert!(abs_difference_0 < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn sin_cos(self) -> (Self, Self); - - /// Returns `e^(self) - 1` in a way that is accurate even if the - /// number is close to zero. - /// - /// ``` - /// # #![feature(std_misc)] - /// use std::num::Float; - /// - /// let x = 7.0; - /// - /// // e^(ln(7)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", reason = "may be renamed")] - fn exp_m1(self) -> Self; - /// Returns `ln(1+n)` (natural logarithm) more accurately than if - /// the operations were performed separately. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let x = f64::consts::E - 1.0; - /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[unstable(feature = "std_misc", reason = "may be renamed")] - fn ln_1p(self) -> Self; - - /// Hyperbolic sine function. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let e = f64::consts::E; - /// let x = 1.0; - /// - /// let f = x.sinh(); - /// // Solving sinh() at 1 gives `(e^2-1)/(2e)` - /// let g = (e*e - 1.0)/(2.0*e); - /// let abs_difference = (f - g).abs(); - /// - /// assert!(abs_difference < 1e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn sinh(self) -> Self; - /// Hyperbolic cosine function. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let e = f64::consts::E; - /// let x = 1.0; - /// let f = x.cosh(); - /// // Solving cosh() at 1 gives this result - /// let g = (e*e + 1.0)/(2.0*e); - /// let abs_difference = (f - g).abs(); - /// - /// // Same result - /// assert!(abs_difference < 1.0e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn cosh(self) -> Self; - /// Hyperbolic tangent function. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let e = f64::consts::E; - /// let x = 1.0; - /// - /// let f = x.tanh(); - /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))` - /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2)); - /// let abs_difference = (f - g).abs(); - /// - /// assert!(abs_difference < 1.0e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn tanh(self) -> Self; - /// Inverse hyperbolic sine function. - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 1.0; - /// let f = x.sinh().asinh(); - /// - /// let abs_difference = (f - x).abs(); - /// - /// assert!(abs_difference < 1.0e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn asinh(self) -> Self; - /// Inverse hyperbolic cosine function. - /// - /// ``` - /// use std::num::Float; - /// - /// let x = 1.0; - /// let f = x.cosh().acosh(); - /// - /// let abs_difference = (f - x).abs(); - /// - /// assert!(abs_difference < 1.0e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn acosh(self) -> Self; - /// Inverse hyperbolic tangent function. - /// - /// ``` - /// use std::num::Float; - /// use std::f64; - /// - /// let e = f64::consts::E; - /// let f = e.tanh().atanh(); - /// - /// let abs_difference = (f - e).abs(); - /// - /// assert!(abs_difference < 1.0e-10); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - fn atanh(self) -> Self; -} +#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem}; +#[cfg(test)] use cmp::PartialEq; +#[cfg(test)] use marker::Copy; /// Helper function for testing numeric operations #[cfg(test)] pub fn test_num<T>(ten: T, two: T) where - T: PartialEq + NumCast + T: PartialEq + Add<Output=T> + Sub<Output=T> + Mul<Output=T> + Div<Output=T> - + Rem<Output=T> + Debug + + Rem<Output=T> + fmt::Debug + Copy { - assert_eq!(ten.add(two), cast(12).unwrap()); - assert_eq!(ten.sub(two), cast(8).unwrap()); - assert_eq!(ten.mul(two), cast(20).unwrap()); - assert_eq!(ten.div(two), cast(5).unwrap()); - assert_eq!(ten.rem(two), cast(0).unwrap()); - assert_eq!(ten.add(two), ten + two); assert_eq!(ten.sub(two), ten - two); assert_eq!(ten.mul(two), ten * two); @@ -1133,6 +43,31 @@ pub fn test_num<T>(ten: T, two: T) where assert_eq!(ten.rem(two), ten % two); } +/// An error which can be returned when parsing a float. +#[derive(Debug, Clone, PartialEq)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct ParseFloatError { inner: num::ParseFloatError } + +impl ::sys_common::FromInner<num::ParseFloatError> for ParseFloatError { + fn from_inner(inner: num::ParseFloatError) -> ParseFloatError { + ParseFloatError { inner: inner } + } +} + +impl ParseFloatError { + #[unstable(feature = "core", reason = "available through Error trait")] + pub fn description(&self) -> &str { + self.inner.description() + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Display for ParseFloatError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.description().fmt(f) + } +} + #[cfg(test)] mod tests { use core::prelude::*; @@ -1148,432 +83,7 @@ mod tests { use u64; use usize; use string::ToString; - - macro_rules! test_cast_20 { - ($_20:expr) => ({ - let _20 = $_20; - - assert_eq!(20usize, _20.to_uint().unwrap()); - assert_eq!(20u8, _20.to_u8().unwrap()); - assert_eq!(20u16, _20.to_u16().unwrap()); - assert_eq!(20u32, _20.to_u32().unwrap()); - assert_eq!(20u64, _20.to_u64().unwrap()); - assert_eq!(20, _20.to_int().unwrap()); - assert_eq!(20i8, _20.to_i8().unwrap()); - assert_eq!(20i16, _20.to_i16().unwrap()); - assert_eq!(20i32, _20.to_i32().unwrap()); - assert_eq!(20i64, _20.to_i64().unwrap()); - assert_eq!(20f32, _20.to_f32().unwrap()); - assert_eq!(20f64, _20.to_f64().unwrap()); - - assert_eq!(_20, NumCast::from(20usize).unwrap()); - assert_eq!(_20, NumCast::from(20u8).unwrap()); - assert_eq!(_20, NumCast::from(20u16).unwrap()); - assert_eq!(_20, NumCast::from(20u32).unwrap()); - assert_eq!(_20, NumCast::from(20u64).unwrap()); - assert_eq!(_20, NumCast::from(20).unwrap()); - assert_eq!(_20, NumCast::from(20i8).unwrap()); - assert_eq!(_20, NumCast::from(20i16).unwrap()); - assert_eq!(_20, NumCast::from(20i32).unwrap()); - assert_eq!(_20, NumCast::from(20i64).unwrap()); - assert_eq!(_20, NumCast::from(20f32).unwrap()); - assert_eq!(_20, NumCast::from(20f64).unwrap()); - - assert_eq!(_20, cast(20usize).unwrap()); - assert_eq!(_20, cast(20u8).unwrap()); - assert_eq!(_20, cast(20u16).unwrap()); - assert_eq!(_20, cast(20u32).unwrap()); - assert_eq!(_20, cast(20u64).unwrap()); - assert_eq!(_20, cast(20).unwrap()); - assert_eq!(_20, cast(20i8).unwrap()); - assert_eq!(_20, cast(20i16).unwrap()); - assert_eq!(_20, cast(20i32).unwrap()); - assert_eq!(_20, cast(20i64).unwrap()); - assert_eq!(_20, cast(20f32).unwrap()); - assert_eq!(_20, cast(20f64).unwrap()); - }) - } - - #[test] fn test_u8_cast() { test_cast_20!(20u8) } - #[test] fn test_u16_cast() { test_cast_20!(20u16) } - #[test] fn test_u32_cast() { test_cast_20!(20u32) } - #[test] fn test_u64_cast() { test_cast_20!(20u64) } - #[test] fn test_uint_cast() { test_cast_20!(20usize) } - #[test] fn test_i8_cast() { test_cast_20!(20i8) } - #[test] fn test_i16_cast() { test_cast_20!(20i16) } - #[test] fn test_i32_cast() { test_cast_20!(20i32) } - #[test] fn test_i64_cast() { test_cast_20!(20i64) } - #[test] fn test_int_cast() { test_cast_20!(20) } - #[test] fn test_f32_cast() { test_cast_20!(20f32) } - #[test] fn test_f64_cast() { test_cast_20!(20f64) } - - #[test] - fn test_cast_range_int_min() { - assert_eq!(isize::MIN.to_int(), Some(isize::MIN as isize)); - assert_eq!(isize::MIN.to_i8(), None); - assert_eq!(isize::MIN.to_i16(), None); - // isize::MIN.to_i32() is word-size specific - assert_eq!(isize::MIN.to_i64(), Some(isize::MIN as i64)); - assert_eq!(isize::MIN.to_uint(), None); - assert_eq!(isize::MIN.to_u8(), None); - assert_eq!(isize::MIN.to_u16(), None); - assert_eq!(isize::MIN.to_u32(), None); - assert_eq!(isize::MIN.to_u64(), None); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(isize::MIN.to_i32(), Some(isize::MIN as i32)); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(isize::MIN.to_i32(), None); - } - - check_word_size(); - } - - #[test] - fn test_cast_range_i8_min() { - assert_eq!(i8::MIN.to_int(), Some(i8::MIN as isize)); - assert_eq!(i8::MIN.to_i8(), Some(i8::MIN as i8)); - assert_eq!(i8::MIN.to_i16(), Some(i8::MIN as i16)); - assert_eq!(i8::MIN.to_i32(), Some(i8::MIN as i32)); - assert_eq!(i8::MIN.to_i64(), Some(i8::MIN as i64)); - assert_eq!(i8::MIN.to_uint(), None); - assert_eq!(i8::MIN.to_u8(), None); - assert_eq!(i8::MIN.to_u16(), None); - assert_eq!(i8::MIN.to_u32(), None); - assert_eq!(i8::MIN.to_u64(), None); - } - - #[test] - fn test_cast_range_i16_min() { - assert_eq!(i16::MIN.to_int(), Some(i16::MIN as isize)); - assert_eq!(i16::MIN.to_i8(), None); - assert_eq!(i16::MIN.to_i16(), Some(i16::MIN as i16)); - assert_eq!(i16::MIN.to_i32(), Some(i16::MIN as i32)); - assert_eq!(i16::MIN.to_i64(), Some(i16::MIN as i64)); - assert_eq!(i16::MIN.to_uint(), None); - assert_eq!(i16::MIN.to_u8(), None); - assert_eq!(i16::MIN.to_u16(), None); - assert_eq!(i16::MIN.to_u32(), None); - assert_eq!(i16::MIN.to_u64(), None); - } - - #[test] - fn test_cast_range_i32_min() { - assert_eq!(i32::MIN.to_int(), Some(i32::MIN as isize)); - assert_eq!(i32::MIN.to_i8(), None); - assert_eq!(i32::MIN.to_i16(), None); - assert_eq!(i32::MIN.to_i32(), Some(i32::MIN as i32)); - assert_eq!(i32::MIN.to_i64(), Some(i32::MIN as i64)); - assert_eq!(i32::MIN.to_uint(), None); - assert_eq!(i32::MIN.to_u8(), None); - assert_eq!(i32::MIN.to_u16(), None); - assert_eq!(i32::MIN.to_u32(), None); - assert_eq!(i32::MIN.to_u64(), None); - } - - #[test] - fn test_cast_range_i64_min() { - // i64::MIN.to_int() is word-size specific - assert_eq!(i64::MIN.to_i8(), None); - assert_eq!(i64::MIN.to_i16(), None); - assert_eq!(i64::MIN.to_i32(), None); - assert_eq!(i64::MIN.to_i64(), Some(i64::MIN as i64)); - assert_eq!(i64::MIN.to_uint(), None); - assert_eq!(i64::MIN.to_u8(), None); - assert_eq!(i64::MIN.to_u16(), None); - assert_eq!(i64::MIN.to_u32(), None); - assert_eq!(i64::MIN.to_u64(), None); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(i64::MIN.to_int(), None); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(i64::MIN.to_int(), Some(i64::MIN as isize)); - } - - check_word_size(); - } - - #[test] - fn test_cast_range_int_max() { - assert_eq!(isize::MAX.to_int(), Some(isize::MAX as isize)); - assert_eq!(isize::MAX.to_i8(), None); - assert_eq!(isize::MAX.to_i16(), None); - // isize::MAX.to_i32() is word-size specific - assert_eq!(isize::MAX.to_i64(), Some(isize::MAX as i64)); - assert_eq!(isize::MAX.to_u8(), None); - assert_eq!(isize::MAX.to_u16(), None); - // isize::MAX.to_u32() is word-size specific - assert_eq!(isize::MAX.to_u64(), Some(isize::MAX as u64)); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(isize::MAX.to_i32(), Some(isize::MAX as i32)); - assert_eq!(isize::MAX.to_u32(), Some(isize::MAX as u32)); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(isize::MAX.to_i32(), None); - assert_eq!(isize::MAX.to_u32(), None); - } - - check_word_size(); - } - - #[test] - fn test_cast_range_i8_max() { - assert_eq!(i8::MAX.to_int(), Some(i8::MAX as isize)); - assert_eq!(i8::MAX.to_i8(), Some(i8::MAX as i8)); - assert_eq!(i8::MAX.to_i16(), Some(i8::MAX as i16)); - assert_eq!(i8::MAX.to_i32(), Some(i8::MAX as i32)); - assert_eq!(i8::MAX.to_i64(), Some(i8::MAX as i64)); - assert_eq!(i8::MAX.to_uint(), Some(i8::MAX as usize)); - assert_eq!(i8::MAX.to_u8(), Some(i8::MAX as u8)); - assert_eq!(i8::MAX.to_u16(), Some(i8::MAX as u16)); - assert_eq!(i8::MAX.to_u32(), Some(i8::MAX as u32)); - assert_eq!(i8::MAX.to_u64(), Some(i8::MAX as u64)); - } - - #[test] - fn test_cast_range_i16_max() { - assert_eq!(i16::MAX.to_int(), Some(i16::MAX as isize)); - assert_eq!(i16::MAX.to_i8(), None); - assert_eq!(i16::MAX.to_i16(), Some(i16::MAX as i16)); - assert_eq!(i16::MAX.to_i32(), Some(i16::MAX as i32)); - assert_eq!(i16::MAX.to_i64(), Some(i16::MAX as i64)); - assert_eq!(i16::MAX.to_uint(), Some(i16::MAX as usize)); - assert_eq!(i16::MAX.to_u8(), None); - assert_eq!(i16::MAX.to_u16(), Some(i16::MAX as u16)); - assert_eq!(i16::MAX.to_u32(), Some(i16::MAX as u32)); - assert_eq!(i16::MAX.to_u64(), Some(i16::MAX as u64)); - } - - #[test] - fn test_cast_range_i32_max() { - assert_eq!(i32::MAX.to_int(), Some(i32::MAX as isize)); - assert_eq!(i32::MAX.to_i8(), None); - assert_eq!(i32::MAX.to_i16(), None); - assert_eq!(i32::MAX.to_i32(), Some(i32::MAX as i32)); - assert_eq!(i32::MAX.to_i64(), Some(i32::MAX as i64)); - assert_eq!(i32::MAX.to_uint(), Some(i32::MAX as usize)); - assert_eq!(i32::MAX.to_u8(), None); - assert_eq!(i32::MAX.to_u16(), None); - assert_eq!(i32::MAX.to_u32(), Some(i32::MAX as u32)); - assert_eq!(i32::MAX.to_u64(), Some(i32::MAX as u64)); - } - - #[test] - fn test_cast_range_i64_max() { - // i64::MAX.to_int() is word-size specific - assert_eq!(i64::MAX.to_i8(), None); - assert_eq!(i64::MAX.to_i16(), None); - assert_eq!(i64::MAX.to_i32(), None); - assert_eq!(i64::MAX.to_i64(), Some(i64::MAX as i64)); - // i64::MAX.to_uint() is word-size specific - assert_eq!(i64::MAX.to_u8(), None); - assert_eq!(i64::MAX.to_u16(), None); - assert_eq!(i64::MAX.to_u32(), None); - assert_eq!(i64::MAX.to_u64(), Some(i64::MAX as u64)); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(i64::MAX.to_int(), None); - assert_eq!(i64::MAX.to_uint(), None); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(i64::MAX.to_int(), Some(i64::MAX as isize)); - assert_eq!(i64::MAX.to_uint(), Some(i64::MAX as usize)); - } - - check_word_size(); - } - - #[test] - fn test_cast_range_uint_min() { - assert_eq!(usize::MIN.to_int(), Some(usize::MIN as isize)); - assert_eq!(usize::MIN.to_i8(), Some(usize::MIN as i8)); - assert_eq!(usize::MIN.to_i16(), Some(usize::MIN as i16)); - assert_eq!(usize::MIN.to_i32(), Some(usize::MIN as i32)); - assert_eq!(usize::MIN.to_i64(), Some(usize::MIN as i64)); - assert_eq!(usize::MIN.to_uint(), Some(usize::MIN as usize)); - assert_eq!(usize::MIN.to_u8(), Some(usize::MIN as u8)); - assert_eq!(usize::MIN.to_u16(), Some(usize::MIN as u16)); - assert_eq!(usize::MIN.to_u32(), Some(usize::MIN as u32)); - assert_eq!(usize::MIN.to_u64(), Some(usize::MIN as u64)); - } - - #[test] - fn test_cast_range_u8_min() { - assert_eq!(u8::MIN.to_int(), Some(u8::MIN as isize)); - assert_eq!(u8::MIN.to_i8(), Some(u8::MIN as i8)); - assert_eq!(u8::MIN.to_i16(), Some(u8::MIN as i16)); - assert_eq!(u8::MIN.to_i32(), Some(u8::MIN as i32)); - assert_eq!(u8::MIN.to_i64(), Some(u8::MIN as i64)); - assert_eq!(u8::MIN.to_uint(), Some(u8::MIN as usize)); - assert_eq!(u8::MIN.to_u8(), Some(u8::MIN as u8)); - assert_eq!(u8::MIN.to_u16(), Some(u8::MIN as u16)); - assert_eq!(u8::MIN.to_u32(), Some(u8::MIN as u32)); - assert_eq!(u8::MIN.to_u64(), Some(u8::MIN as u64)); - } - - #[test] - fn test_cast_range_u16_min() { - assert_eq!(u16::MIN.to_int(), Some(u16::MIN as isize)); - assert_eq!(u16::MIN.to_i8(), Some(u16::MIN as i8)); - assert_eq!(u16::MIN.to_i16(), Some(u16::MIN as i16)); - assert_eq!(u16::MIN.to_i32(), Some(u16::MIN as i32)); - assert_eq!(u16::MIN.to_i64(), Some(u16::MIN as i64)); - assert_eq!(u16::MIN.to_uint(), Some(u16::MIN as usize)); - assert_eq!(u16::MIN.to_u8(), Some(u16::MIN as u8)); - assert_eq!(u16::MIN.to_u16(), Some(u16::MIN as u16)); - assert_eq!(u16::MIN.to_u32(), Some(u16::MIN as u32)); - assert_eq!(u16::MIN.to_u64(), Some(u16::MIN as u64)); - } - - #[test] - fn test_cast_range_u32_min() { - assert_eq!(u32::MIN.to_int(), Some(u32::MIN as isize)); - assert_eq!(u32::MIN.to_i8(), Some(u32::MIN as i8)); - assert_eq!(u32::MIN.to_i16(), Some(u32::MIN as i16)); - assert_eq!(u32::MIN.to_i32(), Some(u32::MIN as i32)); - assert_eq!(u32::MIN.to_i64(), Some(u32::MIN as i64)); - assert_eq!(u32::MIN.to_uint(), Some(u32::MIN as usize)); - assert_eq!(u32::MIN.to_u8(), Some(u32::MIN as u8)); - assert_eq!(u32::MIN.to_u16(), Some(u32::MIN as u16)); - assert_eq!(u32::MIN.to_u32(), Some(u32::MIN as u32)); - assert_eq!(u32::MIN.to_u64(), Some(u32::MIN as u64)); - } - - #[test] - fn test_cast_range_u64_min() { - assert_eq!(u64::MIN.to_int(), Some(u64::MIN as isize)); - assert_eq!(u64::MIN.to_i8(), Some(u64::MIN as i8)); - assert_eq!(u64::MIN.to_i16(), Some(u64::MIN as i16)); - assert_eq!(u64::MIN.to_i32(), Some(u64::MIN as i32)); - assert_eq!(u64::MIN.to_i64(), Some(u64::MIN as i64)); - assert_eq!(u64::MIN.to_uint(), Some(u64::MIN as usize)); - assert_eq!(u64::MIN.to_u8(), Some(u64::MIN as u8)); - assert_eq!(u64::MIN.to_u16(), Some(u64::MIN as u16)); - assert_eq!(u64::MIN.to_u32(), Some(u64::MIN as u32)); - assert_eq!(u64::MIN.to_u64(), Some(u64::MIN as u64)); - } - - #[test] - fn test_cast_range_uint_max() { - assert_eq!(usize::MAX.to_int(), None); - assert_eq!(usize::MAX.to_i8(), None); - assert_eq!(usize::MAX.to_i16(), None); - assert_eq!(usize::MAX.to_i32(), None); - // usize::MAX.to_i64() is word-size specific - assert_eq!(usize::MAX.to_u8(), None); - assert_eq!(usize::MAX.to_u16(), None); - // usize::MAX.to_u32() is word-size specific - assert_eq!(usize::MAX.to_u64(), Some(usize::MAX as u64)); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(usize::MAX.to_u32(), Some(usize::MAX as u32)); - assert_eq!(usize::MAX.to_i64(), Some(usize::MAX as i64)); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(usize::MAX.to_u32(), None); - assert_eq!(usize::MAX.to_i64(), None); - } - - check_word_size(); - } - - #[test] - fn test_cast_range_u8_max() { - assert_eq!(u8::MAX.to_int(), Some(u8::MAX as isize)); - assert_eq!(u8::MAX.to_i8(), None); - assert_eq!(u8::MAX.to_i16(), Some(u8::MAX as i16)); - assert_eq!(u8::MAX.to_i32(), Some(u8::MAX as i32)); - assert_eq!(u8::MAX.to_i64(), Some(u8::MAX as i64)); - assert_eq!(u8::MAX.to_uint(), Some(u8::MAX as usize)); - assert_eq!(u8::MAX.to_u8(), Some(u8::MAX as u8)); - assert_eq!(u8::MAX.to_u16(), Some(u8::MAX as u16)); - assert_eq!(u8::MAX.to_u32(), Some(u8::MAX as u32)); - assert_eq!(u8::MAX.to_u64(), Some(u8::MAX as u64)); - } - - #[test] - fn test_cast_range_u16_max() { - assert_eq!(u16::MAX.to_int(), Some(u16::MAX as isize)); - assert_eq!(u16::MAX.to_i8(), None); - assert_eq!(u16::MAX.to_i16(), None); - assert_eq!(u16::MAX.to_i32(), Some(u16::MAX as i32)); - assert_eq!(u16::MAX.to_i64(), Some(u16::MAX as i64)); - assert_eq!(u16::MAX.to_uint(), Some(u16::MAX as usize)); - assert_eq!(u16::MAX.to_u8(), None); - assert_eq!(u16::MAX.to_u16(), Some(u16::MAX as u16)); - assert_eq!(u16::MAX.to_u32(), Some(u16::MAX as u32)); - assert_eq!(u16::MAX.to_u64(), Some(u16::MAX as u64)); - } - - #[test] - fn test_cast_range_u32_max() { - // u32::MAX.to_int() is word-size specific - assert_eq!(u32::MAX.to_i8(), None); - assert_eq!(u32::MAX.to_i16(), None); - assert_eq!(u32::MAX.to_i32(), None); - assert_eq!(u32::MAX.to_i64(), Some(u32::MAX as i64)); - assert_eq!(u32::MAX.to_uint(), Some(u32::MAX as usize)); - assert_eq!(u32::MAX.to_u8(), None); - assert_eq!(u32::MAX.to_u16(), None); - assert_eq!(u32::MAX.to_u32(), Some(u32::MAX as u32)); - assert_eq!(u32::MAX.to_u64(), Some(u32::MAX as u64)); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(u32::MAX.to_int(), None); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(u32::MAX.to_int(), Some(u32::MAX as isize)); - } - - check_word_size(); - } - - #[test] - fn test_cast_range_u64_max() { - assert_eq!(u64::MAX.to_int(), None); - assert_eq!(u64::MAX.to_i8(), None); - assert_eq!(u64::MAX.to_i16(), None); - assert_eq!(u64::MAX.to_i32(), None); - assert_eq!(u64::MAX.to_i64(), None); - // u64::MAX.to_uint() is word-size specific - assert_eq!(u64::MAX.to_u8(), None); - assert_eq!(u64::MAX.to_u16(), None); - assert_eq!(u64::MAX.to_u32(), None); - assert_eq!(u64::MAX.to_u64(), Some(u64::MAX as u64)); - - #[cfg(target_pointer_width = "32")] - fn check_word_size() { - assert_eq!(u64::MAX.to_uint(), None); - } - - #[cfg(target_pointer_width = "64")] - fn check_word_size() { - assert_eq!(u64::MAX.to_uint(), Some(u64::MAX as usize)); - } - - check_word_size(); - } + use ops::Mul; #[test] fn test_saturating_add_uint() { @@ -1596,23 +106,23 @@ mod tests { #[test] fn test_saturating_add_int() { use isize::{MIN,MAX}; - assert_eq!(3.saturating_add(5), 8); - assert_eq!(3.saturating_add(MAX-1), MAX); + assert_eq!(3i32.saturating_add(5), 8); + assert_eq!(3isize.saturating_add(MAX-1), MAX); assert_eq!(MAX.saturating_add(MAX), MAX); assert_eq!((MAX-2).saturating_add(1), MAX-1); - assert_eq!(3.saturating_add(-5), -2); + assert_eq!(3i32.saturating_add(-5), -2); assert_eq!(MIN.saturating_add(-1), MIN); - assert_eq!((-2).saturating_add(-MAX), MIN); + assert_eq!((-2isize).saturating_add(-MAX), MIN); } #[test] fn test_saturating_sub_int() { use isize::{MIN,MAX}; - assert_eq!(3.saturating_sub(5), -2); + assert_eq!(3i32.saturating_sub(5), -2); assert_eq!(MIN.saturating_sub(1), MIN); - assert_eq!((-2).saturating_sub(MAX), MIN); - assert_eq!(3.saturating_sub(-5), 8); - assert_eq!(3.saturating_sub(-(MAX-1)), MAX); + assert_eq!((-2isize).saturating_sub(MAX), MIN); + assert_eq!(3i32.saturating_sub(-5), 8); + assert_eq!(3isize.saturating_sub(-(MAX-1)), MAX); assert_eq!(MAX.saturating_sub(-MAX), MAX); assert_eq!((MAX-2).saturating_sub(-1), MAX-1); } @@ -1716,56 +226,10 @@ mod tests { test_checked_next_power_of_two! { test_checked_next_power_of_two_u64, u64 } test_checked_next_power_of_two! { test_checked_next_power_of_two_uint, usize } - #[derive(PartialEq, Debug)] - struct Value { x: isize } - - impl ToPrimitive for Value { - fn to_i64(&self) -> Option<i64> { self.x.to_i64() } - fn to_u64(&self) -> Option<u64> { self.x.to_u64() } - } - - impl FromPrimitive for Value { - fn from_i64(n: i64) -> Option<Value> { Some(Value { x: n as isize }) } - fn from_u64(n: u64) -> Option<Value> { Some(Value { x: n as isize }) } - } - - #[test] - fn test_to_primitive() { - let value = Value { x: 5 }; - assert_eq!(value.to_int(), Some(5)); - assert_eq!(value.to_i8(), Some(5)); - assert_eq!(value.to_i16(), Some(5)); - assert_eq!(value.to_i32(), Some(5)); - assert_eq!(value.to_i64(), Some(5)); - assert_eq!(value.to_uint(), Some(5)); - assert_eq!(value.to_u8(), Some(5)); - assert_eq!(value.to_u16(), Some(5)); - assert_eq!(value.to_u32(), Some(5)); - assert_eq!(value.to_u64(), Some(5)); - assert_eq!(value.to_f32(), Some(5f32)); - assert_eq!(value.to_f64(), Some(5f64)); - } - - #[test] - fn test_from_primitive() { - assert_eq!(from_int(5), Some(Value { x: 5 })); - assert_eq!(from_i8(5), Some(Value { x: 5 })); - assert_eq!(from_i16(5), Some(Value { x: 5 })); - assert_eq!(from_i32(5), Some(Value { x: 5 })); - assert_eq!(from_i64(5), Some(Value { x: 5 })); - assert_eq!(from_uint(5), Some(Value { x: 5 })); - assert_eq!(from_u8(5), Some(Value { x: 5 })); - assert_eq!(from_u16(5), Some(Value { x: 5 })); - assert_eq!(from_u32(5), Some(Value { x: 5 })); - assert_eq!(from_u64(5), Some(Value { x: 5 })); - assert_eq!(from_f32(5f32), Some(Value { x: 5 })); - assert_eq!(from_f64(5f64), Some(Value { x: 5 })); - } - #[test] fn test_pow() { - fn naive_pow<T: Int>(base: T, exp: usize) -> T { - let one: T = Int::one(); + fn naive_pow<T: Mul<Output=T> + One + Copy>(base: T, exp: usize) -> T { + let one: T = T::one(); (0..exp).fold(one, |acc, _| acc * base) } macro_rules! assert_pow { @@ -1775,11 +239,11 @@ mod tests { assert_eq!(result, naive_pow($num, $exp)); }} } - assert_pow!((3, 0 ) => 1); - assert_pow!((5, 1 ) => 5); - assert_pow!((-4, 2 ) => 16); - assert_pow!((8, 3 ) => 512); - assert_pow!((2u64, 50) => 1125899906842624); + assert_pow!((3u32, 0 ) => 1); + assert_pow!((5u32, 1 ) => 5); + assert_pow!((-4i32, 2 ) => 16); + assert_pow!((8u32, 3 ) => 512); + assert_pow!((2u64, 50) => 1125899906842624); } #[test] @@ -1854,12 +318,11 @@ mod tests { mod bench { extern crate test; use self::test::Bencher; - use num::Int; use prelude::v1::*; #[bench] fn bench_pow_function(b: &mut Bencher) { - let v = (0..1024).collect::<Vec<_>>(); - b.iter(|| {v.iter().fold(0, |old, new| old.pow(*new as u32));}); + let v = (0..1024).collect::<Vec<u32>>(); + b.iter(|| {v.iter().fold(0u32, |old, new| old.pow(*new as u32));}); } } diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs deleted file mode 100644 index ce1da4742d1..00000000000 --- a/src/libstd/num/strconv.rs +++ /dev/null @@ -1,556 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![allow(missing_docs)] -#![allow(deprecated)] - -use self::ExponentFormat::*; -use self::SignificantDigits::*; -use self::SignFormat::*; - -use char; -use num::{self, Int, Float, ToPrimitive}; -use num::FpCategory as Fp; -use ops::FnMut; -use string::String; -use vec::Vec; - -/// A flag that specifies whether to use exponential (scientific) notation. -#[derive(Copy, Clone)] -pub enum ExponentFormat { - /// Do not use exponential notation. - ExpNone, - /// Use exponential notation with the exponent having a base of 10 and the - /// exponent sign being `e` or `E`. For example, 1000 would be printed - /// 1e3. - ExpDec, - /// Use exponential notation with the exponent having a base of 2 and the - /// exponent sign being `p` or `P`. For example, 8 would be printed 1p3. - ExpBin, -} - -/// The number of digits used for emitting the fractional part of a number, if -/// any. -#[derive(Copy, Clone)] -pub enum SignificantDigits { - /// All calculable digits will be printed. - /// - /// Note that bignums or fractions may cause a surprisingly large number - /// of digits to be printed. - DigAll, - - /// At most the given number of digits will be printed, truncating any - /// trailing zeroes. - DigMax(usize), - - /// Precisely the given number of digits will be printed. - DigExact(usize) -} - -/// How to emit the sign of a number. -#[derive(Copy, Clone)] -pub enum SignFormat { - /// No sign will be printed. The exponent sign will also be emitted. - SignNone, - /// `-` will be printed for negative values, but no sign will be emitted - /// for positive numbers. - SignNeg, - /// `+` will be printed for positive values, and `-` will be printed for - /// negative values. - SignAll, -} - -/// Converts an integral number to its string representation as a byte vector. -/// This is meant to be a common base implementation for all integral string -/// conversion functions like `to_string()` or `to_str_radix()`. -/// -/// # Arguments -/// -/// - `num` - The number to convert. Accepts any number that -/// implements the numeric traits. -/// - `radix` - Base to use. Accepts only the values 2-36. -/// - `sign` - How to emit the sign. Options are: -/// - `SignNone`: No sign at all. Basically emits `abs(num)`. -/// - `SignNeg`: Only `-` on negative values. -/// - `SignAll`: Both `+` on positive, and `-` on negative numbers. -/// - `f` - a callback which will be invoked for each ascii character -/// which composes the string representation of this integer -/// -/// # Panics -/// -/// - Panics if `radix` < 2 or `radix` > 36. -fn int_to_str_bytes_common<T, F>(num: T, radix: usize, sign: SignFormat, mut f: F) where - T: Int, - F: FnMut(u8), -{ - assert!(2 <= radix && radix <= 36); - - let _0: T = Int::zero(); - - let neg = num < _0; - let radix_gen: T = num::cast(radix).unwrap(); - - let mut deccum = num; - // This is just for integral types, the largest of which is a u64. The - // smallest base that we can have is 2, so the most number of digits we're - // ever going to have is 64 - let mut buf = [0; 64]; - let mut cur = 0; - - // Loop at least once to make sure at least a `0` gets emitted. - loop { - // Calculate the absolute value of each digit instead of only - // doing it once for the whole number because a - // representable negative number doesn't necessary have an - // representable additive inverse of the same type - // (See twos complement). But we assume that for the - // numbers [-35 .. 0] we always have [0 .. 35]. - let current_digit_signed = deccum % radix_gen; - let current_digit = if current_digit_signed < _0 { - _0 - current_digit_signed - } else { - current_digit_signed - }; - buf[cur] = match current_digit.to_u8().unwrap() { - i @ 0...9 => b'0' + i, - i => b'a' + (i - 10), - }; - cur += 1; - - deccum = deccum / radix_gen; - // No more digits to calculate for the non-fractional part -> break - if deccum == _0 { break; } - } - - // Decide what sign to put in front - match sign { - SignNeg | SignAll if neg => { f(b'-'); } - SignAll => { f(b'+'); } - _ => () - } - - // We built the number in reverse order, so un-reverse it here - while cur > 0 { - cur -= 1; - f(buf[cur]); - } -} - -/// Converts a number to its string representation as a byte vector. -/// This is meant to be a common base implementation for all numeric string -/// conversion functions like `to_string()` or `to_str_radix()`. -/// -/// # Arguments -/// -/// - `num` - The number to convert. Accepts any number that -/// implements the numeric traits. -/// - `radix` - Base to use. Accepts only the values 2-36. If the exponential notation -/// is used, then this base is only used for the significand. The exponent -/// itself always printed using a base of 10. -/// - `negative_zero` - Whether to treat the special value `-0` as -/// `-0` or as `+0`. -/// - `sign` - How to emit the sign. See `SignFormat`. -/// - `digits` - The amount of digits to use for emitting the fractional -/// part, if any. See `SignificantDigits`. -/// - `exp_format` - Whether or not to use the exponential (scientific) notation. -/// See `ExponentFormat`. -/// - `exp_capital` - Whether or not to use a capital letter for the exponent sign, if -/// exponential notation is desired. -/// -/// # Return value -/// -/// A tuple containing the byte vector, and a boolean flag indicating -/// whether it represents a special value like `inf`, `-inf`, `NaN` or not. -/// It returns a tuple because there can be ambiguity between a special value -/// and a number representation at higher bases. -/// -/// # Panics -/// -/// - Panics if `radix` < 2 or `radix` > 36. -/// - Panics if `radix` > 14 and `exp_format` is `ExpDec` due to conflict -/// between digit and exponent sign `'e'`. -/// - Panics if `radix` > 25 and `exp_format` is `ExpBin` due to conflict -/// between digit and exponent sign `'p'`. -pub fn float_to_str_bytes_common<T: Float>( - num: T, radix: u32, negative_zero: bool, - sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_upper: bool - ) -> (Vec<u8>, bool) { - assert!(2 <= radix && radix <= 36); - match exp_format { - ExpDec if radix >= DIGIT_E_RADIX // decimal exponent 'e' - => panic!("float_to_str_bytes_common: radix {} incompatible with \ - use of 'e' as decimal exponent", radix), - ExpBin if radix >= DIGIT_P_RADIX // binary exponent 'p' - => panic!("float_to_str_bytes_common: radix {} incompatible with \ - use of 'p' as binary exponent", radix), - _ => () - } - - let _0: T = Float::zero(); - let _1: T = Float::one(); - - match num.classify() { - Fp::Nan => { return (b"NaN".to_vec(), true); } - Fp::Infinite if num > _0 => { - return match sign { - SignAll => (b"+inf".to_vec(), true), - _ => (b"inf".to_vec(), true) - }; - } - Fp::Infinite if num < _0 => { - return match sign { - SignNone => (b"inf".to_vec(), true), - _ => (b"-inf".to_vec(), true), - }; - } - _ => {} - } - - let neg = num < _0 || (negative_zero && _1 / num == Float::neg_infinity()); - let mut buf = Vec::new(); - let radix_gen: T = num::cast(radix as isize).unwrap(); - - let (num, exp) = match exp_format { - ExpNone => (num, 0), - ExpDec | ExpBin => { - if num == _0 { - (num, 0) - } else { - let (exp, exp_base) = match exp_format { - ExpDec => (num.abs().log10().floor(), num::cast::<f64, T>(10.0f64).unwrap()), - ExpBin => (num.abs().log2().floor(), num::cast::<f64, T>(2.0f64).unwrap()), - ExpNone => unreachable!() - }; - - (num / exp_base.powf(exp), num::cast::<T, i32>(exp).unwrap()) - } - } - }; - - // First emit the non-fractional part, looping at least once to make - // sure at least a `0` gets emitted. - let mut deccum = num.trunc(); - loop { - // Calculate the absolute value of each digit instead of only - // doing it once for the whole number because a - // representable negative number doesn't necessary have an - // representable additive inverse of the same type - // (See twos complement). But we assume that for the - // numbers [-35 .. 0] we always have [0 .. 35]. - let current_digit = (deccum % radix_gen).abs(); - - // Decrease the deccumulator one digit at a time - deccum = deccum / radix_gen; - deccum = deccum.trunc(); - - buf.push(char::from_digit(current_digit.to_isize().unwrap() as u32, radix) - .unwrap() as u8); - - // No more digits to calculate for the non-fractional part -> break - if deccum == _0 { break; } - } - - // If limited digits, calculate one digit more for rounding. - let (limit_digits, digit_count, exact) = match digits { - DigAll => (false, 0, false), - DigMax(count) => (true, count+1, false), - DigExact(count) => (true, count+1, true) - }; - - // Decide what sign to put in front - match sign { - SignNeg | SignAll if neg => { - buf.push(b'-'); - } - SignAll => { - buf.push(b'+'); - } - _ => () - } - - buf.reverse(); - - // Remember start of the fractional digits. - // Points one beyond end of buf if none get generated, - // or at the '.' otherwise. - let start_fractional_digits = buf.len(); - - // Now emit the fractional part, if any - deccum = num.fract(); - if deccum != _0 || (limit_digits && exact && digit_count > 0) { - buf.push(b'.'); - let mut dig = 0; - - // calculate new digits while - // - there is no limit and there are digits left - // - or there is a limit, it's not reached yet and - // - it's exact - // - or it's a maximum, and there are still digits left - while (!limit_digits && deccum != _0) - || (limit_digits && dig < digit_count && ( - exact - || (!exact && deccum != _0) - ) - ) { - // Shift first fractional digit into the integer part - deccum = deccum * radix_gen; - - // Calculate the absolute value of each digit. - // See note in first loop. - let current_digit = deccum.trunc().abs(); - - buf.push(char::from_digit( - current_digit.to_isize().unwrap() as u32, radix).unwrap() as u8); - - // Decrease the deccumulator one fractional digit at a time - deccum = deccum.fract(); - dig += 1; - } - - // If digits are limited, and that limit has been reached, - // cut off the one extra digit, and depending on its value - // round the remaining ones. - if limit_digits && dig == digit_count { - let ascii2value = |chr: u8| { - (chr as char).to_digit(radix).unwrap() - }; - let value2ascii = |val: u32| { - char::from_digit(val, radix).unwrap() as u8 - }; - - let extra_digit = ascii2value(buf.pop().unwrap()); - if extra_digit >= radix / 2 { // -> need to round - let mut i: isize = buf.len() as isize - 1; - loop { - // If reached left end of number, have to - // insert additional digit: - if i < 0 - || buf[i as usize] == b'-' - || buf[i as usize] == b'+' { - buf.insert((i + 1) as usize, value2ascii(1)); - break; - } - - // Skip the '.' - if buf[i as usize] == b'.' { i -= 1; continue; } - - // Either increment the digit, - // or set to 0 if max and carry the 1. - let current_digit = ascii2value(buf[i as usize]); - if current_digit < (radix - 1) { - buf[i as usize] = value2ascii(current_digit+1); - break; - } else { - buf[i as usize] = value2ascii(0); - i -= 1; - } - } - } - } - } - - // if number of digits is not exact, remove all trailing '0's up to - // and including the '.' - if !exact { - let buf_max_i = buf.len() - 1; - - // index to truncate from - let mut i = buf_max_i; - - // discover trailing zeros of fractional part - while i > start_fractional_digits && buf[i] == b'0' { - i -= 1; - } - - // Only attempt to truncate digits if buf has fractional digits - if i >= start_fractional_digits { - // If buf ends with '.', cut that too. - if buf[i] == b'.' { i -= 1 } - - // only resize buf if we actually remove digits - if i < buf_max_i { - buf = buf[.. (i + 1)].to_vec(); - } - } - } // If exact and trailing '.', just cut that - else { - let max_i = buf.len() - 1; - if buf[max_i] == b'.' { - buf = buf[.. max_i].to_vec(); - } - } - - match exp_format { - ExpNone => (), - _ => { - buf.push(match exp_format { - ExpDec if exp_upper => 'E', - ExpDec if !exp_upper => 'e', - ExpBin if exp_upper => 'P', - ExpBin if !exp_upper => 'p', - _ => unreachable!() - } as u8); - - int_to_str_bytes_common(exp, 10, sign, |c| buf.push(c)); - } - } - - (buf, false) -} - -/// Converts a number to its string representation. This is a wrapper for -/// `to_str_bytes_common()`, for details see there. -#[inline] -pub fn float_to_str_common<T: Float>( - num: T, radix: u32, negative_zero: bool, - sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_capital: bool - ) -> (String, bool) { - let (bytes, special) = float_to_str_bytes_common(num, radix, - negative_zero, sign, digits, exp_format, exp_capital); - (String::from_utf8(bytes).unwrap(), special) -} - -// Some constants for from_str_bytes_common's input validation, -// they define minimum radix values for which the character is a valid digit. -const DIGIT_P_RADIX: u32 = ('p' as u32) - ('a' as u32) + 11; -const DIGIT_E_RADIX: u32 = ('e' as u32) - ('a' as u32) + 11; - -#[cfg(test)] -mod tests { - use core::num::wrapping::WrappingOps; - use string::ToString; - - #[test] - fn test_int_to_str_overflow() { - let mut i8_val: i8 = 127; - assert_eq!(i8_val.to_string(), "127"); - - i8_val = i8_val.wrapping_add(1); - assert_eq!(i8_val.to_string(), "-128"); - - let mut i16_val: i16 = 32_767; - assert_eq!(i16_val.to_string(), "32767"); - - i16_val = i16_val.wrapping_add(1); - assert_eq!(i16_val.to_string(), "-32768"); - - let mut i32_val: i32 = 2_147_483_647; - assert_eq!(i32_val.to_string(), "2147483647"); - - i32_val = i32_val.wrapping_add(1); - assert_eq!(i32_val.to_string(), "-2147483648"); - - let mut i64_val: i64 = 9_223_372_036_854_775_807; - assert_eq!(i64_val.to_string(), "9223372036854775807"); - - i64_val = i64_val.wrapping_add(1); - assert_eq!(i64_val.to_string(), "-9223372036854775808"); - } -} - -#[cfg(test)] -mod bench { - #![allow(deprecated)] // rand - extern crate test; - - mod usize { - use super::test::Bencher; - use rand::{thread_rng, Rng}; - use std::fmt; - - #[inline] - fn to_string(x: usize, base: u8) { - format!("{}", fmt::radix(x, base)); - } - - #[bench] - fn to_str_bin(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<usize>(), 2); }) - } - - #[bench] - fn to_str_oct(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<usize>(), 8); }) - } - - #[bench] - fn to_str_dec(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<usize>(), 10); }) - } - - #[bench] - fn to_str_hex(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<usize>(), 16); }) - } - - #[bench] - fn to_str_base_36(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<usize>(), 36); }) - } - } - - mod isize { - use super::test::Bencher; - use rand::{thread_rng, Rng}; - use std::fmt; - - #[inline] - fn to_string(x: isize, base: u8) { - format!("{}", fmt::radix(x, base)); - } - - #[bench] - fn to_str_bin(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<isize>(), 2); }) - } - - #[bench] - fn to_str_oct(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<isize>(), 8); }) - } - - #[bench] - fn to_str_dec(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<isize>(), 10); }) - } - - #[bench] - fn to_str_hex(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<isize>(), 16); }) - } - - #[bench] - fn to_str_base_36(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { to_string(rng.gen::<isize>(), 36); }) - } - } - - mod f64 { - use super::test::Bencher; - use rand::{thread_rng, Rng}; - use f64; - - #[bench] - fn float_to_string(b: &mut Bencher) { - let mut rng = thread_rng(); - b.iter(|| { f64::to_string(rng.gen()); }) - } - } -} diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index c9e6a8f66d1..96b0ba1c77f 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -12,12 +12,11 @@ #![doc(hidden)] #![allow(unsigned_negation)] -macro_rules! uint_module { ($T:ty) => ( +macro_rules! uint_module { ($T:ident) => ( #[cfg(test)] mod tests { use prelude::v1::*; - use num::FromStrRadix; fn from_str<T: ::str::FromStr>(t: &str) -> Option<T> { ::str::FromStr::from_str(t).ok() @@ -38,15 +37,15 @@ mod tests { #[test] pub fn test_parse_bytes() { - assert_eq!(FromStrRadix::from_str_radix("123", 10), Ok(123 as $T)); - assert_eq!(FromStrRadix::from_str_radix("1001", 2), Ok(9 as $T)); - assert_eq!(FromStrRadix::from_str_radix("123", 8), Ok(83 as $T)); - assert_eq!(FromStrRadix::from_str_radix("123", 16), Ok(291 as u16)); - assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Ok(65535 as u16)); - assert_eq!(FromStrRadix::from_str_radix("z", 36), Ok(35 as $T)); - - assert_eq!(FromStrRadix::from_str_radix("Z", 10).ok(), None::<$T>); - assert_eq!(FromStrRadix::from_str_radix("_", 2).ok(), None::<$T>); + assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T)); + assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T)); + assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T)); + assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16)); + assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16)); + assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T)); + + assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>); + assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>); } } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 26aaa63aabb..e8052041aeb 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -110,7 +110,7 @@ use string::String; use vec::Vec; use fmt; -use ffi::{OsStr, OsString, AsOsStr}; +use ffi::{OsStr, OsString}; use self::platform::{is_sep_byte, is_verbatim_sep, MAIN_SEP_STR, parse_prefix}; @@ -1185,14 +1185,6 @@ impl AsRef<OsStr> for PathBuf { } #[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl AsOsStr for PathBuf { - fn as_os_str(&self) -> &OsStr { - &self.inner[..] - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl Into<OsString> for PathBuf { fn into(self) -> OsString { self.inner @@ -1653,14 +1645,6 @@ impl AsRef<OsStr> for Path { } #[stable(feature = "rust1", since = "1.0.0")] -#[deprecated(since = "1.0.0", reason = "trait is deprecated")] -impl AsOsStr for Path { - fn as_os_str(&self) -> &OsStr { - &self.inner - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Path { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.inner.fmt(formatter) @@ -1711,22 +1695,6 @@ impl cmp::Ord for Path { } } -/// Freely convertible to a `Path`. -#[unstable(feature = "std_misc")] -#[deprecated(since = "1.0.0", reason = "use std::convert::AsRef<Path> instead")] -pub trait AsPath { - /// Converts to a `Path`. - #[unstable(feature = "std_misc")] - fn as_path(&self) -> &Path; -} - -#[unstable(feature = "std_misc")] -#[deprecated(since = "1.0.0", reason = "use std::convert::AsRef<Path> instead")] -#[allow(deprecated)] -impl<T: AsOsStr + ?Sized> AsPath for T { - fn as_path(&self) -> &Path { Path::new(self.as_os_str()) } -} - #[stable(feature = "rust1", since = "1.0.0")] impl AsRef<Path> for Path { fn as_ref(&self) -> &Path { self } diff --git a/src/libstd/rand/reader.rs b/src/libstd/rand/reader.rs index f8dd6a00c7f..2837bac4456 100644 --- a/src/libstd/rand/reader.rs +++ b/src/libstd/rand/reader.rs @@ -67,7 +67,6 @@ mod test { use prelude::v1::*; use super::ReaderRng; - use num::Int; use rand::Rng; #[test] @@ -78,18 +77,18 @@ mod test { 0, 0, 0, 0, 0, 0, 0, 3][..]; let mut rng = ReaderRng::new(v); - assert_eq!(rng.next_u64(), 1.to_be()); - assert_eq!(rng.next_u64(), 2.to_be()); - assert_eq!(rng.next_u64(), 3.to_be()); + assert_eq!(rng.next_u64(), 1u64.to_be()); + assert_eq!(rng.next_u64(), 2u64.to_be()); + assert_eq!(rng.next_u64(), 3u64.to_be()); } #[test] fn test_reader_rng_u32() { let v = &[0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3][..]; let mut rng = ReaderRng::new(v); - assert_eq!(rng.next_u32(), 1.to_be()); - assert_eq!(rng.next_u32(), 2.to_be()); - assert_eq!(rng.next_u32(), 3.to_be()); + assert_eq!(rng.next_u32(), 1u32.to_be()); + assert_eq!(rng.next_u32(), 2u32.to_be()); + assert_eq!(rng.next_u32(), 3u32.to_be()); } #[test] fn test_reader_rng_fill_bytes() { diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index fcb0d2c0b2d..c2964b7a4f1 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -161,14 +161,6 @@ impl Condvar { } } - /// Deprecated: use `wait_timeout_ms` instead. - #[unstable(feature = "std_misc")] - #[deprecated(since = "1.0.0", reason = "use wait_timeout_ms instead")] - pub fn wait_timeout<'a, T>(&self, guard: MutexGuard<'a, T>, dur: Duration) - -> LockResult<(MutexGuard<'a, T>, bool)> { - self.wait_timeout_ms(guard, dur.num_milliseconds() as u32) - } - /// Waits on this condition variable for a notification, timing out after a /// specified duration. /// diff --git a/src/libstd/sys/common/wtf8.rs b/src/libstd/sys/common/wtf8.rs index c44bf08cae7..56a952e6a7e 100644 --- a/src/libstd/sys/common/wtf8.rs +++ b/src/libstd/sys/common/wtf8.rs @@ -37,8 +37,6 @@ use fmt; use hash::{Hash, Hasher}; use iter::FromIterator; use mem; -#[allow(deprecated)] // Int -use num::Int; use ops; use slice; use str; diff --git a/src/libstd/sys/unix/condvar.rs b/src/libstd/sys/unix/condvar.rs index 90dfebc4c45..ed6382e000a 100644 --- a/src/libstd/sys/unix/condvar.rs +++ b/src/libstd/sys/unix/condvar.rs @@ -17,7 +17,6 @@ use sys::mutex::{self, Mutex}; use sys::time; use sys::sync as ffi; use time::Duration; -use num::{Int, NumCast}; pub struct Condvar { inner: UnsafeCell<ffi::pthread_cond_t> } @@ -70,8 +69,8 @@ impl Condvar { let r = ffi::gettimeofday(&mut sys_now, ptr::null_mut()); debug_assert_eq!(r, 0); - let seconds = NumCast::from(dur.num_seconds()); - let timeout = match seconds.and_then(|s| sys_now.tv_sec.checked_add(s)) { + let seconds = dur.num_seconds() as libc::time_t; + let timeout = match sys_now.tv_sec.checked_add(seconds) { Some(sec) => { libc::timespec { tv_sec: sec, @@ -81,7 +80,7 @@ impl Condvar { } None => { libc::timespec { - tv_sec: Int::max_value(), + tv_sec: <libc::time_t>::max_value(), tv_nsec: 1_000_000_000 - 1, } } diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index a8a6219f398..d99753a6a4c 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -15,7 +15,8 @@ use prelude::v1::*; use io::{self, ErrorKind}; use libc; -use num::{Int, SignedInt}; +use num::One; +use ops::Neg; pub mod backtrace; pub mod c; @@ -63,23 +64,8 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind { } } -#[inline] -#[allow(deprecated)] -pub fn retry<T, F> (mut f: F) -> T where - T: SignedInt, - F: FnMut() -> T, -{ - let one: T = Int::one(); - loop { - let n = f(); - if n == -one && os::errno() == libc::EINTR as i32 { } - else { return n } - } -} - -#[allow(deprecated)] -pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> { - let one: T = Int::one(); +pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> { + let one: T = T::one(); if t == -one { Err(io::Error::last_os_error()) } else { @@ -89,7 +75,7 @@ pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> { #[allow(deprecated)] pub fn cvt_r<T, F>(mut f: F) -> io::Result<T> - where T: SignedInt, F: FnMut() -> T + where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T { loop { match cvt(f()) { diff --git a/src/libstd/sys/unix/process2.rs b/src/libstd/sys/unix/process2.rs index caa7b4eb29c..4e7c4d241f5 100644 --- a/src/libstd/sys/unix/process2.rs +++ b/src/libstd/sys/unix/process2.rs @@ -19,7 +19,7 @@ use io::{self, Error, ErrorKind}; use libc::{self, pid_t, c_void, c_int, gid_t, uid_t}; use ptr; use sys::pipe2::AnonPipe; -use sys::{self, retry, c, cvt}; +use sys::{self, c, cvt, cvt_r}; use sys::fs2::{File, OpenOptions}; //////////////////////////////////////////////////////////////////////////////// @@ -273,7 +273,7 @@ impl Process { } } }; - retry(|| libc::dup2(fd.raw(), dst)) != -1 + cvt_r(|| libc::dup2(fd.raw(), dst)).is_ok() }; if !setup(in_fd, libc::STDIN_FILENO) { fail(&mut output) } @@ -317,19 +317,19 @@ impl Process { pub fn wait(&self) -> io::Result<ExitStatus> { let mut status = 0 as c_int; - try!(cvt(retry(|| unsafe { c::waitpid(self.pid, &mut status, 0) }))); + try!(cvt_r(|| unsafe { c::waitpid(self.pid, &mut status, 0) })); Ok(translate_status(status)) } pub fn try_wait(&self) -> Option<ExitStatus> { let mut status = 0 as c_int; - match retry(|| unsafe { + match cvt_r(|| unsafe { c::waitpid(self.pid, &mut status, c::WNOHANG) }) { - n if n == self.pid => Some(translate_status(status)), - 0 => None, - n => panic!("unknown waitpid error `{}`: {}", n, - io::Error::last_os_error()), + Ok(0) => None, + Ok(n) if n == self.pid => Some(translate_status(status)), + Ok(n) => panic!("unkown pid: {}", n), + Err(e) => panic!("unknown waitpid error: {}", e), } } } diff --git a/src/libstd/sys/windows/fs2.rs b/src/libstd/sys/windows/fs2.rs index 07612d125e1..af61eec319c 100644 --- a/src/libstd/sys/windows/fs2.rs +++ b/src/libstd/sys/windows/fs2.rs @@ -13,7 +13,7 @@ use io::prelude::*; use os::windows::prelude::*; use default::Default; -use ffi::{OsString, AsOsStr}; +use ffi::OsString; use fmt; use io::{self, Error, SeekFrom}; use libc::{self, HANDLE}; diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs index 1171c6c068b..5ae5f6f201b 100644 --- a/src/libstd/sys/windows/mod.rs +++ b/src/libstd/sys/windows/mod.rs @@ -17,8 +17,7 @@ use prelude::v1::*; use ffi::{OsStr, OsString}; use io::{self, ErrorKind}; use libc; -#[allow(deprecated)] -use num::Int; +use num::Zero; use os::windows::ffi::{OsStrExt, OsStringExt}; use path::PathBuf; @@ -144,9 +143,8 @@ pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { } } -#[allow(deprecated)] -fn cvt<I: Int>(i: I) -> io::Result<I> { - if i == Int::zero() { +fn cvt<I: PartialEq + Zero>(i: I) -> io::Result<I> { + if i == I::zero() { Err(io::Error::last_os_error()) } else { Ok(i) diff --git a/src/libstd/sys/windows/net.rs b/src/libstd/sys/windows/net.rs index cbc3876dbb1..6bbcd968157 100644 --- a/src/libstd/sys/windows/net.rs +++ b/src/libstd/sys/windows/net.rs @@ -15,8 +15,8 @@ use libc::consts::os::extra::INVALID_SOCKET; use libc::{self, c_int, c_void}; use mem; use net::SocketAddr; -#[allow(deprecated)] -use num::{SignedInt, Int}; +use num::One; +use ops::Neg; use rt; use sync::{Once, ONCE_INIT}; use sys::c; @@ -49,11 +49,8 @@ fn last_error() -> io::Error { /// Checks if the signed integer is the Windows constant `SOCKET_ERROR` (-1) /// and if so, returns the last error from the Windows socket interface. . This /// function must be called before another call to the socket API is made. -/// -/// FIXME: generics needed? -#[allow(deprecated)] -pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> { - let one: T = Int::one(); +pub fn cvt<T: One + Neg<Output=T> + PartialEq>(t: T) -> io::Result<T> { + let one: T = T::one(); if t == -one { Err(last_error()) } else { @@ -70,7 +67,9 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> { /// Provides the functionality of `cvt` for a closure. #[allow(deprecated)] -pub fn cvt_r<T: SignedInt, F>(mut f: F) -> io::Result<T> where F: FnMut() -> T { +pub fn cvt_r<T, F>(mut f: F) -> io::Result<T> + where F: FnMut() -> T, T: One + Neg<Output=T> + PartialEq +{ cvt(f()) } diff --git a/src/libstd/sys/windows/process2.rs b/src/libstd/sys/windows/process2.rs index 16c2a9125ea..5ddcf3d1ea2 100644 --- a/src/libstd/sys/windows/process2.rs +++ b/src/libstd/sys/windows/process2.rs @@ -140,7 +140,7 @@ impl Process { // read the *child's* PATH if one is provided. See #15149 for more details. let program = cfg.env.as_ref().and_then(|env| { for (key, v) in env { - if OsStr::from_str("PATH") != &**key { continue } + if OsStr::new("PATH") != &**key { continue } // Split the value and test each path to see if the // program exists. @@ -463,7 +463,7 @@ mod tests { fn test_make_command_line() { fn test_wrapper(prog: &str, args: &[&str]) -> String { String::from_utf16( - &make_command_line(OsStr::from_str(prog), + &make_command_line(OsStr::new(prog), &args.iter() .map(|a| OsString::from(a)) .collect::<Vec<OsString>>())).unwrap() diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 4fd0340f09a..c65377e238f 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -497,15 +497,6 @@ pub fn sleep_ms(ms: u32) { imp::sleep(Duration::milliseconds(ms as i64)) } -/// Deprecated: use `sleep_ms` instead. -#[unstable(feature = "thread_sleep", - reason = "recently added, needs an RFC, and `Duration` itself is \ - unstable")] -#[deprecated(since = "1.0.0", reason = "use sleep_ms instead")] -pub fn sleep(dur: Duration) { - imp::sleep(dur) -} - /// Blocks unless or until the current thread's token is made available (may wake spuriously). /// /// See the module doc for more detail. @@ -546,13 +537,6 @@ pub fn park_timeout_ms(ms: u32) { *guard = false; } -/// Deprecated: use `park_timeout_ms` -#[unstable(feature = "std_misc", reason = "recently introduced, depends on Duration")] -#[deprecated(since = "1.0.0", reason = "use park_timeout_ms instead")] -pub fn park_timeout(duration: Duration) { - park_timeout_ms(duration.num_milliseconds() as u32) -} - //////////////////////////////////////////////////////////////////////////////// // Thread //////////////////////////////////////////////////////////////////////////////// diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 9b79b483b28..636a0dd697a 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -12,13 +12,10 @@ #![unstable(feature = "std_misc")] +use prelude::v1::*; + use {fmt, i64}; -use ops::{Add, Sub, Mul, Div, Neg, FnOnce}; -use option::Option; -use option::Option::{Some, None}; -#[allow(deprecated)] // Int -use num::Int; -use result::Result::Ok; +use ops::{Add, Sub, Mul, Div, Neg}; /// The number of nanoseconds in a microsecond. const NANOS_PER_MICRO: i32 = 1000; |
