Merge #92
92: exclude CI files from crates.io r=cuviper a=ignatenkobrain Co-authored-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
This commit is contained in:
commit
6f9b7bb802
|
@ -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"]
|
||||
|
|
48
README.md
48
README.md
|
@ -1,51 +1,7 @@
|
|||
# num-traits
|
||||
|
||||
[](https://crates.io/crates/num-traits)
|
||||
[](https://docs.rs/num-traits)
|
||||

|
||||
[](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.
|
||||
|
|
66
src/float.rs
66
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<Output = Self> {
|
||||
pub trait Float where Self: Num + Copy + NumCast + Neg<Output = Self> {
|
||||
type Typo;
|
||||
/// Returns the `NaN` value.
|
||||
///
|
||||
/// ```
|
||||
|
@ -1779,6 +1780,42 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
|
|||
/// ```
|
||||
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<Output = Self> {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
34
src/lib.rs
34
src/lib.rs
|
@ -90,13 +90,12 @@ pub trait NumOps<Rhs = Self, Output = Self>:
|
|||
{
|
||||
}
|
||||
|
||||
impl<T, Rhs, Output> NumOps<Rhs, Output> for T
|
||||
where
|
||||
impl<T, Rhs, Output> NumOps<Rhs, Output> for T where
|
||||
T: Add<Rhs, Output = Output>
|
||||
+ Sub<Rhs, Output = Output>
|
||||
+ Mul<Rhs, Output = Output>
|
||||
+ Div<Rhs, Output = Output>
|
||||
+ Rem<Rhs, Output = Output>,
|
||||
+ Rem<Rhs, Output = Output>
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -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<T> NumRef for T
|
||||
where
|
||||
T: Num + for<'r> NumOps<&'r T>,
|
||||
{
|
||||
}
|
||||
impl<T> 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<Base>: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base> {}
|
||||
impl<T, Base> RefNum<Base> for T
|
||||
where
|
||||
T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base>,
|
||||
{
|
||||
}
|
||||
impl<T, Base> RefNum<Base> for T where T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base> {}
|
||||
|
||||
/// The trait for types implementing numeric assignment operators (like `+=`).
|
||||
///
|
||||
|
@ -130,9 +121,8 @@ pub trait NumAssignOps<Rhs = Self>:
|
|||
{
|
||||
}
|
||||
|
||||
impl<T, Rhs> NumAssignOps<Rhs> for T
|
||||
where
|
||||
T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,
|
||||
impl<T, Rhs> NumAssignOps<Rhs> for T where
|
||||
T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -140,22 +130,14 @@ where
|
|||
///
|
||||
/// This is automatically implemented for types which implement the operators.
|
||||
pub trait NumAssign: Num + NumAssignOps {}
|
||||
impl<T> NumAssign for T
|
||||
where
|
||||
T: Num + NumAssignOps,
|
||||
{
|
||||
}
|
||||
impl<T> 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<T> NumAssignRef for T
|
||||
where
|
||||
T: NumAssign + for<'r> NumAssignOps<&'r T>,
|
||||
{
|
||||
}
|
||||
impl<T> NumAssignRef for T where T: NumAssign + for<'r> NumAssignOps<&'r T> {}
|
||||
|
||||
macro_rules! int_trait_impl {
|
||||
($name:ident for $($t:ty)*) => ($(
|
||||
|
|
55
src/real.rs
55
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<Output = Self> {
|
||||
type Typo;
|
||||
/// Returns the smallest finite value that this type can represent.
|
||||
///
|
||||
/// ```
|
||||
|
@ -775,9 +776,61 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
|
|||
/// 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<T: Float> Real for T {
|
||||
impl<T: Float + PartialOrd> 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;
|
||||
|
|
|
@ -206,11 +206,7 @@ empty_trait_impl!(Unsigned for usize u8 u16 u32 u64);
|
|||
#[cfg(has_i128)]
|
||||
empty_trait_impl!(Unsigned for u128);
|
||||
|
||||
impl<T: Unsigned> Unsigned for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Num,
|
||||
{
|
||||
}
|
||||
impl<T: Unsigned> Unsigned for Wrapping<T> where Wrapping<T>: Num {}
|
||||
|
||||
#[test]
|
||||
fn unsigned_wrapping_is_unsigned() {
|
||||
|
|
Loading…
Reference in New Issue