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"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::{usize, u8, u16, u32, u64};
|
||||
use std::{isize, i8, i16, i32, i64};
|
||||
use std::{f32, f64};
|
||||
use std::num::Wrapping;
|
||||
use core::{usize, u8, u16, u32, u64};
|
||||
use core::{isize, i8, i16, i32, i64};
|
||||
use core::{f32, f64};
|
||||
use core::num::Wrapping;
|
||||
|
||||
/// Numbers which have upper and lower bounds
|
||||
pub trait Bounded {
|
||||
|
|
13
src/cast.rs
13
src/cast.rs
|
@ -1,8 +1,9 @@
|
|||
use std::mem::size_of;
|
||||
use std::num::Wrapping;
|
||||
use core::mem::size_of;
|
||||
use core::num::Wrapping;
|
||||
|
||||
use identities::Zero;
|
||||
use bounds::Bounded;
|
||||
use float::Float;
|
||||
|
||||
/// A generic trait for converting a value to a number.
|
||||
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.
|
||||
// NaN and +-inf are cast as they are.
|
||||
let n = $slf as f64;
|
||||
let max_value: $DstT = ::std::$DstT::MAX;
|
||||
if !n.is_finite() || (-max_value as f64 <= n && n <= max_value as f64) {
|
||||
let max_value: $DstT = ::core::$DstT::MAX;
|
||||
if !Float::is_finite(n) || (-max_value as f64 <= n && n <= max_value as f64) {
|
||||
Some($slf as $DstT)
|
||||
} else {
|
||||
None
|
||||
|
@ -454,8 +455,8 @@ impl<T: NumCast> NumCast for Wrapping<T> {
|
|||
|
||||
#[test]
|
||||
fn to_primitive_float() {
|
||||
use std::f32;
|
||||
use std::f64;
|
||||
use core::f32;
|
||||
use core::f64;
|
||||
|
||||
let f32_toolarge = 1e39f64;
|
||||
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 std::num::Wrapping;
|
||||
use core::ops::{Add, Mul};
|
||||
use core::num::Wrapping;
|
||||
|
||||
/// Defines an additive identity element for `Self`.
|
||||
pub trait Zero: Sized + Add<Self, Output = Self> {
|
||||
|
@ -79,6 +79,10 @@ pub trait One: Sized + Mul<Self, Output = Self> {
|
|||
/// `static mut`s.
|
||||
// FIXME (#5527): This should be an associated constant
|
||||
fn one() -> Self;
|
||||
|
||||
/// Returns `true` if `self` is equal to the multiplicative identity.
|
||||
#[inline]
|
||||
fn is_one(&self) -> bool;
|
||||
}
|
||||
|
||||
macro_rules! one_impl {
|
||||
|
@ -86,6 +90,9 @@ macro_rules! one_impl {
|
|||
impl One for $t {
|
||||
#[inline]
|
||||
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 {
|
||||
Wrapping(T::one())
|
||||
}
|
||||
|
||||
fn is_one(&self) -> bool {
|
||||
self.0.is_one()
|
||||
}
|
||||
}
|
||||
|
||||
// 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 bounds::Bounded;
|
||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -9,12 +9,20 @@
|
|||
// except according to those terms.
|
||||
|
||||
//! 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};
|
||||
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
|
||||
use std::num::Wrapping;
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#[cfg(feature = "std")]
|
||||
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 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 {
|
||||
($name:ident for $($t:ty)*) => ($(
|
||||
impl $name for $t {
|
||||
type FromStrRadixErr = ::std::num::ParseIntError;
|
||||
type FromStrRadixErr = ::core::num::ParseIntError;
|
||||
#[inline]
|
||||
fn from_str_radix(s: &str, radix: u32)
|
||||
-> Result<Self, ::std::num::ParseIntError>
|
||||
-> Result<Self, ::core::num::ParseIntError>
|
||||
{
|
||||
<$t>::from_str_radix(s, radix)
|
||||
}
|
||||
|
@ -156,7 +164,7 @@ pub enum FloatErrorKind {
|
|||
Empty,
|
||||
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.
|
||||
#[derive(Debug)]
|
||||
pub struct ParseFloatError {
|
||||
|
@ -303,8 +311,8 @@ macro_rules! float_trait_impl {
|
|||
};
|
||||
|
||||
match (is_positive, exp) {
|
||||
(true, Ok(exp)) => base.powi(exp as i32),
|
||||
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
|
||||
(true, Ok(exp)) => Float::powi(base, exp as i32),
|
||||
(false, Ok(exp)) => 1.0 / Float::powi(base, exp as i32),
|
||||
(_, 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
|
||||
/// overflow.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::ops::{Add, Sub, Mul};
|
||||
use std::num::Wrapping;
|
||||
use core::ops::{Add, Sub, Mul};
|
||||
use core::num::Wrapping;
|
||||
|
||||
macro_rules! wrapping_impl {
|
||||
($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};
|
||||
|
||||
/// 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);
|
||||
/// ```
|
||||
#[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() }
|
||||
|
||||
while exp & 1 == 0 {
|
||||
base = base.clone() * base;
|
||||
base = &base * &base;
|
||||
exp >>= 1;
|
||||
}
|
||||
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();
|
||||
while exp > 1 {
|
||||
exp >>= 1;
|
||||
base = base.clone() * base;
|
||||
base = &base * &base;
|
||||
if exp & 1 == 1 {
|
||||
acc = acc * base.clone();
|
||||
acc = &acc * &base;
|
||||
}
|
||||
}
|
||||
acc
|
||||
|
|
15
src/sign.rs
15
src/sign.rs
|
@ -1,8 +1,8 @@
|
|||
use std::ops::Neg;
|
||||
use std::{f32, f64};
|
||||
use std::num::Wrapping;
|
||||
use core::ops::Neg;
|
||||
use core::{f32, f64};
|
||||
use core::num::Wrapping;
|
||||
|
||||
use Num;
|
||||
use {Num, Float};
|
||||
|
||||
/// Useful functions for signed numbers (i.e. numbers that can be negative).
|
||||
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`.
|
||||
#[inline]
|
||||
fn abs(&self) -> $t {
|
||||
<$t>::abs(*self)
|
||||
(*self).abs()
|
||||
}
|
||||
|
||||
/// The positive difference of two numbers. Returns `0.0` if the number is
|
||||
/// less than or equal to `other`, otherwise the difference between`self`
|
||||
/// and `other` is returned.
|
||||
#[inline]
|
||||
#[allow(deprecated)]
|
||||
fn abs_sub(&self, other: &$t) -> $t {
|
||||
<$t>::abs_sub(*self, *other)
|
||||
if *self <= *other { 0. } else { *self - *other }
|
||||
}
|
||||
|
||||
/// # Returns
|
||||
|
@ -123,7 +122,7 @@ macro_rules! signed_float_impl {
|
|||
/// - `NAN` if the number is NaN
|
||||
#[inline]
|
||||
fn signum(&self) -> $t {
|
||||
<$t>::signum(*self)
|
||||
Float::signum(*self)
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
|
||||
|
|
Loading…
Reference in New Issue