diff --git a/Cargo.toml b/Cargo.toml index 44f57e9..672b4c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ name = "num-traits" version = "0.2.6" readme = "README.md" build = "build.rs" +exclude = ["/ci/*", "/.travis.yml", "/bors.toml"] [package.metadata.docs.rs] features = ["std"] diff --git a/README.md b/README.md index d2ea0d0..949793d 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,7 @@ # num-traits -[![crate](https://img.shields.io/crates/v/num-traits.svg)](https://crates.io/crates/num-traits) -[![documentation](https://docs.rs/num-traits/badge.svg)](https://docs.rs/num-traits) -![minimum rustc 1.8](https://img.shields.io/badge/rustc-1.8+-red.svg) -[![Travis status](https://travis-ci.org/rust-num/num-traits.svg?branch=master)](https://travis-ci.org/rust-num/num-traits) - -Numeric traits for generic mathematics in Rust. - -## Usage - -Add this to your `Cargo.toml`: - -```toml -[dependencies] -num-traits = "0.2" -``` - -and this to your crate root: - -```rust -extern crate num_traits; -``` - -## Features - -This crate can be used without the standard library (`#![no_std]`) by disabling -the default `std` feature. Use this in `Cargo.toml`: - -```toml -[dependencies.num-traits] -version = "0.2" -default-features = false -``` - -The `Float` and `Real` traits are only available when `std` is enabled. The -`FloatCore` trait is always available. `MulAdd` and `MulAddAssign` for `f32` -and `f64` also require `std`, as do implementations of signed and floating- -point exponents in `Pow`. - -Implementations for `i128` and `u128` are only available with Rust 1.26 and -later. The build script automatically detects this, but you can make it -mandatory by enabling the `i128` crate feature. - -## Releases - -Release notes are available in [RELEASES.md](RELEASES.md). +4y's num-traits fork (for the Horse Shoe library only). ## Compatibility -The `num-traits` crate is tested for rustc 1.8 and greater. +Only for the [Horse Shoe](https://github.com/YakoYakoYokuYoku/Horse-Shoe) library. diff --git a/src/float.rs b/src/float.rs index c91233c..3094dd4 100644 --- a/src/float.rs +++ b/src/float.rs @@ -895,7 +895,8 @@ impl FloatCore for f64 { /// /// This trait is only available with the `std` feature. #[cfg(feature = "std")] -pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { +pub trait Float where Self: Num + Copy + NumCast + Neg { + type Typo; /// Returns the `NaN` value. /// /// ``` @@ -1779,6 +1780,42 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { /// ``` fn atanh(self) -> Self; + /// Returns the real part of the float. + /// + /// ``` + /// use num_traits::Float; + /// + /// let n = 0.5f64; + /// + /// assert!(n.real() > 0.4f64); + /// ``` + #[inline] + fn real(self) -> Self::Typo; + + /// Returns the imaginary part of the float which equals to zero. + /// + /// ``` + /// use num_traits::Float; + /// + /// let n = 2.7f64; + /// + /// assert!(n.imag() == 0.0f64); + /// ``` + #[inline] + fn imag(self) -> Self::Typo; + + /// Computes the argument of the float.Float + /// + /// ``` + /// use num_traits::Float; + /// + /// let n = 0.8f32; + /// + /// assert_eq!(n.arg(), 0.0f32); + /// ``` + #[inline] + fn arg(self) -> Self::Typo; + /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. /// @@ -1805,6 +1842,7 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { macro_rules! float_impl { ($T:ident $decode:ident) => { impl Float for $T { + type Typo = $T; constant! { nan() -> $T::NAN; infinity() -> $T::INFINITY; @@ -1827,6 +1865,25 @@ macro_rules! float_impl { $decode(self) } + #[inline] + fn real(self) -> Self::Typo { + self + } + + #[inline] + fn imag(self) -> Self::Typo { + 0.0 + } + + #[inline] + fn arg(self) -> Self::Typo { + if self >= 0.0 { + 0.0 + } else { + 1.0.asin() * 2.0 + } + } + forward! { Self::is_nan(self) -> bool; Self::is_infinite(self) -> bool; @@ -2021,4 +2078,11 @@ mod tests { 57.2957795130823208767981548141051703 ); } + + #[test] + fn realret() { + use float::Float; + + assert_eq!(Float::imag(0.5f64), 0.0f64); + } } diff --git a/src/lib.rs b/src/lib.rs index 5a25bab..172e714 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,13 +90,12 @@ pub trait NumOps: { } -impl NumOps for T -where +impl NumOps for T where T: Add + Sub + Mul + Div - + Rem, + + Rem { } @@ -105,22 +104,14 @@ where /// /// This is automatically implemented for types which implement the operators. pub trait NumRef: Num + for<'r> NumOps<&'r Self> {} -impl NumRef for T -where - T: Num + for<'r> NumOps<&'r T>, -{ -} +impl NumRef for T where T: Num + for<'r> NumOps<&'r T> {} /// The trait for references which implement numeric operations, taking the /// second operand either by value or by reference. /// /// This is automatically implemented for types which implement the operators. pub trait RefNum: NumOps + for<'r> NumOps<&'r Base, Base> {} -impl RefNum for T -where - T: NumOps + for<'r> NumOps<&'r Base, Base>, -{ -} +impl RefNum for T where T: NumOps + for<'r> NumOps<&'r Base, Base> {} /// The trait for types implementing numeric assignment operators (like `+=`). /// @@ -130,9 +121,8 @@ pub trait NumAssignOps: { } -impl NumAssignOps for T -where - T: AddAssign + SubAssign + MulAssign + DivAssign + RemAssign, +impl NumAssignOps for T where + T: AddAssign + SubAssign + MulAssign + DivAssign + RemAssign { } @@ -140,22 +130,14 @@ where /// /// This is automatically implemented for types which implement the operators. pub trait NumAssign: Num + NumAssignOps {} -impl NumAssign for T -where - T: Num + NumAssignOps, -{ -} +impl NumAssign for T where T: Num + NumAssignOps {} /// The trait for `NumAssign` types which also implement assignment operations /// taking the second operand by reference. /// /// This is automatically implemented for types which implement the operators. pub trait NumAssignRef: NumAssign + for<'r> NumAssignOps<&'r Self> {} -impl NumAssignRef for T -where - T: NumAssign + for<'r> NumAssignOps<&'r T>, -{ -} +impl NumAssignRef for T where T: NumAssign + for<'r> NumAssignOps<&'r T> {} macro_rules! int_trait_impl { ($name:ident for $($t:ty)*) => ($( diff --git a/src/real.rs b/src/real.rs index 23ac6b8..f891d0f 100644 --- a/src/real.rs +++ b/src/real.rs @@ -13,6 +13,7 @@ use {Float, Num, NumCast}; /// /// This trait is only available with the `std` feature. pub trait Real: Num + Copy + NumCast + PartialOrd + Neg { + type Typo; /// Returns the smallest finite value that this type can represent. /// /// ``` @@ -775,9 +776,61 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg { /// assert!(abs_difference < 1.0e-10); /// ``` fn atanh(self) -> Self; + + /// Returns the real part of the float. + /// + /// ``` + /// use num_traits::Float; + /// + /// let n = 0.5f64; + /// + /// assert!(n.real() > 0.4f64); + /// ``` + fn real(self) -> Self::Typo; + + /// Returns the imaginary part of the float which equals to zero. + /// + /// ``` + /// use num_traits::Float; + /// + /// let n = 2.7f64; + /// + /// assert!(n.imag() == 0.0f64); + /// ``` + fn imag(self) -> Self::Typo; + + /// Computes the argument of the float.Float + /// + /// ``` + /// use num_traits::Float; + /// + /// let n = 0.8f32; + /// + /// assert_eq!(n.arg(), 0.0f32); + /// ``` + fn arg(self) -> Self::Typo; } -impl Real for T { +impl Real for T { + type Typo = T; + #[inline] + fn real(self) -> T { + self + } + #[inline] + fn imag(self) -> T { + T::neg_zero() + } + + #[inline] + fn arg(self) -> Self::Typo { + if self >= T::from(0).unwrap() { + T::from(0).unwrap() + } else { + T::from(3.14159265358979323846264338327950288_f64).unwrap() + } + } + forward! { Float::min_value() -> Self; Float::min_positive_value() -> Self; diff --git a/src/sign.rs b/src/sign.rs index cb1d9fb..26d44c5 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -206,11 +206,7 @@ empty_trait_impl!(Unsigned for usize u8 u16 u32 u64); #[cfg(has_i128)] empty_trait_impl!(Unsigned for u128); -impl Unsigned for Wrapping -where - Wrapping: Num, -{ -} +impl Unsigned for Wrapping where Wrapping: Num {} #[test] fn unsigned_wrapping_is_unsigned() {