Import "next" branch of rust-num/num
This commit is contained in:
parent
42a610d323
commit
0b1169c17b
|
@ -12,3 +12,7 @@ version = "0.1.41"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std"]
|
||||||
|
std = []
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{usize, u8, u16, u32, u64};
|
use core::{usize, u8, u16, u32, u64};
|
||||||
use std::{isize, i8, i16, i32, i64};
|
use core::{isize, i8, i16, i32, i64};
|
||||||
use std::{f32, f64};
|
use core::{f32, f64};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
/// Numbers which have upper and lower bounds
|
/// Numbers which have upper and lower bounds
|
||||||
pub trait Bounded {
|
pub trait Bounded {
|
||||||
|
|
13
src/cast.rs
13
src/cast.rs
|
@ -1,8 +1,9 @@
|
||||||
use std::mem::size_of;
|
use core::mem::size_of;
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
use identities::Zero;
|
use identities::Zero;
|
||||||
use bounds::Bounded;
|
use bounds::Bounded;
|
||||||
|
use float::Float;
|
||||||
|
|
||||||
/// A generic trait for converting a value to a number.
|
/// A generic trait for converting a value to a number.
|
||||||
pub trait ToPrimitive {
|
pub trait ToPrimitive {
|
||||||
|
@ -226,8 +227,8 @@ macro_rules! impl_to_primitive_float_to_float {
|
||||||
// Make sure the value is in range for the cast.
|
// Make sure the value is in range for the cast.
|
||||||
// NaN and +-inf are cast as they are.
|
// NaN and +-inf are cast as they are.
|
||||||
let n = $slf as f64;
|
let n = $slf as f64;
|
||||||
let max_value: $DstT = ::std::$DstT::MAX;
|
let max_value: $DstT = ::core::$DstT::MAX;
|
||||||
if !n.is_finite() || (-max_value as f64 <= n && n <= max_value as f64) {
|
if !Float::is_finite(n) || (-max_value as f64 <= n && n <= max_value as f64) {
|
||||||
Some($slf as $DstT)
|
Some($slf as $DstT)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -454,8 +455,8 @@ impl<T: NumCast> NumCast for Wrapping<T> {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn to_primitive_float() {
|
fn to_primitive_float() {
|
||||||
use std::f32;
|
use core::f32;
|
||||||
use std::f64;
|
use core::f64;
|
||||||
|
|
||||||
let f32_toolarge = 1e39f64;
|
let f32_toolarge = 1e39f64;
|
||||||
assert_eq!(f32_toolarge.to_f32(), None);
|
assert_eq!(f32_toolarge.to_f32(), None);
|
||||||
|
|
373
src/float.rs
373
src/float.rs
File diff suppressed because it is too large
Load Diff
|
@ -1,5 +1,5 @@
|
||||||
use std::ops::{Add, Mul};
|
use core::ops::{Add, Mul};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
/// Defines an additive identity element for `Self`.
|
/// Defines an additive identity element for `Self`.
|
||||||
pub trait Zero: Sized + Add<Self, Output = Self> {
|
pub trait Zero: Sized + Add<Self, Output = Self> {
|
||||||
|
@ -79,6 +79,10 @@ pub trait One: Sized + Mul<Self, Output = Self> {
|
||||||
/// `static mut`s.
|
/// `static mut`s.
|
||||||
// FIXME (#5527): This should be an associated constant
|
// FIXME (#5527): This should be an associated constant
|
||||||
fn one() -> Self;
|
fn one() -> Self;
|
||||||
|
|
||||||
|
/// Returns `true` if `self` is equal to the multiplicative identity.
|
||||||
|
#[inline]
|
||||||
|
fn is_one(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! one_impl {
|
macro_rules! one_impl {
|
||||||
|
@ -86,6 +90,9 @@ macro_rules! one_impl {
|
||||||
impl One for $t {
|
impl One for $t {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn one() -> $t { $v }
|
fn one() -> $t { $v }
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn is_one(&self) -> bool { *self == $v }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,6 +116,10 @@ impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
|
||||||
fn one() -> Self {
|
fn one() -> Self {
|
||||||
Wrapping(T::one())
|
Wrapping(T::one())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_one(&self) -> bool {
|
||||||
|
self.0.is_one()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some helper functions provided for backwards compatibility.
|
// Some helper functions provided for backwards compatibility.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
|
use core::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
|
||||||
|
|
||||||
use {Num, NumCast};
|
use {Num, NumCast};
|
||||||
use bounds::Bounded;
|
use bounds::Bounded;
|
||||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -9,12 +9,20 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
//! Numeric traits for generic mathematics
|
//! Numeric traits for generic mathematics
|
||||||
|
#![doc(html_logo_url = "https://rust-num.github.io/num/rust-logo-128x128-blk-v2.png",
|
||||||
|
html_favicon_url = "https://rust-num.github.io/num/favicon.ico",
|
||||||
|
html_root_url = "https://rust-num.github.io/num/",
|
||||||
|
html_playground_url = "http://play.integer32.com/")]
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/num-traits/0.1")]
|
#![deny(unconditional_recursion)]
|
||||||
|
|
||||||
use std::ops::{Add, Sub, Mul, Div, Rem};
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
|
#[cfg(feature = "std")]
|
||||||
use std::num::Wrapping;
|
extern crate core;
|
||||||
|
|
||||||
|
use core::ops::{Add, Sub, Mul, Div, Rem};
|
||||||
|
use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
|
||||||
|
use core::num::Wrapping;
|
||||||
|
|
||||||
pub use bounds::Bounded;
|
pub use bounds::Bounded;
|
||||||
pub use float::{Float, FloatConst};
|
pub use float::{Float, FloatConst};
|
||||||
|
@ -127,10 +135,10 @@ impl<T> NumAssignRef for T where T: NumAssign + for<'r> NumAssignOps<&'r T> {}
|
||||||
macro_rules! int_trait_impl {
|
macro_rules! int_trait_impl {
|
||||||
($name:ident for $($t:ty)*) => ($(
|
($name:ident for $($t:ty)*) => ($(
|
||||||
impl $name for $t {
|
impl $name for $t {
|
||||||
type FromStrRadixErr = ::std::num::ParseIntError;
|
type FromStrRadixErr = ::core::num::ParseIntError;
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_str_radix(s: &str, radix: u32)
|
fn from_str_radix(s: &str, radix: u32)
|
||||||
-> Result<Self, ::std::num::ParseIntError>
|
-> Result<Self, ::core::num::ParseIntError>
|
||||||
{
|
{
|
||||||
<$t>::from_str_radix(s, radix)
|
<$t>::from_str_radix(s, radix)
|
||||||
}
|
}
|
||||||
|
@ -156,7 +164,7 @@ pub enum FloatErrorKind {
|
||||||
Empty,
|
Empty,
|
||||||
Invalid,
|
Invalid,
|
||||||
}
|
}
|
||||||
// FIXME: std::num::ParseFloatError is stable in 1.0, but opaque to us,
|
// FIXME: core::num::ParseFloatError is stable in 1.0, but opaque to us,
|
||||||
// so there's not really any way for us to reuse it.
|
// so there's not really any way for us to reuse it.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ParseFloatError {
|
pub struct ParseFloatError {
|
||||||
|
@ -303,8 +311,8 @@ macro_rules! float_trait_impl {
|
||||||
};
|
};
|
||||||
|
|
||||||
match (is_positive, exp) {
|
match (is_positive, exp) {
|
||||||
(true, Ok(exp)) => base.powi(exp as i32),
|
(true, Ok(exp)) => Float::powi(base, exp as i32),
|
||||||
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
|
(false, Ok(exp)) => 1.0 / Float::powi(base, exp as i32),
|
||||||
(_, Err(_)) => return Err(PFE { kind: Invalid }),
|
(_, Err(_)) => return Err(PFE { kind: Invalid }),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::ops::{Add, Sub, Mul, Div};
|
use core::ops::{Add, Sub, Mul, Div};
|
||||||
|
|
||||||
/// Performs addition that returns `None` instead of wrapping around on
|
/// Performs addition that returns `None` instead of wrapping around on
|
||||||
/// overflow.
|
/// overflow.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::ops::{Add, Sub, Mul};
|
use core::ops::{Add, Sub, Mul};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
macro_rules! wrapping_impl {
|
macro_rules! wrapping_impl {
|
||||||
($trait_name:ident, $method:ident, $t:ty) => {
|
($trait_name:ident, $method:ident, $t:ty) => {
|
||||||
|
|
13
src/pow.rs
13
src/pow.rs
|
@ -1,4 +1,4 @@
|
||||||
use std::ops::Mul;
|
use core::ops::Mul;
|
||||||
use {One, CheckedMul};
|
use {One, CheckedMul};
|
||||||
|
|
||||||
/// Raises a value to the power of exp, using exponentiation by squaring.
|
/// Raises a value to the power of exp, using exponentiation by squaring.
|
||||||
|
@ -12,11 +12,14 @@ use {One, CheckedMul};
|
||||||
/// assert_eq!(pow(6u8, 3), 216);
|
/// assert_eq!(pow(6u8, 3), 216);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
|
pub fn pow<T: Clone + One>(mut base: T, mut exp: usize) -> T
|
||||||
|
where
|
||||||
|
for<'a> &'a T: Mul<&'a T, Output = T>,
|
||||||
|
{
|
||||||
if exp == 0 { return T::one() }
|
if exp == 0 { return T::one() }
|
||||||
|
|
||||||
while exp & 1 == 0 {
|
while exp & 1 == 0 {
|
||||||
base = base.clone() * base;
|
base = &base * &base;
|
||||||
exp >>= 1;
|
exp >>= 1;
|
||||||
}
|
}
|
||||||
if exp == 1 { return base }
|
if exp == 1 { return base }
|
||||||
|
@ -24,9 +27,9 @@ pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) ->
|
||||||
let mut acc = base.clone();
|
let mut acc = base.clone();
|
||||||
while exp > 1 {
|
while exp > 1 {
|
||||||
exp >>= 1;
|
exp >>= 1;
|
||||||
base = base.clone() * base;
|
base = &base * &base;
|
||||||
if exp & 1 == 1 {
|
if exp & 1 == 1 {
|
||||||
acc = acc * base.clone();
|
acc = &acc * &base;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
acc
|
acc
|
||||||
|
|
15
src/sign.rs
15
src/sign.rs
|
@ -1,8 +1,8 @@
|
||||||
use std::ops::Neg;
|
use core::ops::Neg;
|
||||||
use std::{f32, f64};
|
use core::{f32, f64};
|
||||||
use std::num::Wrapping;
|
use core::num::Wrapping;
|
||||||
|
|
||||||
use Num;
|
use {Num, Float};
|
||||||
|
|
||||||
/// Useful functions for signed numbers (i.e. numbers that can be negative).
|
/// Useful functions for signed numbers (i.e. numbers that can be negative).
|
||||||
pub trait Signed: Sized + Num + Neg<Output = Self> {
|
pub trait Signed: Sized + Num + Neg<Output = Self> {
|
||||||
|
@ -104,16 +104,15 @@ macro_rules! signed_float_impl {
|
||||||
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
|
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn abs(&self) -> $t {
|
fn abs(&self) -> $t {
|
||||||
<$t>::abs(*self)
|
(*self).abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The positive difference of two numbers. Returns `0.0` if the number is
|
/// The positive difference of two numbers. Returns `0.0` if the number is
|
||||||
/// less than or equal to `other`, otherwise the difference between`self`
|
/// less than or equal to `other`, otherwise the difference between`self`
|
||||||
/// and `other` is returned.
|
/// and `other` is returned.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(deprecated)]
|
|
||||||
fn abs_sub(&self, other: &$t) -> $t {
|
fn abs_sub(&self, other: &$t) -> $t {
|
||||||
<$t>::abs_sub(*self, *other)
|
if *self <= *other { 0. } else { *self - *other }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Returns
|
/// # Returns
|
||||||
|
@ -123,7 +122,7 @@ macro_rules! signed_float_impl {
|
||||||
/// - `NAN` if the number is NaN
|
/// - `NAN` if the number is NaN
|
||||||
#[inline]
|
#[inline]
|
||||||
fn signum(&self) -> $t {
|
fn signum(&self) -> $t {
|
||||||
<$t>::signum(*self)
|
Float::signum(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
|
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
|
||||||
|
|
Loading…
Reference in New Issue