Remove Signed trait and add SignedInt trait

The methods have been moved into Float and SignedInt
This commit is contained in:
Brendan Zabarauskas 2014-11-13 00:02:42 +11:00
parent e965ba85ca
commit de938b6ca1
26 changed files with 179 additions and 130 deletions

View File

@ -97,7 +97,7 @@ syn keyword rustTrait FromIterator IntoIterator Extend ExactSize
syn keyword rustTrait Iterator DoubleEndedIterator
syn keyword rustTrait RandomAccessIterator CloneableIterator
syn keyword rustTrait OrdIterator MutableDoubleEndedIterator
syn keyword rustTrait NumCast Signed Int UnsignedInt Float
syn keyword rustTrait NumCast Int SignedInt UnsignedInt Float
syn keyword rustTrait FloatMath ToPrimitive FromPrimitive
syn keyword rustTrait Box
syn keyword rustTrait GenericPath Path PosixPath WindowsPath

View File

@ -19,6 +19,8 @@
//! operators, you could do the following:
//!
//! ```rust
//! use core::num::SignedInt;
//!
//! // Our type.
//! struct SketchyNum {
//! num : int

View File

@ -620,7 +620,7 @@ impl<'a, T> Pointer for &'a mut T {
macro_rules! floating(($ty:ident) => {
impl Float for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
use num::{Float, Signed};
use num::Float;
let digits = match fmt.precision {
Some(i) => float::DigExact(i),
@ -641,7 +641,7 @@ macro_rules! floating(($ty:ident) => {
impl LowerExp for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
use num::{Float, Signed};
use num::Float;
let digits = match fmt.precision {
Some(i) => float::DigExact(i),
@ -662,7 +662,7 @@ macro_rules! floating(($ty:ident) => {
impl UpperExp for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
use num::{Float, Signed};
use num::Float;
let digits = match fmt.precision {
Some(i) => float::DigExact(i),

View File

@ -573,6 +573,8 @@ pub trait Iterator<A> {
/// # Example
///
/// ```rust
/// use core::num::SignedInt;
///
/// let xs = [-3i, 0, 1, 5, -10];
/// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
/// ```
@ -597,6 +599,8 @@ pub trait Iterator<A> {
/// # Example
///
/// ```rust
/// use core::num::SignedInt;
///
/// let xs = [-3i, 0, 1, 5, -10];
/// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
/// ```

View File

@ -242,6 +242,41 @@ impl Float for f32 {
#[inline]
fn fract(self) -> f32 { self - self.trunc() }
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
#[inline]
fn abs(self) -> f32 {
unsafe { intrinsics::fabsf32(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()`
#[inline]
fn signum(self) -> f32 {
if self.is_nan() {
Float::nan()
} else {
unsafe { intrinsics::copysignf32(1.0, self) }
}
}
/// Returns `true` if `self` is positive, including `+0.0` and
/// `Float::infinity()`.
#[inline]
fn is_positive(self) -> bool {
self > 0.0 || (1.0 / self) == Float::infinity()
}
/// Returns `true` if `self` is negative, including `-0.0` and
/// `Float::neg_infinity()`.
#[inline]
fn is_negative(self) -> bool {
self < 0.0 || (1.0 / self) == Float::neg_infinity()
}
/// 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.
@ -254,6 +289,7 @@ impl Float for f32 {
#[inline]
fn recip(self) -> f32 { 1.0 / self }
#[inline]
fn powi(self, n: i32) -> f32 {
unsafe { intrinsics::powif32(self, n) }
}

View File

@ -248,6 +248,41 @@ impl Float for f64 {
#[inline]
fn fract(self) -> f64 { self - self.trunc() }
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
#[inline]
fn abs(self) -> f64 {
unsafe { intrinsics::fabsf64(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()`
#[inline]
fn signum(self) -> f64 {
if self.is_nan() {
Float::nan()
} else {
unsafe { intrinsics::copysignf64(1.0, self) }
}
}
/// Returns `true` if `self` is positive, including `+0.0` and
/// `Float::infinity()`.
#[inline]
fn is_positive(self) -> bool {
self > 0.0 || (1.0 / self) == Float::infinity()
}
/// Returns `true` if `self` is negative, including `-0.0` and
/// `Float::neg_infinity()`.
#[inline]
fn is_negative(self) -> bool {
self < 0.0 || (1.0 / self) == Float::neg_infinity()
}
/// 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.

View File

@ -13,6 +13,7 @@
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
use num::Float;
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);

View File

@ -33,113 +33,6 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
(x / y, x % y)
}
/// A built-in signed number.
pub trait Signed: Neg<Self> {
/// Computes the absolute value of `self`.
fn abs(self) -> Self;
/// Returns a number (either `-1`, `0` or `1`) representing sign of `self`.
fn signum(self) -> Self;
/// Returns `true` if `self` is a positive value.
fn is_positive(self) -> bool;
/// Returns `true` if `self` is a negative value.
fn is_negative(self) -> bool;
}
macro_rules! signed_int_impl {
($T:ty) => {
impl Signed for $T {
/// Computes the absolute value. `Int::min_value()` will be returned
/// if the number is `Int::min_value()`.
#[inline]
fn abs(self) -> $T {
if self.is_negative() { -self } else { self }
}
/// # Returns
///
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
#[inline]
fn signum(self) -> $T {
match self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
}
/// Returns `true` if `self` is positive and `false` if the number
/// is zero or negative.
#[inline]
fn is_positive(self) -> bool { self > 0 }
/// Returns `true` if `self` is negative and `false` if the number
/// is zero or positive.
#[inline]
fn is_negative(self) -> bool { self < 0 }
}
}
}
signed_int_impl!(i8)
signed_int_impl!(i16)
signed_int_impl!(i32)
signed_int_impl!(i64)
signed_int_impl!(int)
macro_rules! signed_float_impl {
($T:ty, $fabs:path, $fcopysign:path) => {
impl Signed for $T {
/// Computes the absolute value. Returns `Float::nan()` if the
/// number is `Float::nan()`.
#[inline]
fn abs(self) -> $T {
unsafe { $fabs(self) }
}
/// # Returns
///
/// - `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()`
#[inline]
fn signum(self) -> $T {
if self.is_nan() {
Float::nan()
} else {
unsafe { $fcopysign(1.0, self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and
/// `Float::infinity()`.
#[inline]
fn is_positive(self) -> bool {
self > 0.0 || (1.0 / self) == Float::infinity()
}
/// Returns `true` if the number is negative, including `-0.0` and
/// `Float::neg_infinity()`.
#[inline]
fn is_negative(self) -> bool {
self < 0.0 || (1.0 / self) == Float::neg_infinity()
}
}
};
}
signed_float_impl!(f32,
intrinsics::fabsf32,
intrinsics::copysignf32)
signed_float_impl!(f64,
intrinsics::fabsf64,
intrinsics::copysignf64)
/// Raises a `base` to the power of `exp`, using exponentiation by squaring.
///
/// # Example
@ -702,6 +595,63 @@ int_impl!(int = i64, u64, 64,
intrinsics::i64_sub_with_overflow,
intrinsics::i64_mul_with_overflow)
/// A built-in two's complement integer.
pub trait SignedInt
: Int
+ Neg<Self>
{
/// Computes the absolute value of `self`. `Int::min_value()` will be
/// returned if the number is `Int::min_value()`.
fn abs(self) -> Self;
/// Returns a number representing sign of `self`.
///
/// - `0` if the number is zero
/// - `1` if the number is positive
/// - `-1` if the number is negative
fn signum(self) -> Self;
/// Returns `true` if `self` is positive and `false` if the number
/// is zero or negative.
fn is_positive(self) -> bool;
/// Returns `true` if `self` is negative and `false` if the number
/// is zero or positive.
fn is_negative(self) -> bool;
}
macro_rules! signed_int_impl {
($T:ty) => {
impl SignedInt for $T {
#[inline]
fn abs(self) -> $T {
if self.is_negative() { -self } else { self }
}
#[inline]
fn signum(self) -> $T {
match self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
}
#[inline]
fn is_positive(self) -> bool { self > 0 }
#[inline]
fn is_negative(self) -> bool { self < 0 }
}
}
}
signed_int_impl!(i8)
signed_int_impl!(i16)
signed_int_impl!(i32)
signed_int_impl!(i64)
signed_int_impl!(int)
/// A built-in unsigned integer.
pub trait UnsignedInt: Int {
/// Returns `true` iff `self == 2^k` for some `k`.
@ -1257,7 +1207,7 @@ pub trait Float
+ NumCast
+ PartialOrd
+ PartialEq
+ Signed
+ Neg<Self>
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
@ -1327,6 +1277,22 @@ pub trait Float
/// Return the fractional part of a number.
fn fract(self) -> Self;
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
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()`
fn signum(self) -> Self;
/// Returns `true` if `self` is positive, including `+0.0` and
/// `Float::infinity()`.
fn is_positive(self) -> bool;
/// Returns `true` if `self` is negative, including `-0.0` and
/// `Float::neg_infinity()`.
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.
@ -1494,14 +1460,6 @@ one_impl!(i64, 1i64)
one_impl!(f32, 1.0f32)
one_impl!(f64, 1.0f64)
#[deprecated = "Use `Signed::abs`"]
pub fn abs<T: Signed>(value: T) -> T {
value.abs()
}
#[deprecated = "Use `Signed::signum`"]
pub fn signum<T: Signed>(value: T) -> T {
value.signum()
}
#[deprecated = "Use `UnsignedInt::next_power_of_two`"]
pub fn next_power_of_two<T: UnsignedInt>(n: T) -> T {
n.next_power_of_two()

View File

@ -51,7 +51,7 @@ pub use cmp::{Ordering, Less, Equal, Greater, Equiv};
pub use iter::{FromIterator, Extend};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::{Signed, ToPrimitive, FromPrimitive};
pub use num::{ToPrimitive, FromPrimitive};
pub use option::{Option, Some, None};
pub use ptr::RawPtr;
pub use result::{Result, Ok, Err};

View File

@ -109,6 +109,8 @@ fn test_partial_max() {
#[test]
fn test_user_defined_eq() {
use core::num::SignedInt;
// Our type.
struct SketchyNum {
num : int

View File

@ -10,6 +10,7 @@
use core::iter::*;
use core::iter::order::*;
use core::num::SignedInt;
use core::uint;
use core::cmp;
use core::ops::Slice;

View File

@ -15,7 +15,7 @@ macro_rules! int_module (($T:ty, $T_i:ident) => (
mod tests {
use core::$T_i::*;
use core::int;
use core::num::Int;
use core::num::{Int, SignedInt};
use num;
#[test]

View File

@ -23,7 +23,7 @@ that do not need to record state.
#![experimental]
use core::prelude::*;
use core::num::Int;
use core::num::{Float, Int};
use {Rng, Rand};

View File

@ -37,6 +37,7 @@ use lint::{Context, LintPass, LintArray};
use std::cmp;
use std::collections::hash_map::{Occupied, Vacant};
use std::num::SignedInt;
use std::slice;
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
use syntax::abi;

View File

@ -14,6 +14,7 @@
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
use num::Float;
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);

View File

@ -23,9 +23,8 @@ use option::Option;
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
pub use core::num::{Num, div_rem, Zero, zero, One, one};
pub use core::num::{Signed, abs, signum};
pub use core::num::{Unsigned, pow, Bounded};
pub use core::num::{Primitive, Int, UnsignedInt};
pub use core::num::{Primitive, Int, SignedInt, UnsignedInt};
pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive};
pub use core::num::{next_power_of_two, is_power_of_two};
pub use core::num::{checked_next_power_of_two};

View File

@ -67,7 +67,7 @@
#[doc(no_inline)] pub use iter::{Iterator, DoubleEndedIterator};
#[doc(no_inline)] pub use iter::{RandomAccessIterator, CloneableIterator};
#[doc(no_inline)] pub use iter::{OrdIterator, MutableDoubleEndedIterator};
#[doc(no_inline)] pub use num::{Signed, ToPrimitive, FromPrimitive};
#[doc(no_inline)] pub use num::{ToPrimitive, FromPrimitive};
#[doc(no_inline)] pub use boxed::Box;
#[doc(no_inline)] pub use option::{Option, Some, None};
#[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath};

View File

@ -18,7 +18,7 @@
extern crate libc;
use num;
use num::Int;
use num::{Int, SignedInt};
use prelude::*;
use io::{mod, IoResult, IoError};
use sys_common::mkerr_libc;
@ -117,7 +117,7 @@ pub fn decode_error_detailed(errno: i32) -> IoError {
}
#[inline]
pub fn retry<T: Signed + Int> (f: || -> T) -> T {
pub fn retry<T: SignedInt> (f: || -> T) -> T {
let one: T = Int::one();
loop {
let n = f();

View File

@ -60,7 +60,7 @@ use std::io::fs::PathExtensions;
use std::io::stdio::StdWriter;
use std::io::{File, ChanReader, ChanWriter};
use std::io;
use std::num::{Int, FloatMath};
use std::num::{Float, FloatMath, Int};
use std::os;
use std::string::String;
use std::task::TaskBuilder;

View File

@ -461,6 +461,7 @@ mod tests {
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
use std::num::Float;
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);

View File

@ -30,6 +30,7 @@ extern crate libc;
use std::fmt::Show;
use std::fmt;
use std::io::BufReader;
use std::num::SignedInt;
use std::string::String;
use std::time::Duration;

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::num::SignedInt;
fn main() {
let _f = 10i.abs; //~ ERROR attempted to take value of method
}

View File

@ -12,6 +12,7 @@
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
use std::num::Float;
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);

View File

@ -13,6 +13,7 @@
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
use std::num::Float;
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);

View File

@ -10,6 +10,8 @@
// Test for issue #4183: use of Self in supertraits.
use std::num::Float as StdFloat;
pub static FUZZY_EPSILON: f64 = 0.1;
pub trait FuzzyEq<Eps> {

View File

@ -10,9 +10,10 @@
//
// ignore-lexer-test FIXME #15679
#![feature(non_ascii_idents)]
use std::num::Float;
pub fn main() {
let ε = 0.00001f64;
let Π = 3.14f64;