Compare commits
87 Commits
num-traits
...
master
Author | SHA1 | Date |
---|---|---|
|
2f0cffd522 | |
|
4fc3d8f72d | |
|
06b3f854d4 | |
|
5b9f6e4c47 | |
|
93328dfc90 | |
|
c4256bd4df | |
|
b64ee3809c | |
|
27b9202ff3 | |
|
f050c60df9 | |
|
2d113f56c8 | |
|
63047365be | |
|
0547a355ee | |
|
4b1ea5fb12 | |
|
1b28e6182d | |
|
55c5b7455a | |
|
c28e2fe062 | |
|
aaf3c267bd | |
|
849e2a0b1b | |
|
4234eb76aa | |
|
4d3cb0a4ba | |
|
f523f532e6 | |
|
b4558d1c49 | |
|
fec6c3610c | |
|
d394467906 | |
|
987ed8fd38 | |
|
d02f166765 | |
|
33b74618b6 | |
|
428f89a7d5 | |
|
0e7c2a4a00 | |
|
3add713434 | |
|
40898e5071 | |
|
973ba72e4f | |
|
e7ba9b62dc | |
|
2fb8a6e8a9 | |
|
b8906eff1b | |
|
7a61e79757 | |
|
45067c1357 | |
|
cd0da1ae5e | |
|
f20d74fce8 | |
|
e8dce19146 | |
|
58f02a8677 | |
|
d1f5658bfe | |
|
107a326745 | |
|
84e14d4f36 | |
|
2c2cfe1bf3 | |
|
a194d91625 | |
|
8e765ee1ff | |
|
6d62b6a228 | |
|
852ec9380f | |
|
615d83a5b6 | |
|
38655c91c1 | |
|
a1688f6991 | |
|
c38b4b601d | |
|
4ab251b0a2 | |
|
5404658360 | |
|
09e27abaa0 | |
|
87d4dbc418 | |
|
398c298fa9 | |
|
8915b74ae4 | |
|
9cd2422221 | |
|
d2a1e035ad | |
|
8ac6a62a6e | |
|
f06893feb0 | |
|
80052795ba | |
|
f3869040c7 | |
|
d668985fae | |
|
ff45e00849 | |
|
2925f10f40 | |
|
fc4f1afdf6 | |
|
5c24fcc4a7 | |
|
b8ad107c1c | |
|
ac7e550b00 | |
|
abb51f9a09 | |
|
5985a8b750 | |
|
2b975badfa | |
|
0f228be4d5 | |
|
a415e2a751 | |
|
d2bf4e04e4 | |
|
83841d15f8 | |
|
e796afc83a | |
|
714057979e | |
|
60924ecc70 | |
|
21e3620999 | |
|
dd7900d62f | |
|
d968efbc76 | |
|
15dc0e7127 | |
|
4775dee66b |
33
.travis.yml
33
.travis.yml
|
@ -1,15 +1,46 @@
|
|||
language: rust
|
||||
sudo: false
|
||||
rust:
|
||||
- 1.8.0
|
||||
- 1.15.0
|
||||
- 1.20.0
|
||||
- 1.26.0 # has_i128
|
||||
- 1.31.0 # 2018!
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
sudo: false
|
||||
script:
|
||||
- cargo build --verbose
|
||||
- ./ci/test_full.sh
|
||||
matrix:
|
||||
include:
|
||||
# i586 presents floating point challenges for lack of SSE/SSE2
|
||||
- name: "i586"
|
||||
rust: stable
|
||||
env: TARGET=i586-unknown-linux-gnu
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- gcc-multilib
|
||||
before_script:
|
||||
- rustup target add $TARGET
|
||||
script:
|
||||
- cargo test --verbose --target $TARGET --all-features
|
||||
# try a target that doesn't have std at all
|
||||
- name: "no_std"
|
||||
rust: stable
|
||||
env: TARGET=thumbv6m-none-eabi
|
||||
before_script:
|
||||
- rustup target add $TARGET
|
||||
script:
|
||||
- cargo build --verbose --target $TARGET --no-default-features --features i128
|
||||
- cargo build --verbose --target $TARGET --no-default-features --features libm
|
||||
- name: "rustfmt"
|
||||
rust: 1.31.0
|
||||
before_script:
|
||||
- rustup component add rustfmt
|
||||
script:
|
||||
- cargo fmt --all -- --check
|
||||
notifications:
|
||||
email:
|
||||
on_success: never
|
||||
|
|
|
@ -8,16 +8,21 @@ categories = ["algorithms", "science", "no-std"]
|
|||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/rust-num/num-traits"
|
||||
name = "num-traits"
|
||||
version = "0.2.4"
|
||||
version = "0.2.8"
|
||||
readme = "README.md"
|
||||
build = "build.rs"
|
||||
exclude = ["/ci/*", "/.travis.yml", "/bors.toml"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["std"]
|
||||
|
||||
[dependencies]
|
||||
libm = { version = "0.1.4", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
i128 = []
|
||||
|
||||
[build-dependencies]
|
||||
autocfg = "0.1.3"
|
||||
|
|
|
@ -31,11 +31,14 @@ the default `std` feature. Use this in `Cargo.toml`:
|
|||
[dependencies.num-traits]
|
||||
version = "0.2"
|
||||
default-features = false
|
||||
# features = ["libm"] # <--- Uncomment if you wish to use `Float` and `Real` without `std`
|
||||
```
|
||||
|
||||
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-
|
||||
The `Float` and `Real` traits are only available when either `std` or `libm` is enabled.
|
||||
The `libm` feature is only available with Rust 1.31 and later ([see PR #99](https://github.com/rust-num/num-traits/pull/99)).
|
||||
|
||||
The `FloatCore` trait is always available. `MulAdd` and `MulAddAssign` for `f32`
|
||||
and `f64` also require `std` or `libm`, as do implementations of signed and floating-
|
||||
point exponents in `Pow`.
|
||||
|
||||
Implementations for `i128` and `u128` are only available with Rust 1.26 and
|
||||
|
|
58
RELEASES.md
58
RELEASES.md
|
@ -1,4 +1,48 @@
|
|||
# Release 0.2.4
|
||||
# Release 0.2.8 (2019-05-21)
|
||||
|
||||
- [Fixed feature detection on `no_std` targets][116].
|
||||
|
||||
**Contributors**: @cuviper
|
||||
|
||||
[116]: https://github.com/rust-num/num-traits/pull/116
|
||||
|
||||
# Release 0.2.7 (2019-05-20)
|
||||
|
||||
- [Documented when `CheckedShl` and `CheckedShr` return `None`][90].
|
||||
- [The new `Zero::set_zero` and `One::set_one`][104] will set values to their
|
||||
identities in place, possibly optimized better than direct assignment.
|
||||
- [Documented general features and intentions of `PrimInt`][108].
|
||||
|
||||
**Contributors**: @cuviper, @dvdhrm, @ignatenkobrain, @lcnr, @samueltardieu
|
||||
|
||||
[90]: https://github.com/rust-num/num-traits/pull/90
|
||||
[104]: https://github.com/rust-num/num-traits/pull/104
|
||||
[108]: https://github.com/rust-num/num-traits/pull/108
|
||||
|
||||
# Release 0.2.6 (2018-09-13)
|
||||
|
||||
- [Documented that `pow(0, 0)` returns `1`][79]. Mathematically, this is not
|
||||
strictly defined, but the current behavior is a pragmatic choice that has
|
||||
precedent in Rust `core` for the primitives and in many other languages.
|
||||
- [The new `WrappingShl` and `WrappingShr` traits][81] will wrap the shift count
|
||||
if it exceeds the bit size of the type.
|
||||
|
||||
**Contributors**: @cuviper, @edmccard, @meltinglava
|
||||
|
||||
[79]: https://github.com/rust-num/num-traits/pull/79
|
||||
[81]: https://github.com/rust-num/num-traits/pull/81
|
||||
|
||||
# Release 0.2.5 (2018-06-20)
|
||||
|
||||
- [Documentation for `mul_add` now clarifies that it's not always faster.][70]
|
||||
- [The default methods in `FromPrimitive` and `ToPrimitive` are more robust.][73]
|
||||
|
||||
**Contributors**: @cuviper, @frewsxcv
|
||||
|
||||
[70]: https://github.com/rust-num/num-traits/pull/70
|
||||
[73]: https://github.com/rust-num/num-traits/pull/73
|
||||
|
||||
# Release 0.2.4 (2018-05-11)
|
||||
|
||||
- [Support for 128-bit integers is now automatically detected and enabled.][69]
|
||||
Setting the `i128` crate feature now causes the build script to panic if such
|
||||
|
@ -8,7 +52,7 @@
|
|||
|
||||
[69]: https://github.com/rust-num/num-traits/pull/69
|
||||
|
||||
# Release 0.2.3
|
||||
# Release 0.2.3 (2018-05-10)
|
||||
|
||||
- [The new `CheckedNeg` and `CheckedRem` traits][63] perform checked `Neg` and
|
||||
`Rem`, returning `Some(output)` or `None` on overflow.
|
||||
|
@ -31,7 +75,7 @@
|
|||
[63]: https://github.com/rust-num/num-traits/pull/63
|
||||
[rust#47919]: https://github.com/rust-lang/rust/pull/47919
|
||||
|
||||
# Release 0.2.2
|
||||
# Release 0.2.2 (2018-03-18)
|
||||
|
||||
- [Casting from floating point to integers now returns `None` on overflow][52],
|
||||
avoiding [rustc's undefined behavior][rust-10184]. This applies to the `cast`
|
||||
|
@ -43,7 +87,7 @@
|
|||
[rust-10184]: https://github.com/rust-lang/rust/issues/10184
|
||||
|
||||
|
||||
# Release 0.2.1
|
||||
# Release 0.2.1 (2018-03-01)
|
||||
|
||||
- [The new `FloatCore` trait][32] offers a subset of `Float` for `#![no_std]` use.
|
||||
[This includes everything][41] except the transcendental functions and FMA.
|
||||
|
@ -62,7 +106,7 @@
|
|||
[41]: https://github.com/rust-num/num-traits/pull/41
|
||||
|
||||
|
||||
# Release 0.2.0
|
||||
# Release 0.2.0 (2018-02-06)
|
||||
|
||||
- **breaking change**: [There is now a `std` feature][30], enabled by default, along
|
||||
with the implication that building *without* this feature makes this a
|
||||
|
@ -77,14 +121,14 @@
|
|||
[30]: https://github.com/rust-num/num-traits/pull/30
|
||||
|
||||
|
||||
# Release 0.1.43
|
||||
# Release 0.1.43 (2018-02-06)
|
||||
|
||||
- All items are now [re-exported from num-traits 0.2][31] for compatibility.
|
||||
|
||||
[31]: https://github.com/rust-num/num-traits/pull/31
|
||||
|
||||
|
||||
# Release 0.1.42
|
||||
# Release 0.1.42 (2018-01-22)
|
||||
|
||||
- [num-traits now has its own source repository][num-356] at [rust-num/num-traits][home].
|
||||
- [`ParseFloatError` now implements `Display`][22].
|
||||
|
|
33
build.rs
33
build.rs
|
@ -1,35 +1,14 @@
|
|||
extern crate autocfg;
|
||||
|
||||
use std::env;
|
||||
use std::io::Write;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
fn main() {
|
||||
if probe("fn main() { 0i128; }") {
|
||||
let ac = autocfg::new();
|
||||
if ac.probe_type("i128") {
|
||||
println!("cargo:rustc-cfg=has_i128");
|
||||
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
|
||||
panic!("i128 support was not detected!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Test if a code snippet can be compiled
|
||||
fn probe(code: &str) -> bool {
|
||||
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
|
||||
let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR");
|
||||
|
||||
let mut child = Command::new(rustc)
|
||||
.arg("--out-dir")
|
||||
.arg(out_dir)
|
||||
.arg("--emit=obj")
|
||||
.arg("-")
|
||||
.stdin(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("rustc probe");
|
||||
|
||||
child
|
||||
.stdin
|
||||
.as_mut()
|
||||
.expect("rustc stdin")
|
||||
.write_all(code.as_bytes())
|
||||
.expect("write rustc stdin");
|
||||
|
||||
child.wait().expect("rustc probe").success()
|
||||
|
||||
autocfg::rerun_path(file!());
|
||||
}
|
||||
|
|
|
@ -12,8 +12,16 @@ cargo test --verbose
|
|||
cargo build --verbose --no-default-features
|
||||
cargo test --verbose --no-default-features
|
||||
|
||||
# test `i128`
|
||||
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable)$ ]]; then
|
||||
# test `i128`
|
||||
cargo build --verbose --features=i128
|
||||
cargo test --verbose --features=i128
|
||||
|
||||
# test with std and libm (libm build fails on Rust 1.26 and earlier)
|
||||
cargo build --verbose --features "libm"
|
||||
cargo test --verbose --features "libm"
|
||||
|
||||
# test `no_std` with libm (libm build fails on Rust 1.26 and earlier)
|
||||
cargo build --verbose --no-default-features --features "libm"
|
||||
cargo test --verbose --no-default-features --features "libm"
|
||||
fi
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use core::{usize, u8, u16, u32, u64};
|
||||
use core::{isize, i8, i16, i32, i64};
|
||||
use core::{f32, f64};
|
||||
use core::num::Wrapping;
|
||||
use core::{f32, f64};
|
||||
#[cfg(has_i128)]
|
||||
use core::{i128, u128};
|
||||
use core::{i16, i32, i64, i8, isize};
|
||||
use core::{u16, u32, u64, u8, usize};
|
||||
|
||||
/// Numbers which have upper and lower bounds
|
||||
pub trait Bounded {
|
||||
|
@ -18,33 +18,41 @@ macro_rules! bounded_impl {
|
|||
($t:ty, $min:expr, $max:expr) => {
|
||||
impl Bounded for $t {
|
||||
#[inline]
|
||||
fn min_value() -> $t { $min }
|
||||
fn min_value() -> $t {
|
||||
$min
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn max_value() -> $t { $max }
|
||||
fn max_value() -> $t {
|
||||
$max
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
bounded_impl!(usize, usize::MIN, usize::MAX);
|
||||
bounded_impl!(u8, u8::MIN, u8::MAX);
|
||||
bounded_impl!(u16, u16::MIN, u16::MAX);
|
||||
bounded_impl!(u32, u32::MIN, u32::MAX);
|
||||
bounded_impl!(u64, u64::MIN, u64::MAX);
|
||||
bounded_impl!(u8, u8::MIN, u8::MAX);
|
||||
bounded_impl!(u16, u16::MIN, u16::MAX);
|
||||
bounded_impl!(u32, u32::MIN, u32::MAX);
|
||||
bounded_impl!(u64, u64::MIN, u64::MAX);
|
||||
#[cfg(has_i128)]
|
||||
bounded_impl!(u128, u128::MIN, u128::MAX);
|
||||
bounded_impl!(u128, u128::MIN, u128::MAX);
|
||||
|
||||
bounded_impl!(isize, isize::MIN, isize::MAX);
|
||||
bounded_impl!(i8, i8::MIN, i8::MAX);
|
||||
bounded_impl!(i16, i16::MIN, i16::MAX);
|
||||
bounded_impl!(i32, i32::MIN, i32::MAX);
|
||||
bounded_impl!(i64, i64::MIN, i64::MAX);
|
||||
bounded_impl!(i8, i8::MIN, i8::MAX);
|
||||
bounded_impl!(i16, i16::MIN, i16::MAX);
|
||||
bounded_impl!(i32, i32::MIN, i32::MAX);
|
||||
bounded_impl!(i64, i64::MIN, i64::MAX);
|
||||
#[cfg(has_i128)]
|
||||
bounded_impl!(i128, i128::MIN, i128::MAX);
|
||||
bounded_impl!(i128, i128::MIN, i128::MAX);
|
||||
|
||||
impl<T: Bounded> Bounded for Wrapping<T> {
|
||||
fn min_value() -> Self { Wrapping(T::min_value()) }
|
||||
fn max_value() -> Self { Wrapping(T::max_value()) }
|
||||
fn min_value() -> Self {
|
||||
Wrapping(T::min_value())
|
||||
}
|
||||
fn max_value() -> Self {
|
||||
Wrapping(T::max_value())
|
||||
}
|
||||
}
|
||||
|
||||
bounded_impl!(f32, f32::MIN, f32::MAX);
|
||||
|
@ -59,9 +67,9 @@ macro_rules! for_each_tuple_ {
|
|||
);
|
||||
}
|
||||
macro_rules! for_each_tuple {
|
||||
( $m:ident ) => (
|
||||
($m:ident) => {
|
||||
for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! bounded_tuple {
|
||||
|
@ -82,14 +90,13 @@ macro_rules! bounded_tuple {
|
|||
for_each_tuple!(bounded_tuple);
|
||||
bounded_impl!(f64, f64::MIN, f64::MAX);
|
||||
|
||||
|
||||
#[test]
|
||||
fn wrapping_bounded() {
|
||||
macro_rules! test_wrapping_bounded {
|
||||
($($t:ty)+) => {
|
||||
$(
|
||||
assert_eq!(Wrapping::<$t>::min_value().0, <$t>::min_value());
|
||||
assert_eq!(Wrapping::<$t>::max_value().0, <$t>::max_value());
|
||||
assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
|
||||
assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
@ -103,8 +110,8 @@ fn wrapping_bounded_i128() {
|
|||
macro_rules! test_wrapping_bounded {
|
||||
($($t:ty)+) => {
|
||||
$(
|
||||
assert_eq!(Wrapping::<$t>::min_value().0, <$t>::min_value());
|
||||
assert_eq!(Wrapping::<$t>::max_value().0, <$t>::max_value());
|
||||
assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
|
||||
assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
|
251
src/cast.rs
251
src/cast.rs
|
@ -1,10 +1,10 @@
|
|||
use core::{i8, i16, i32, i64, isize};
|
||||
use core::{u8, u16, u32, u64, usize};
|
||||
use core::{f32, f64};
|
||||
use core::mem::size_of;
|
||||
use core::num::Wrapping;
|
||||
use core::{f32, f64};
|
||||
#[cfg(has_i128)]
|
||||
use core::{i128, u128};
|
||||
use core::{i16, i32, i64, i8, isize};
|
||||
use core::{u16, u32, u64, u8, usize};
|
||||
|
||||
use float::FloatCore;
|
||||
|
||||
|
@ -13,25 +13,25 @@ pub trait ToPrimitive {
|
|||
/// Converts the value of `self` to an `isize`.
|
||||
#[inline]
|
||||
fn to_isize(&self) -> Option<isize> {
|
||||
self.to_i64().and_then(|x| x.to_isize())
|
||||
self.to_i64().as_ref().and_then(ToPrimitive::to_isize)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `i8`.
|
||||
#[inline]
|
||||
fn to_i8(&self) -> Option<i8> {
|
||||
self.to_i64().and_then(|x| x.to_i8())
|
||||
self.to_i64().as_ref().and_then(ToPrimitive::to_i8)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `i16`.
|
||||
#[inline]
|
||||
fn to_i16(&self) -> Option<i16> {
|
||||
self.to_i64().and_then(|x| x.to_i16())
|
||||
self.to_i64().as_ref().and_then(ToPrimitive::to_i16)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `i32`.
|
||||
#[inline]
|
||||
fn to_i32(&self) -> Option<i32> {
|
||||
self.to_i64().and_then(|x| x.to_i32())
|
||||
self.to_i64().as_ref().and_then(ToPrimitive::to_i32)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `i64`.
|
||||
|
@ -52,25 +52,25 @@ pub trait ToPrimitive {
|
|||
/// Converts the value of `self` to a `usize`.
|
||||
#[inline]
|
||||
fn to_usize(&self) -> Option<usize> {
|
||||
self.to_u64().and_then(|x| x.to_usize())
|
||||
self.to_u64().as_ref().and_then(ToPrimitive::to_usize)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `u8`.
|
||||
#[inline]
|
||||
fn to_u8(&self) -> Option<u8> {
|
||||
self.to_u64().and_then(|x| x.to_u8())
|
||||
self.to_u64().as_ref().and_then(ToPrimitive::to_u8)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `u16`.
|
||||
#[inline]
|
||||
fn to_u16(&self) -> Option<u16> {
|
||||
self.to_u64().and_then(|x| x.to_u16())
|
||||
self.to_u64().as_ref().and_then(ToPrimitive::to_u16)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `u32`.
|
||||
#[inline]
|
||||
fn to_u32(&self) -> Option<u32> {
|
||||
self.to_u64().and_then(|x| x.to_u32())
|
||||
self.to_u64().as_ref().and_then(ToPrimitive::to_u32)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `u64`.
|
||||
|
@ -92,13 +92,16 @@ pub trait ToPrimitive {
|
|||
/// Converts the value of `self` to an `f32`.
|
||||
#[inline]
|
||||
fn to_f32(&self) -> Option<f32> {
|
||||
self.to_f64().and_then(|x| x.to_f32())
|
||||
self.to_f64().as_ref().and_then(ToPrimitive::to_f32)
|
||||
}
|
||||
|
||||
/// Converts the value of `self` to an `f64`.
|
||||
#[inline]
|
||||
fn to_f64(&self) -> Option<f64> {
|
||||
self.to_i64().and_then(|x| x.to_f64())
|
||||
match self.to_i64() {
|
||||
Some(i) => i.to_f64(),
|
||||
None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,7 +137,7 @@ macro_rules! impl_to_primitive_int_to_uint {
|
|||
}
|
||||
|
||||
macro_rules! impl_to_primitive_int {
|
||||
($T:ident) => (
|
||||
($T:ident) => {
|
||||
impl ToPrimitive for $T {
|
||||
impl_to_primitive_int_to_int! { $T:
|
||||
fn to_isize -> isize;
|
||||
|
@ -157,11 +160,15 @@ macro_rules! impl_to_primitive_int {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
|
||||
fn to_f32(&self) -> Option<f32> {
|
||||
Some(*self as f32)
|
||||
}
|
||||
#[inline]
|
||||
fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
|
||||
fn to_f64(&self) -> Option<f64> {
|
||||
Some(*self as f64)
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
impl_to_primitive_int!(isize);
|
||||
|
@ -203,7 +210,7 @@ macro_rules! impl_to_primitive_uint_to_uint {
|
|||
}
|
||||
|
||||
macro_rules! impl_to_primitive_uint {
|
||||
($T:ident) => (
|
||||
($T:ident) => {
|
||||
impl ToPrimitive for $T {
|
||||
impl_to_primitive_uint_to_int! { $T:
|
||||
fn to_isize -> isize;
|
||||
|
@ -226,11 +233,15 @@ macro_rules! impl_to_primitive_uint {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
|
||||
fn to_f32(&self) -> Option<f32> {
|
||||
Some(*self as f32)
|
||||
}
|
||||
#[inline]
|
||||
fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
|
||||
fn to_f64(&self) -> Option<f64> {
|
||||
Some(*self as f64)
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
impl_to_primitive_uint!(usize);
|
||||
|
@ -316,7 +327,7 @@ macro_rules! impl_to_primitive_float_to_unsigned_int {
|
|||
}
|
||||
|
||||
macro_rules! impl_to_primitive_float {
|
||||
($T:ident) => (
|
||||
($T:ident) => {
|
||||
impl ToPrimitive for $T {
|
||||
impl_to_primitive_float_to_signed_int! { $T:
|
||||
fn to_isize -> isize;
|
||||
|
@ -343,7 +354,7 @@ macro_rules! impl_to_primitive_float {
|
|||
fn to_f64 -> f64;
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
impl_to_primitive_float!(f32);
|
||||
|
@ -352,39 +363,39 @@ impl_to_primitive_float!(f64);
|
|||
/// A generic trait for converting a number to a value.
|
||||
pub trait FromPrimitive: Sized {
|
||||
/// Convert an `isize` to return an optional value of this type. If the
|
||||
/// value cannot be represented by this value, the `None` is returned.
|
||||
/// value cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_isize(n: isize) -> Option<Self> {
|
||||
FromPrimitive::from_i64(n as i64)
|
||||
n.to_i64().and_then(FromPrimitive::from_i64)
|
||||
}
|
||||
|
||||
/// Convert an `i8` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_i8(n: i8) -> Option<Self> {
|
||||
FromPrimitive::from_i64(n as i64)
|
||||
FromPrimitive::from_i64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert an `i16` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_i16(n: i16) -> Option<Self> {
|
||||
FromPrimitive::from_i64(n as i64)
|
||||
FromPrimitive::from_i64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert an `i32` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_i32(n: i32) -> Option<Self> {
|
||||
FromPrimitive::from_i64(n as i64)
|
||||
FromPrimitive::from_i64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert an `i64` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
fn from_i64(n: i64) -> Option<Self>;
|
||||
|
||||
/// Convert an `i128` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
///
|
||||
/// This method is only available with feature `i128` enabled on Rust >= 1.26.
|
||||
///
|
||||
|
@ -397,39 +408,39 @@ pub trait FromPrimitive: Sized {
|
|||
}
|
||||
|
||||
/// Convert a `usize` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_usize(n: usize) -> Option<Self> {
|
||||
FromPrimitive::from_u64(n as u64)
|
||||
n.to_u64().and_then(FromPrimitive::from_u64)
|
||||
}
|
||||
|
||||
/// Convert an `u8` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_u8(n: u8) -> Option<Self> {
|
||||
FromPrimitive::from_u64(n as u64)
|
||||
FromPrimitive::from_u64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert an `u16` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_u16(n: u16) -> Option<Self> {
|
||||
FromPrimitive::from_u64(n as u64)
|
||||
FromPrimitive::from_u64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert an `u32` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_u32(n: u32) -> Option<Self> {
|
||||
FromPrimitive::from_u64(n as u64)
|
||||
FromPrimitive::from_u64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert an `u64` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
fn from_u64(n: u64) -> Option<Self>;
|
||||
|
||||
/// Convert an `u128` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
///
|
||||
/// This method is only available with feature `i128` enabled on Rust >= 1.26.
|
||||
///
|
||||
|
@ -442,61 +453,107 @@ pub trait FromPrimitive: Sized {
|
|||
}
|
||||
|
||||
/// Convert a `f32` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_f32(n: f32) -> Option<Self> {
|
||||
FromPrimitive::from_f64(n as f64)
|
||||
FromPrimitive::from_f64(From::from(n))
|
||||
}
|
||||
|
||||
/// Convert a `f64` to return an optional value of this type. If the
|
||||
/// type cannot be represented by this value, the `None` is returned.
|
||||
/// type cannot be represented by this value, then `None` is returned.
|
||||
#[inline]
|
||||
fn from_f64(n: f64) -> Option<Self> {
|
||||
FromPrimitive::from_i64(n as i64)
|
||||
match n.to_i64() {
|
||||
Some(i) => FromPrimitive::from_i64(i),
|
||||
None => n.to_u64().and_then(FromPrimitive::from_u64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_from_primitive {
|
||||
($T:ty, $to_ty:ident) => (
|
||||
($T:ty, $to_ty:ident) => {
|
||||
#[allow(deprecated)]
|
||||
impl FromPrimitive for $T {
|
||||
#[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() }
|
||||
#[inline]
|
||||
fn from_isize(n: isize) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_i8(n: i8) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_i16(n: i16) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_i32(n: i32) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_i64(n: i64) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[cfg(has_i128)]
|
||||
#[inline] fn from_i128(n: i128) -> Option<$T> { n.$to_ty() }
|
||||
#[inline]
|
||||
fn from_i128(n: i128) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
|
||||
#[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() }
|
||||
#[inline]
|
||||
fn from_usize(n: usize) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_u8(n: u8) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_u16(n: u16) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_u32(n: u32) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_u64(n: u64) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[cfg(has_i128)]
|
||||
#[inline] fn from_u128(n: u128) -> Option<$T> { n.$to_ty() }
|
||||
#[inline]
|
||||
fn from_u128(n: u128) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
|
||||
#[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() }
|
||||
#[inline] fn from_f64(n: f64) -> Option<$T> { n.$to_ty() }
|
||||
#[inline]
|
||||
fn from_f32(n: f32) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
#[inline]
|
||||
fn from_f64(n: f64) -> Option<$T> {
|
||||
n.$to_ty()
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
impl_from_primitive!(isize, to_isize);
|
||||
impl_from_primitive!(i8, to_i8);
|
||||
impl_from_primitive!(i16, to_i16);
|
||||
impl_from_primitive!(i32, to_i32);
|
||||
impl_from_primitive!(i64, to_i64);
|
||||
impl_from_primitive!(i8, to_i8);
|
||||
impl_from_primitive!(i16, to_i16);
|
||||
impl_from_primitive!(i32, to_i32);
|
||||
impl_from_primitive!(i64, to_i64);
|
||||
#[cfg(has_i128)]
|
||||
impl_from_primitive!(i128, to_i128);
|
||||
impl_from_primitive!(i128, to_i128);
|
||||
impl_from_primitive!(usize, to_usize);
|
||||
impl_from_primitive!(u8, to_u8);
|
||||
impl_from_primitive!(u16, to_u16);
|
||||
impl_from_primitive!(u32, to_u32);
|
||||
impl_from_primitive!(u64, to_u64);
|
||||
impl_from_primitive!(u8, to_u8);
|
||||
impl_from_primitive!(u16, to_u16);
|
||||
impl_from_primitive!(u32, to_u32);
|
||||
impl_from_primitive!(u64, to_u64);
|
||||
#[cfg(has_i128)]
|
||||
impl_from_primitive!(u128, to_u128);
|
||||
impl_from_primitive!(f32, to_f32);
|
||||
impl_from_primitive!(f64, to_f64);
|
||||
|
||||
impl_from_primitive!(u128, to_u128);
|
||||
impl_from_primitive!(f32, to_f32);
|
||||
impl_from_primitive!(f64, to_f64);
|
||||
|
||||
macro_rules! impl_to_primitive_wrapping {
|
||||
($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
|
||||
|
@ -564,7 +621,6 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// Cast from one machine scalar to another.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -588,7 +644,7 @@ pub trait NumCast: Sized + ToPrimitive {
|
|||
}
|
||||
|
||||
macro_rules! impl_num_cast {
|
||||
($T:ty, $conv:ident) => (
|
||||
($T:ty, $conv:ident) => {
|
||||
impl NumCast for $T {
|
||||
#[inline]
|
||||
#[allow(deprecated)]
|
||||
|
@ -598,25 +654,25 @@ macro_rules! impl_num_cast {
|
|||
n.$conv()
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
impl_num_cast!(u8, to_u8);
|
||||
impl_num_cast!(u16, to_u16);
|
||||
impl_num_cast!(u32, to_u32);
|
||||
impl_num_cast!(u64, to_u64);
|
||||
impl_num_cast!(u8, to_u8);
|
||||
impl_num_cast!(u16, to_u16);
|
||||
impl_num_cast!(u32, to_u32);
|
||||
impl_num_cast!(u64, to_u64);
|
||||
#[cfg(has_i128)]
|
||||
impl_num_cast!(u128, to_u128);
|
||||
impl_num_cast!(u128, to_u128);
|
||||
impl_num_cast!(usize, to_usize);
|
||||
impl_num_cast!(i8, to_i8);
|
||||
impl_num_cast!(i16, to_i16);
|
||||
impl_num_cast!(i32, to_i32);
|
||||
impl_num_cast!(i64, to_i64);
|
||||
impl_num_cast!(i8, to_i8);
|
||||
impl_num_cast!(i16, to_i16);
|
||||
impl_num_cast!(i32, to_i32);
|
||||
impl_num_cast!(i64, to_i64);
|
||||
#[cfg(has_i128)]
|
||||
impl_num_cast!(i128, to_i128);
|
||||
impl_num_cast!(i128, to_i128);
|
||||
impl_num_cast!(isize, to_isize);
|
||||
impl_num_cast!(f32, to_f32);
|
||||
impl_num_cast!(f64, to_f64);
|
||||
impl_num_cast!(f32, to_f32);
|
||||
impl_num_cast!(f64, to_f64);
|
||||
|
||||
impl<T: NumCast> NumCast for Wrapping<T> {
|
||||
fn from<U: ToPrimitive>(n: U) -> Option<Self> {
|
||||
|
@ -626,7 +682,7 @@ impl<T: NumCast> NumCast for Wrapping<T> {
|
|||
|
||||
/// A generic interface for casting between machine scalars with the
|
||||
/// `as` operator, which admits narrowing and precision loss.
|
||||
/// Implementers of this trait AsPrimitive should behave like a primitive
|
||||
/// Implementers of this trait `AsPrimitive` should behave like a primitive
|
||||
/// numeric type (e.g. a newtype around another primitive), and the
|
||||
/// intended conversion must never fail.
|
||||
///
|
||||
|
@ -637,20 +693,20 @@ impl<T: NumCast> NumCast for Wrapping<T> {
|
|||
/// let three: i32 = (3.14159265f32).as_();
|
||||
/// assert_eq!(three, 3);
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
///
|
||||
/// Currently, some uses of the `as` operator are not entirely safe.
|
||||
/// In particular, it is undefined behavior if:
|
||||
///
|
||||
///
|
||||
/// - A truncated floating point value cannot fit in the target integer
|
||||
/// type ([#10184](https://github.com/rust-lang/rust/issues/10184));
|
||||
///
|
||||
///
|
||||
/// ```ignore
|
||||
/// # use num_traits::AsPrimitive;
|
||||
/// let x: u8 = (1.04E+17).as_(); // UB
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
/// - Or a floating point value does not fit in another floating
|
||||
/// point type ([#15536](https://github.com/rust-lang/rust/issues/15536)).
|
||||
///
|
||||
|
@ -658,10 +714,10 @@ impl<T: NumCast> NumCast for Wrapping<T> {
|
|||
/// # use num_traits::AsPrimitive;
|
||||
/// let x: f32 = (1e300f64).as_(); // UB
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
pub trait AsPrimitive<T>: 'static + Copy
|
||||
where
|
||||
T: 'static + Copy
|
||||
T: 'static + Copy,
|
||||
{
|
||||
/// Convert a value to another, using the `as` operator.
|
||||
fn as_(self) -> T;
|
||||
|
@ -704,4 +760,3 @@ impl_as_primitive!(f32 => { f32, f64 });
|
|||
impl_as_primitive!(f64 => { f32, f64 });
|
||||
impl_as_primitive!(char => { char });
|
||||
impl_as_primitive!(bool => {});
|
||||
|
||||
|
|
157
src/float.rs
157
src/float.rs
|
@ -1,12 +1,15 @@
|
|||
use core::mem;
|
||||
use core::ops::Neg;
|
||||
use core::num::FpCategory;
|
||||
use core::ops::Neg;
|
||||
|
||||
use core::f32;
|
||||
use core::f64;
|
||||
|
||||
use {Num, NumCast, ToPrimitive};
|
||||
|
||||
#[cfg(all(not(feature = "std"), feature = "libm"))]
|
||||
use libm::{F32Ext, F64Ext};
|
||||
|
||||
/// Generic trait for floating point numbers that works with `no_std`.
|
||||
///
|
||||
/// This trait implements a subset of the `Float` trait.
|
||||
|
@ -586,7 +589,11 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
|
|||
if other.is_nan() {
|
||||
return self;
|
||||
}
|
||||
if self < other { self } else { other }
|
||||
if self < other {
|
||||
self
|
||||
} else {
|
||||
other
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the maximum of the two numbers.
|
||||
|
@ -616,7 +623,11 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
|
|||
if other.is_nan() {
|
||||
return self;
|
||||
}
|
||||
if self > other { self } else { other }
|
||||
if self > other {
|
||||
self
|
||||
} else {
|
||||
other
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the reciprocal (multiplicative inverse) of the number.
|
||||
|
@ -758,6 +769,8 @@ impl FloatCore for f32 {
|
|||
const EXP_MASK: u32 = 0x7f800000;
|
||||
const MAN_MASK: u32 = 0x007fffff;
|
||||
|
||||
// Safety: this identical to the implementation of f32::to_bits(),
|
||||
// which is only available starting at Rust 1.20
|
||||
let bits: u32 = unsafe { mem::transmute(self) };
|
||||
match (bits & MAN_MASK, bits & EXP_MASK) {
|
||||
(0, 0) => FpCategory::Zero,
|
||||
|
@ -830,6 +843,8 @@ impl FloatCore for f64 {
|
|||
const EXP_MASK: u64 = 0x7ff0000000000000;
|
||||
const MAN_MASK: u64 = 0x000fffffffffffff;
|
||||
|
||||
// Safety: this identical to the implementation of f64::to_bits(),
|
||||
// which is only available starting at Rust 1.20
|
||||
let bits: u64 = unsafe { mem::transmute(self) };
|
||||
match (bits & MAN_MASK, bits & EXP_MASK) {
|
||||
(0, 0) => FpCategory::Zero,
|
||||
|
@ -885,15 +900,9 @@ impl FloatCore for f64 {
|
|||
|
||||
/// Generic trait for floating point numbers
|
||||
///
|
||||
/// This trait is only available with the `std` feature.
|
||||
#[cfg(feature = "std")]
|
||||
pub trait Float
|
||||
: Num
|
||||
+ Copy
|
||||
+ NumCast
|
||||
+ PartialOrd
|
||||
+ Neg<Output = Self>
|
||||
{
|
||||
/// This trait is only available with the `std` feature, or with the `libm` feature otherwise.
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
|
||||
/// Returns the `NaN` value.
|
||||
///
|
||||
/// ```
|
||||
|
@ -1237,8 +1246,10 @@ pub trait Float
|
|||
fn is_sign_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.
|
||||
/// error, yielding a more accurate result than an unfused multiply-add.
|
||||
///
|
||||
/// Using `mul_add` can be more performant than an unfused multiply-add if
|
||||
/// the target architecture has a dedicated `fma` CPU instruction.
|
||||
///
|
||||
/// ```
|
||||
/// use num_traits::Float;
|
||||
|
@ -1775,7 +1786,6 @@ pub trait Float
|
|||
/// ```
|
||||
fn atanh(self) -> Self;
|
||||
|
||||
|
||||
/// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
|
||||
/// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
|
||||
///
|
||||
|
@ -1799,8 +1809,8 @@ pub trait Float
|
|||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
macro_rules! float_impl {
|
||||
($T:ident $decode:ident) => (
|
||||
macro_rules! float_impl_std {
|
||||
($T:ident $decode:ident) => {
|
||||
impl Float for $T {
|
||||
constant! {
|
||||
nan() -> $T::NAN;
|
||||
|
@ -1874,16 +1884,93 @@ macro_rules! float_impl {
|
|||
Self::atanh(self) -> Self;
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(all(not(feature = "std"), feature = "libm"))]
|
||||
macro_rules! float_impl_libm {
|
||||
($T:ident $decode:ident $LibmImpl:ident) => {
|
||||
impl Float for $T {
|
||||
constant! {
|
||||
nan() -> $T::NAN;
|
||||
infinity() -> $T::INFINITY;
|
||||
neg_infinity() -> $T::NEG_INFINITY;
|
||||
neg_zero() -> -0.0;
|
||||
min_value() -> $T::MIN;
|
||||
min_positive_value() -> $T::MIN_POSITIVE;
|
||||
epsilon() -> $T::EPSILON;
|
||||
max_value() -> $T::MAX;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(deprecated)]
|
||||
fn abs_sub(self, other: Self) -> Self {
|
||||
<$T as $LibmImpl>::fdim(self, other)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn integer_decode(self) -> (u64, i16, i8) {
|
||||
$decode(self)
|
||||
}
|
||||
|
||||
forward! {
|
||||
FloatCore::is_nan(self) -> bool;
|
||||
FloatCore::is_infinite(self) -> bool;
|
||||
FloatCore::is_finite(self) -> bool;
|
||||
FloatCore::is_normal(self) -> bool;
|
||||
FloatCore::classify(self) -> FpCategory;
|
||||
$LibmImpl::floor(self) -> Self;
|
||||
$LibmImpl::ceil(self) -> Self;
|
||||
$LibmImpl::round(self) -> Self;
|
||||
$LibmImpl::trunc(self) -> Self;
|
||||
$LibmImpl::fract(self) -> Self;
|
||||
$LibmImpl::abs(self) -> Self;
|
||||
FloatCore::signum(self) -> Self;
|
||||
FloatCore::is_sign_positive(self) -> bool;
|
||||
FloatCore::is_sign_negative(self) -> bool;
|
||||
$LibmImpl::mul_add(self, a: Self, b: Self) -> Self;
|
||||
FloatCore::recip(self) -> Self;
|
||||
FloatCore::powi(self, n: i32) -> Self;
|
||||
$LibmImpl::powf(self, n: Self) -> Self;
|
||||
$LibmImpl::sqrt(self) -> Self;
|
||||
$LibmImpl::exp(self) -> Self;
|
||||
$LibmImpl::exp2(self) -> Self;
|
||||
$LibmImpl::ln(self) -> Self;
|
||||
$LibmImpl::log(self, base: Self) -> Self;
|
||||
$LibmImpl::log2(self) -> Self;
|
||||
$LibmImpl::log10(self) -> Self;
|
||||
FloatCore::to_degrees(self) -> Self;
|
||||
FloatCore::to_radians(self) -> Self;
|
||||
FloatCore::max(self, other: Self) -> Self;
|
||||
FloatCore::min(self, other: Self) -> Self;
|
||||
$LibmImpl::cbrt(self) -> Self;
|
||||
$LibmImpl::hypot(self, other: Self) -> Self;
|
||||
$LibmImpl::sin(self) -> Self;
|
||||
$LibmImpl::cos(self) -> Self;
|
||||
$LibmImpl::tan(self) -> Self;
|
||||
$LibmImpl::asin(self) -> Self;
|
||||
$LibmImpl::acos(self) -> Self;
|
||||
$LibmImpl::atan(self) -> Self;
|
||||
$LibmImpl::atan2(self, other: Self) -> Self;
|
||||
$LibmImpl::sin_cos(self) -> (Self, Self);
|
||||
$LibmImpl::exp_m1(self) -> Self;
|
||||
$LibmImpl::ln_1p(self) -> Self;
|
||||
$LibmImpl::sinh(self) -> Self;
|
||||
$LibmImpl::cosh(self) -> Self;
|
||||
$LibmImpl::tanh(self) -> Self;
|
||||
$LibmImpl::asinh(self) -> Self;
|
||||
$LibmImpl::acosh(self) -> Self;
|
||||
$LibmImpl::atanh(self) -> Self;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
|
||||
// Safety: this identical to the implementation of f32::to_bits(),
|
||||
// which is only available starting at Rust 1.20
|
||||
let bits: u32 = unsafe { mem::transmute(f) };
|
||||
let sign: i8 = if bits >> 31 == 0 {
|
||||
1
|
||||
} else {
|
||||
-1
|
||||
};
|
||||
let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
|
||||
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
|
||||
let mantissa = if exponent == 0 {
|
||||
(bits & 0x7fffff) << 1
|
||||
|
@ -1896,12 +1983,10 @@ fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
|
|||
}
|
||||
|
||||
fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
|
||||
// Safety: this identical to the implementation of f64::to_bits(),
|
||||
// which is only available starting at Rust 1.20
|
||||
let bits: u64 = unsafe { mem::transmute(f) };
|
||||
let sign: i8 = if bits >> 63 == 0 {
|
||||
1
|
||||
} else {
|
||||
-1
|
||||
};
|
||||
let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
|
||||
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
|
||||
let mantissa = if exponent == 0 {
|
||||
(bits & 0xfffffffffffff) << 1
|
||||
|
@ -1914,9 +1999,14 @@ fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
|
|||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
float_impl!(f32 integer_decode_f32);
|
||||
float_impl_std!(f32 integer_decode_f32);
|
||||
#[cfg(feature = "std")]
|
||||
float_impl!(f64 integer_decode_f64);
|
||||
float_impl_std!(f64 integer_decode_f64);
|
||||
|
||||
#[cfg(all(not(feature = "std"), feature = "libm"))]
|
||||
float_impl_libm!(f32 integer_decode_f32 F32Ext);
|
||||
#[cfg(all(not(feature = "std"), feature = "libm"))]
|
||||
float_impl_libm!(f64 integer_decode_f64 F64Ext);
|
||||
|
||||
macro_rules! float_const_impl {
|
||||
($(#[$doc:meta] $constant:ident,)+) => (
|
||||
|
@ -1999,7 +2089,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
#[test]
|
||||
fn convert_deg_rad_std() {
|
||||
for &(deg, rad) in &DEG_RAD_PAIRS {
|
||||
|
@ -2021,6 +2111,9 @@ mod tests {
|
|||
fn to_degrees_rounding() {
|
||||
use float::FloatCore;
|
||||
|
||||
assert_eq!(FloatCore::to_degrees(1_f32), 57.2957795130823208767981548141051703);
|
||||
assert_eq!(
|
||||
FloatCore::to_degrees(1_f32),
|
||||
57.2957795130823208767981548141051703
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
use core::ops::{Add, Mul};
|
||||
use core::num::Wrapping;
|
||||
use core::ops::{Add, Mul};
|
||||
|
||||
/// Defines an additive identity element for `Self`.
|
||||
///
|
||||
/// # Laws
|
||||
///
|
||||
/// ```{.text}
|
||||
/// a + 0 = a ∀ a ∈ Self
|
||||
/// 0 + a = a ∀ a ∈ Self
|
||||
/// ```
|
||||
pub trait Zero: Sized + Add<Self, Output = Self> {
|
||||
/// Returns the additive identity element of `Self`, `0`.
|
||||
///
|
||||
/// # Laws
|
||||
///
|
||||
/// ```{.text}
|
||||
/// a + 0 = a ∀ a ∈ Self
|
||||
/// 0 + a = a ∀ a ∈ Self
|
||||
/// ```
|
||||
///
|
||||
/// # Purity
|
||||
///
|
||||
/// This function should return the same result at all times regardless of
|
||||
|
@ -20,6 +19,11 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
|
|||
// This cannot be an associated constant, because of bignums.
|
||||
fn zero() -> Self;
|
||||
|
||||
/// Sets `self` to the additive identity element of `Self`, `0`.
|
||||
fn set_zero(&mut self) {
|
||||
*self = Zero::zero();
|
||||
}
|
||||
|
||||
/// Returns `true` if `self` is equal to the additive identity.
|
||||
#[inline]
|
||||
fn is_zero(&self) -> bool;
|
||||
|
@ -29,53 +33,64 @@ macro_rules! zero_impl {
|
|||
($t:ty, $v:expr) => {
|
||||
impl Zero for $t {
|
||||
#[inline]
|
||||
fn zero() -> $t { $v }
|
||||
fn zero() -> $t {
|
||||
$v
|
||||
}
|
||||
#[inline]
|
||||
fn is_zero(&self) -> bool { *self == $v }
|
||||
fn is_zero(&self) -> bool {
|
||||
*self == $v
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
zero_impl!(usize, 0);
|
||||
zero_impl!(u8, 0);
|
||||
zero_impl!(u16, 0);
|
||||
zero_impl!(u32, 0);
|
||||
zero_impl!(u64, 0);
|
||||
zero_impl!(u8, 0);
|
||||
zero_impl!(u16, 0);
|
||||
zero_impl!(u32, 0);
|
||||
zero_impl!(u64, 0);
|
||||
#[cfg(has_i128)]
|
||||
zero_impl!(u128, 0);
|
||||
zero_impl!(u128, 0);
|
||||
|
||||
zero_impl!(isize, 0);
|
||||
zero_impl!(i8, 0);
|
||||
zero_impl!(i16, 0);
|
||||
zero_impl!(i32, 0);
|
||||
zero_impl!(i64, 0);
|
||||
zero_impl!(i8, 0);
|
||||
zero_impl!(i16, 0);
|
||||
zero_impl!(i32, 0);
|
||||
zero_impl!(i64, 0);
|
||||
#[cfg(has_i128)]
|
||||
zero_impl!(i128, 0);
|
||||
zero_impl!(i128, 0);
|
||||
|
||||
zero_impl!(f32, 0.0);
|
||||
zero_impl!(f64, 0.0);
|
||||
|
||||
impl<T: Zero> Zero for Wrapping<T> where Wrapping<T>: Add<Output=Wrapping<T>> {
|
||||
impl<T: Zero> Zero for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Add<Output = Wrapping<T>>,
|
||||
{
|
||||
fn is_zero(&self) -> bool {
|
||||
self.0.is_zero()
|
||||
}
|
||||
|
||||
fn set_zero(&mut self) {
|
||||
self.0.set_zero();
|
||||
}
|
||||
|
||||
fn zero() -> Self {
|
||||
Wrapping(T::zero())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Defines a multiplicative identity element for `Self`.
|
||||
///
|
||||
/// # Laws
|
||||
///
|
||||
/// ```{.text}
|
||||
/// a * 1 = a ∀ a ∈ Self
|
||||
/// 1 * a = a ∀ a ∈ Self
|
||||
/// ```
|
||||
pub trait One: Sized + Mul<Self, Output = Self> {
|
||||
/// Returns the multiplicative identity element of `Self`, `1`.
|
||||
///
|
||||
/// # Laws
|
||||
///
|
||||
/// ```{.text}
|
||||
/// a * 1 = a ∀ a ∈ Self
|
||||
/// 1 * a = a ∀ a ∈ Self
|
||||
/// ```
|
||||
///
|
||||
/// # Purity
|
||||
///
|
||||
/// This function should return the same result at all times regardless of
|
||||
|
@ -84,13 +99,21 @@ pub trait One: Sized + Mul<Self, Output = Self> {
|
|||
// This cannot be an associated constant, because of bignums.
|
||||
fn one() -> Self;
|
||||
|
||||
/// Sets `self` to the multiplicative identity element of `Self`, `1`.
|
||||
fn set_one(&mut self) {
|
||||
*self = One::one();
|
||||
}
|
||||
|
||||
/// Returns `true` if `self` is equal to the multiplicative identity.
|
||||
///
|
||||
/// For performance reasons, it's best to implement this manually.
|
||||
/// After a semver bump, this method will be required, and the
|
||||
/// `where Self: PartialEq` bound will be removed.
|
||||
#[inline]
|
||||
fn is_one(&self) -> bool where Self: PartialEq {
|
||||
fn is_one(&self) -> bool
|
||||
where
|
||||
Self: PartialEq,
|
||||
{
|
||||
*self == Self::one()
|
||||
}
|
||||
}
|
||||
|
@ -99,31 +122,44 @@ macro_rules! one_impl {
|
|||
($t:ty, $v:expr) => {
|
||||
impl One for $t {
|
||||
#[inline]
|
||||
fn one() -> $t { $v }
|
||||
fn one() -> $t {
|
||||
$v
|
||||
}
|
||||
#[inline]
|
||||
fn is_one(&self) -> bool {
|
||||
*self == $v
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
one_impl!(usize, 1);
|
||||
one_impl!(u8, 1);
|
||||
one_impl!(u16, 1);
|
||||
one_impl!(u32, 1);
|
||||
one_impl!(u64, 1);
|
||||
one_impl!(u8, 1);
|
||||
one_impl!(u16, 1);
|
||||
one_impl!(u32, 1);
|
||||
one_impl!(u64, 1);
|
||||
#[cfg(has_i128)]
|
||||
one_impl!(u128, 1);
|
||||
one_impl!(u128, 1);
|
||||
|
||||
one_impl!(isize, 1);
|
||||
one_impl!(i8, 1);
|
||||
one_impl!(i16, 1);
|
||||
one_impl!(i32, 1);
|
||||
one_impl!(i64, 1);
|
||||
one_impl!(i8, 1);
|
||||
one_impl!(i16, 1);
|
||||
one_impl!(i32, 1);
|
||||
one_impl!(i64, 1);
|
||||
#[cfg(has_i128)]
|
||||
one_impl!(i128, 1);
|
||||
one_impl!(i128, 1);
|
||||
|
||||
one_impl!(f32, 1.0);
|
||||
one_impl!(f64, 1.0);
|
||||
|
||||
impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
|
||||
impl<T: One> One for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Mul<Output = Wrapping<T>>,
|
||||
{
|
||||
fn set_one(&mut self) {
|
||||
self.0.set_one();
|
||||
}
|
||||
|
||||
fn one() -> Self {
|
||||
Wrapping(T::one())
|
||||
}
|
||||
|
@ -132,11 +168,16 @@ impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
|
|||
// Some helper functions provided for backwards compatibility.
|
||||
|
||||
/// Returns the additive identity, `0`.
|
||||
#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
|
||||
#[inline(always)]
|
||||
pub fn zero<T: Zero>() -> T {
|
||||
Zero::zero()
|
||||
}
|
||||
|
||||
/// Returns the multiplicative identity, `1`.
|
||||
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
|
||||
|
||||
#[inline(always)]
|
||||
pub fn one<T: One>() -> T {
|
||||
One::one()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapping_identities() {
|
||||
|
|
91
src/int.rs
91
src/int.rs
|
@ -1,26 +1,55 @@
|
|||
use core::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
|
||||
use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
|
||||
|
||||
use {Num, NumCast};
|
||||
use bounds::Bounded;
|
||||
use ops::checked::*;
|
||||
use ops::saturating::Saturating;
|
||||
use {Num, NumCast};
|
||||
|
||||
pub trait PrimInt
|
||||
: Sized
|
||||
/// Generic trait for primitive integers.
|
||||
///
|
||||
/// The `PrimInt` trait is an abstraction over the builtin primitive integer types (e.g., `u8`,
|
||||
/// `u32`, `isize`, `i128`, ...). It inherits the basic numeric traits and extends them with
|
||||
/// bitwise operators and non-wrapping arithmetic.
|
||||
///
|
||||
/// The trait explicitly inherits `Copy`, `Eq`, `Ord`, and `Sized`. The intention is that all
|
||||
/// types implementing this trait behave like primitive types that are passed by value by default
|
||||
/// and behave like builtin integers. Furthermore, the types are expected to expose the integer
|
||||
/// value in binary representation and support bitwise operators. The standard bitwise operations
|
||||
/// (e.g., bitwise-and, bitwise-or, right-shift, left-shift) are inherited and the trait extends
|
||||
/// these with introspective queries (e.g., `PrimInt::count_ones()`, `PrimInt::leading_zeros()`),
|
||||
/// bitwise combinators (e.g., `PrimInt::rotate_left()`), and endianness converters (e.g.,
|
||||
/// `PrimInt::to_be()`).
|
||||
///
|
||||
/// All `PrimInt` types are expected to be fixed-width binary integers. The width can be queried
|
||||
/// via `T::zero().count_zeros()`. The trait currently lacks a way to query the width at
|
||||
/// compile-time.
|
||||
///
|
||||
/// While a default implementation for all builtin primitive integers is provided, the trait is in
|
||||
/// no way restricted to these. Other integer types that fulfil the requirements are free to
|
||||
/// implement the trait was well.
|
||||
///
|
||||
/// This trait and many of the method names originate in the unstable `core::num::Int` trait from
|
||||
/// the rust standard library. The original trait was never stabilized and thus removed from the
|
||||
/// standard library.
|
||||
pub trait PrimInt:
|
||||
Sized
|
||||
+ Copy
|
||||
+ Num + NumCast
|
||||
+ Num
|
||||
+ NumCast
|
||||
+ Bounded
|
||||
+ PartialOrd + Ord + Eq
|
||||
+ Not<Output=Self>
|
||||
+ BitAnd<Output=Self>
|
||||
+ BitOr<Output=Self>
|
||||
+ BitXor<Output=Self>
|
||||
+ Shl<usize, Output=Self>
|
||||
+ Shr<usize, Output=Self>
|
||||
+ CheckedAdd<Output=Self>
|
||||
+ CheckedSub<Output=Self>
|
||||
+ CheckedMul<Output=Self>
|
||||
+ CheckedDiv<Output=Self>
|
||||
+ PartialOrd
|
||||
+ Ord
|
||||
+ Eq
|
||||
+ Not<Output = Self>
|
||||
+ BitAnd<Output = Self>
|
||||
+ BitOr<Output = Self>
|
||||
+ BitXor<Output = Self>
|
||||
+ Shl<usize, Output = Self>
|
||||
+ Shr<usize, Output = Self>
|
||||
+ CheckedAdd<Output = Self>
|
||||
+ CheckedSub<Output = Self>
|
||||
+ CheckedMul<Output = Self>
|
||||
+ CheckedDiv<Output = Self>
|
||||
+ Saturating
|
||||
{
|
||||
/// Returns the number of ones in the binary representation of `self`.
|
||||
|
@ -168,10 +197,10 @@ pub trait PrimInt
|
|||
/// ```
|
||||
/// use num_traits::PrimInt;
|
||||
///
|
||||
/// let n = 0xFEDCBA9876543210i64;
|
||||
/// let m = 0x000FEDCBA9876543i64;
|
||||
/// let n = -8i8; // 0b11111000
|
||||
/// let m = 62i8; // 0b00111110
|
||||
///
|
||||
/// assert_eq!(n.unsigned_shr(12), m);
|
||||
/// assert_eq!(n.unsigned_shr(2), m);
|
||||
/// ```
|
||||
fn unsigned_shr(self, n: u32) -> Self;
|
||||
|
||||
|
@ -278,7 +307,7 @@ pub trait PrimInt
|
|||
}
|
||||
|
||||
macro_rules! prim_int_impl {
|
||||
($T:ty, $S:ty, $U:ty) => (
|
||||
($T:ty, $S:ty, $U:ty) => {
|
||||
impl PrimInt for $T {
|
||||
#[inline]
|
||||
fn count_ones(self) -> u32 {
|
||||
|
@ -360,21 +389,21 @@ macro_rules! prim_int_impl {
|
|||
<$T>::pow(self, exp)
|
||||
}
|
||||
}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
// prim_int_impl!(type, signed, unsigned);
|
||||
prim_int_impl!(u8, i8, u8);
|
||||
prim_int_impl!(u16, i16, u16);
|
||||
prim_int_impl!(u32, i32, u32);
|
||||
prim_int_impl!(u64, i64, u64);
|
||||
prim_int_impl!(u8, i8, u8);
|
||||
prim_int_impl!(u16, i16, u16);
|
||||
prim_int_impl!(u32, i32, u32);
|
||||
prim_int_impl!(u64, i64, u64);
|
||||
#[cfg(has_i128)]
|
||||
prim_int_impl!(u128, i128, u128);
|
||||
prim_int_impl!(u128, i128, u128);
|
||||
prim_int_impl!(usize, isize, usize);
|
||||
prim_int_impl!(i8, i8, u8);
|
||||
prim_int_impl!(i16, i16, u16);
|
||||
prim_int_impl!(i32, i32, u32);
|
||||
prim_int_impl!(i64, i64, u64);
|
||||
prim_int_impl!(i8, i8, u8);
|
||||
prim_int_impl!(i16, i16, u16);
|
||||
prim_int_impl!(i32, i32, u32);
|
||||
prim_int_impl!(i64, i64, u64);
|
||||
#[cfg(has_i128)]
|
||||
prim_int_impl!(i128, i128, u128);
|
||||
prim_int_impl!(i128, i128, u128);
|
||||
prim_int_impl!(isize, isize, usize);
|
||||
|
|
204
src/lib.rs
204
src/lib.rs
|
@ -15,53 +15,54 @@
|
|||
//! The `num-traits` crate is tested for rustc 1.8 and greater.
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/num-traits/0.2")]
|
||||
|
||||
#![deny(unconditional_recursion)]
|
||||
|
||||
#![no_std]
|
||||
#[cfg(feature = "std")]
|
||||
extern crate std;
|
||||
|
||||
use core::ops::{Add, Sub, Mul, Div, Rem};
|
||||
use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
|
||||
use core::num::Wrapping;
|
||||
// Only `no_std` builds actually use `libm`.
|
||||
#[cfg(all(not(feature = "std"), feature = "libm"))]
|
||||
extern crate libm;
|
||||
|
||||
use core::fmt;
|
||||
use core::num::Wrapping;
|
||||
use core::ops::{Add, Div, Mul, Rem, Sub};
|
||||
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
|
||||
|
||||
pub use bounds::Bounded;
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
pub use float::Float;
|
||||
pub use float::FloatConst;
|
||||
// pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`.
|
||||
pub use identities::{Zero, One, zero, one};
|
||||
pub use cast::{cast, AsPrimitive, FromPrimitive, NumCast, ToPrimitive};
|
||||
pub use identities::{one, zero, One, Zero};
|
||||
pub use int::PrimInt;
|
||||
pub use ops::checked::{
|
||||
CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub,
|
||||
};
|
||||
pub use ops::inv::Inv;
|
||||
pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
|
||||
CheckedRem, CheckedNeg, CheckedShl, CheckedShr};
|
||||
pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub};
|
||||
pub use ops::mul_add::{MulAdd, MulAddAssign};
|
||||
pub use ops::saturating::Saturating;
|
||||
pub use sign::{Signed, Unsigned, abs, abs_sub, signum};
|
||||
pub use cast::{AsPrimitive, FromPrimitive, ToPrimitive, NumCast, cast};
|
||||
pub use int::PrimInt;
|
||||
pub use pow::{Pow, pow, checked_pow};
|
||||
pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingShl, WrappingShr, WrappingSub};
|
||||
pub use pow::{checked_pow, pow, Pow};
|
||||
pub use sign::{abs, abs_sub, signum, Signed, Unsigned};
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
pub mod identities;
|
||||
pub mod sign;
|
||||
pub mod ops;
|
||||
pub mod bounds;
|
||||
pub mod float;
|
||||
#[cfg(feature = "std")]
|
||||
pub mod real;
|
||||
pub mod cast;
|
||||
pub mod float;
|
||||
pub mod identities;
|
||||
pub mod int;
|
||||
pub mod ops;
|
||||
pub mod pow;
|
||||
pub mod real;
|
||||
pub mod sign;
|
||||
|
||||
/// The base trait for numeric types, covering `0` and `1` values,
|
||||
/// comparisons, basic numeric operations, and string conversion.
|
||||
pub trait Num: PartialEq + Zero + One + NumOps
|
||||
{
|
||||
pub trait Num: PartialEq + Zero + One + NumOps {
|
||||
type FromStrRadixErr;
|
||||
|
||||
/// Convert from a string and radix <= 36.
|
||||
|
@ -83,21 +84,23 @@ pub trait Num: PartialEq + Zero + One + NumOps
|
|||
/// The trait for types implementing basic numeric operations
|
||||
///
|
||||
/// This is automatically implemented for types which implement the operators.
|
||||
pub trait NumOps<Rhs = Self, Output = Self>
|
||||
: Add<Rhs, Output = Output>
|
||||
pub trait NumOps<Rhs = Self, Output = Self>:
|
||||
Add<Rhs, Output = Output>
|
||||
+ Sub<Rhs, Output = Output>
|
||||
+ Mul<Rhs, Output = Output>
|
||||
+ Div<Rhs, Output = Output>
|
||||
+ Rem<Rhs, Output = Output>
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
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>
|
||||
{}
|
||||
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>
|
||||
{
|
||||
}
|
||||
|
||||
/// The trait for `Num` types which also implement numeric operations taking
|
||||
/// the second operand by reference.
|
||||
|
@ -116,21 +119,15 @@ impl<T, Base> RefNum<Base> for T where T: NumOps<Base, Base> + for<'r> NumOps<&'
|
|||
/// The trait for types implementing numeric assignment operators (like `+=`).
|
||||
///
|
||||
/// This is automatically implemented for types which implement the operators.
|
||||
pub trait NumAssignOps<Rhs = Self>
|
||||
: AddAssign<Rhs>
|
||||
+ SubAssign<Rhs>
|
||||
+ MulAssign<Rhs>
|
||||
+ DivAssign<Rhs>
|
||||
+ RemAssign<Rhs>
|
||||
{}
|
||||
pub trait NumAssignOps<Rhs = Self>:
|
||||
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>
|
||||
{}
|
||||
impl<T, Rhs> NumAssignOps<Rhs> for T where
|
||||
T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>
|
||||
{
|
||||
}
|
||||
|
||||
/// The trait for `Num` types which also implement assignment operators.
|
||||
///
|
||||
|
@ -145,7 +142,6 @@ impl<T> NumAssign for T where T: Num + NumAssignOps {}
|
|||
pub trait NumAssignRef: NumAssign + for<'r> NumAssignOps<&'r Self> {}
|
||||
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 {
|
||||
|
@ -164,9 +160,12 @@ int_trait_impl!(Num for usize u8 u16 u32 u64 isize i8 i16 i32 i64);
|
|||
int_trait_impl!(Num for u128 i128);
|
||||
|
||||
impl<T: Num> Num for Wrapping<T>
|
||||
where Wrapping<T>:
|
||||
Add<Output = Wrapping<T>> + Sub<Output = Wrapping<T>>
|
||||
+ Mul<Output = Wrapping<T>> + Div<Output = Wrapping<T>> + Rem<Output = Wrapping<T>>
|
||||
where
|
||||
Wrapping<T>: Add<Output = Wrapping<T>>
|
||||
+ Sub<Output = Wrapping<T>>
|
||||
+ Mul<Output = Wrapping<T>>
|
||||
+ Div<Output = Wrapping<T>>
|
||||
+ Rem<Output = Wrapping<T>>,
|
||||
{
|
||||
type FromStrRadixErr = T::FromStrRadixErr;
|
||||
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
|
||||
|
@ -174,7 +173,6 @@ impl<T: Num> Num for Wrapping<T>
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FloatErrorKind {
|
||||
Empty,
|
||||
|
@ -221,7 +219,12 @@ macro_rules! float_trait_impl {
|
|||
}
|
||||
|
||||
fn slice_shift_char(src: &str) -> Option<(char, &str)> {
|
||||
src.chars().nth(0).map(|ch| (ch, &src[1..]))
|
||||
let mut chars = src.chars();
|
||||
if let Some(ch) = chars.next() {
|
||||
Some((ch, chars.as_str()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
let (is_positive, src) = match slice_shift_char(src) {
|
||||
|
@ -364,6 +367,8 @@ float_trait_impl!(Num for f32 f64);
|
|||
/// If input is less than min then this returns min.
|
||||
/// If input is greater than max then this returns max.
|
||||
/// Otherwise this returns input.
|
||||
///
|
||||
/// **Panics** in debug mode if `!(min <= max)`.
|
||||
#[inline]
|
||||
pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
|
||||
debug_assert!(min <= max, "min must be less than or equal to max");
|
||||
|
@ -376,17 +381,97 @@ pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
|
|||
}
|
||||
}
|
||||
|
||||
/// A value bounded by a minimum value
|
||||
///
|
||||
/// If input is less than min then this returns min.
|
||||
/// Otherwise this returns input.
|
||||
/// `clamp_min(std::f32::NAN, 1.0)` preserves `NAN` different from `f32::min(std::f32::NAN, 1.0)`.
|
||||
///
|
||||
/// **Panics** in debug mode if `!(min == min)`. (This occurs if `min` is `NAN`.)
|
||||
#[inline]
|
||||
pub fn clamp_min<T: PartialOrd>(input: T, min: T) -> T {
|
||||
debug_assert!(min == min, "min must not be NAN");
|
||||
if input < min {
|
||||
min
|
||||
} else {
|
||||
input
|
||||
}
|
||||
}
|
||||
|
||||
/// A value bounded by a maximum value
|
||||
///
|
||||
/// If input is greater than max then this returns max.
|
||||
/// Otherwise this returns input.
|
||||
/// `clamp_max(std::f32::NAN, 1.0)` preserves `NAN` different from `f32::max(std::f32::NAN, 1.0)`.
|
||||
///
|
||||
/// **Panics** in debug mode if `!(max == max)`. (This occurs if `max` is `NAN`.)
|
||||
#[inline]
|
||||
pub fn clamp_max<T: PartialOrd>(input: T, max: T) -> T {
|
||||
debug_assert!(max == max, "max must not be NAN");
|
||||
if input > max {
|
||||
max
|
||||
} else {
|
||||
input
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clamp_test() {
|
||||
// Int test
|
||||
assert_eq!(1, clamp(1, -1, 2));
|
||||
assert_eq!(-1, clamp(-2, -1, 2));
|
||||
assert_eq!(2, clamp(3, -1, 2));
|
||||
assert_eq!(1, clamp_min(1, -1));
|
||||
assert_eq!(-1, clamp_min(-2, -1));
|
||||
assert_eq!(-1, clamp_max(1, -1));
|
||||
assert_eq!(-2, clamp_max(-2, -1));
|
||||
|
||||
// Float test
|
||||
assert_eq!(1.0, clamp(1.0, -1.0, 2.0));
|
||||
assert_eq!(-1.0, clamp(-2.0, -1.0, 2.0));
|
||||
assert_eq!(2.0, clamp(3.0, -1.0, 2.0));
|
||||
assert_eq!(1.0, clamp_min(1.0, -1.0));
|
||||
assert_eq!(-1.0, clamp_min(-2.0, -1.0));
|
||||
assert_eq!(-1.0, clamp_max(1.0, -1.0));
|
||||
assert_eq!(-2.0, clamp_max(-2.0, -1.0));
|
||||
assert!(clamp(::core::f32::NAN, -1.0, 1.0).is_nan());
|
||||
assert!(clamp_min(::core::f32::NAN, 1.0).is_nan());
|
||||
assert!(clamp_max(::core::f32::NAN, 1.0).is_nan());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn clamp_nan_min() {
|
||||
clamp(0., ::core::f32::NAN, 1.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn clamp_nan_max() {
|
||||
clamp(0., -1., ::core::f32::NAN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn clamp_nan_min_max() {
|
||||
clamp(0., ::core::f32::NAN, ::core::f32::NAN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn clamp_min_nan_min() {
|
||||
clamp_min(0., ::core::f32::NAN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn clamp_max_nan_max() {
|
||||
clamp_max(0., ::core::f32::NAN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -400,6 +485,15 @@ fn from_str_radix_unwrap() {
|
|||
assert_eq!(f, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_str_radix_multi_byte_fail() {
|
||||
// Ensure parsing doesn't panic, even on invalid sign characters
|
||||
assert!(f32::from_str_radix("™0.2", 10).is_err());
|
||||
|
||||
// Even when parsing the exponent sign
|
||||
assert!(f32::from_str_radix("0.2E™1", 10).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapping_is_num() {
|
||||
fn require_num<T: Num>(_: &T) {}
|
||||
|
@ -442,7 +536,8 @@ fn check_numref_ops() {
|
|||
#[test]
|
||||
fn check_refnum_ops() {
|
||||
fn compute<T: Copy>(x: &T, y: T) -> T
|
||||
where for<'a> &'a T: RefNum<T>
|
||||
where
|
||||
for<'a> &'a T: RefNum<T>,
|
||||
{
|
||||
&(&(&(&(x * y) / y) % y) + y) - y
|
||||
}
|
||||
|
@ -452,7 +547,8 @@ fn check_refnum_ops() {
|
|||
#[test]
|
||||
fn check_refref_ops() {
|
||||
fn compute<T>(x: &T, y: &T) -> T
|
||||
where for<'a> &'a T: RefNum<T>
|
||||
where
|
||||
for<'a> &'a T: RefNum<T>,
|
||||
{
|
||||
&(&(&(&(x * y) / y) % y) + y) - y
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use core::ops::{Add, Sub, Mul, Div, Rem, Shl, Shr};
|
||||
use core::ops::{Add, Div, Mul, Rem, Shl, Shr, Sub};
|
||||
|
||||
/// Performs addition that returns `None` instead of wrapping around on
|
||||
/// overflow.
|
||||
pub trait CheckedAdd: Sized + Add<Self, Output=Self> {
|
||||
pub trait CheckedAdd: Sized + Add<Self, Output = Self> {
|
||||
/// Adds two numbers, checking for overflow. If overflow happens, `None` is
|
||||
/// returned.
|
||||
fn checked_add(&self, v: &Self) -> Option<Self>;
|
||||
|
@ -16,7 +16,7 @@ macro_rules! checked_impl {
|
|||
<$t>::$method(*self, *v)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
checked_impl!(CheckedAdd, checked_add, u8);
|
||||
|
@ -36,7 +36,7 @@ checked_impl!(CheckedAdd, checked_add, isize);
|
|||
checked_impl!(CheckedAdd, checked_add, i128);
|
||||
|
||||
/// Performs subtraction that returns `None` instead of wrapping around on underflow.
|
||||
pub trait CheckedSub: Sized + Sub<Self, Output=Self> {
|
||||
pub trait CheckedSub: Sized + Sub<Self, Output = Self> {
|
||||
/// Subtracts two numbers, checking for underflow. If underflow happens,
|
||||
/// `None` is returned.
|
||||
fn checked_sub(&self, v: &Self) -> Option<Self>;
|
||||
|
@ -60,7 +60,7 @@ checked_impl!(CheckedSub, checked_sub, i128);
|
|||
|
||||
/// Performs multiplication that returns `None` instead of wrapping around on underflow or
|
||||
/// overflow.
|
||||
pub trait CheckedMul: Sized + Mul<Self, Output=Self> {
|
||||
pub trait CheckedMul: Sized + Mul<Self, Output = Self> {
|
||||
/// Multiplies two numbers, checking for underflow or overflow. If underflow
|
||||
/// or overflow happens, `None` is returned.
|
||||
fn checked_mul(&self, v: &Self) -> Option<Self>;
|
||||
|
@ -84,7 +84,7 @@ checked_impl!(CheckedMul, checked_mul, i128);
|
|||
|
||||
/// Performs division that returns `None` instead of panicking on division by zero and instead of
|
||||
/// wrapping around on underflow and overflow.
|
||||
pub trait CheckedDiv: Sized + Div<Self, Output=Self> {
|
||||
pub trait CheckedDiv: Sized + Div<Self, Output = Self> {
|
||||
/// Divides two numbers, checking for underflow, overflow and division by
|
||||
/// zero. If any of that happens, `None` is returned.
|
||||
fn checked_div(&self, v: &Self) -> Option<Self>;
|
||||
|
@ -155,7 +155,7 @@ macro_rules! checked_impl_unary {
|
|||
<$t>::$method(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Performs negation that returns `None` if the result can't be represented.
|
||||
|
@ -195,10 +195,11 @@ checked_impl_unary!(CheckedNeg, checked_neg, isize);
|
|||
#[cfg(has_i128)]
|
||||
checked_impl_unary!(CheckedNeg, checked_neg, i128);
|
||||
|
||||
/// Performs a left shift that returns `None` on overflow.
|
||||
pub trait CheckedShl: Sized + Shl<u32, Output=Self> {
|
||||
/// Shifts a number to the left, checking for overflow. If overflow happens,
|
||||
/// `None` is returned.
|
||||
/// Performs a left shift that returns `None` on shifts larger than
|
||||
/// the type width.
|
||||
pub trait CheckedShl: Sized + Shl<u32, Output = Self> {
|
||||
/// Checked shift left. Computes `self << rhs`, returning `None`
|
||||
/// if `rhs` is larger than or equal to the number of bits in `self`.
|
||||
///
|
||||
/// ```
|
||||
/// use num_traits::CheckedShl;
|
||||
|
@ -221,7 +222,7 @@ macro_rules! checked_shift_impl {
|
|||
<$t>::$method(*self, rhs)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
checked_shift_impl!(CheckedShl, checked_shl, u8);
|
||||
|
@ -240,10 +241,11 @@ checked_shift_impl!(CheckedShl, checked_shl, isize);
|
|||
#[cfg(has_i128)]
|
||||
checked_shift_impl!(CheckedShl, checked_shl, i128);
|
||||
|
||||
/// Performs a right shift that returns `None` on overflow.
|
||||
pub trait CheckedShr: Sized + Shr<u32, Output=Self> {
|
||||
/// Shifts a number to the left, checking for overflow. If overflow happens,
|
||||
/// `None` is returned.
|
||||
/// Performs a right shift that returns `None` on shifts larger than
|
||||
/// the type width.
|
||||
pub trait CheckedShr: Sized + Shr<u32, Output = Self> {
|
||||
/// Checked shift right. Computes `self >> rhs`, returning `None`
|
||||
/// if `rhs` is larger than or equal to the number of bits in `self`.
|
||||
///
|
||||
/// ```
|
||||
/// use num_traits::CheckedShr;
|
||||
|
|
|
@ -20,20 +20,28 @@ pub trait Inv {
|
|||
impl Inv for f32 {
|
||||
type Output = f32;
|
||||
#[inline]
|
||||
fn inv(self) -> f32 { 1.0 / self }
|
||||
fn inv(self) -> f32 {
|
||||
1.0 / self
|
||||
}
|
||||
}
|
||||
impl Inv for f64 {
|
||||
type Output = f64;
|
||||
#[inline]
|
||||
fn inv(self) -> f64 { 1.0 / self }
|
||||
fn inv(self) -> f64 {
|
||||
1.0 / self
|
||||
}
|
||||
}
|
||||
impl<'a> Inv for &'a f32 {
|
||||
type Output = f32;
|
||||
#[inline]
|
||||
fn inv(self) -> f32 { 1.0 / *self }
|
||||
fn inv(self) -> f32 {
|
||||
1.0 / *self
|
||||
}
|
||||
}
|
||||
impl<'a> Inv for &'a f64 {
|
||||
type Output = f64;
|
||||
#[inline]
|
||||
fn inv(self) -> f64 { 1.0 / *self }
|
||||
fn inv(self) -> f64 {
|
||||
1.0 / *self
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
pub mod saturating;
|
||||
pub mod checked;
|
||||
pub mod wrapping;
|
||||
pub mod inv;
|
||||
pub mod mul_add;
|
||||
pub mod saturating;
|
||||
pub mod wrapping;
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
/// The fused multiply-add operation.
|
||||
/// 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.
|
||||
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
|
||||
/// error, yielding a more accurate result than an unfused multiply-add.
|
||||
///
|
||||
/// Using `mul_add` can be more performant than an unfused multiply-add if
|
||||
/// the target architecture has a dedicated `fma` CPU instruction.
|
||||
///
|
||||
/// Note that `A` and `B` are `Self` by default, but this is not mandatory.
|
||||
///
|
||||
|
@ -17,7 +18,7 @@
|
|||
/// // 100.0
|
||||
/// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
|
||||
///
|
||||
/// assert!(abs_difference <= f32::EPSILON);
|
||||
/// assert!(abs_difference <= 100.0 * f32::EPSILON);
|
||||
/// ```
|
||||
pub trait MulAdd<A = Self, B = Self> {
|
||||
/// The resulting type after applying the fused multiply-add.
|
||||
|
@ -33,23 +34,23 @@ pub trait MulAddAssign<A = Self, B = Self> {
|
|||
fn mul_add_assign(&mut self, a: A, b: B);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
impl MulAdd<f32, f32> for f32 {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn mul_add(self, a: Self, b: Self) -> Self::Output {
|
||||
f32::mul_add(self, a, b)
|
||||
<Self as ::Float>::mul_add(self, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
impl MulAdd<f64, f64> for f64 {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
fn mul_add(self, a: Self, b: Self) -> Self::Output {
|
||||
f64::mul_add(self, a, b)
|
||||
<Self as ::Float>::mul_add(self, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,19 +71,19 @@ mul_add_impl!(MulAdd for isize usize i8 u8 i16 u16 i32 u32 i64 u64);
|
|||
#[cfg(has_i128)]
|
||||
mul_add_impl!(MulAdd for i128 u128);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
impl MulAddAssign<f32, f32> for f32 {
|
||||
#[inline]
|
||||
fn mul_add_assign(&mut self, a: Self, b: Self) {
|
||||
*self = f32::mul_add(*self, a, b)
|
||||
*self = <Self as ::Float>::mul_add(*self, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
impl MulAddAssign<f64, f64> for f64 {
|
||||
#[inline]
|
||||
fn mul_add_assign(&mut self, a: Self, b: Self) {
|
||||
*self = f64::mul_add(*self, a, b)
|
||||
*self = <Self as ::Float>::mul_add(*self, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,7 +140,7 @@ mod tests {
|
|||
|
||||
let abs_difference = (MulAdd::mul_add(m, x, b) - (m*x + b)).abs();
|
||||
|
||||
assert!(abs_difference <= $t::EPSILON);
|
||||
assert!(abs_difference <= 46.4 * $t::EPSILON);
|
||||
}
|
||||
)+
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use core::ops::{Add, Sub, Mul};
|
||||
use core::num::Wrapping;
|
||||
use core::ops::{Add, Mul, Shl, Shr, Sub};
|
||||
|
||||
macro_rules! wrapping_impl {
|
||||
($trait_name:ident, $method:ident, $t:ty) => {
|
||||
|
@ -17,11 +17,11 @@ macro_rules! wrapping_impl {
|
|||
<$t>::$method(*self, *v)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Performs addition that wraps around on overflow.
|
||||
pub trait WrappingAdd: Sized + Add<Self, Output=Self> {
|
||||
pub trait WrappingAdd: Sized + Add<Self, Output = Self> {
|
||||
/// Wrapping (modular) addition. Computes `self + other`, wrapping around at the boundary of
|
||||
/// the type.
|
||||
fn wrapping_add(&self, v: &Self) -> Self;
|
||||
|
@ -44,7 +44,7 @@ wrapping_impl!(WrappingAdd, wrapping_add, isize);
|
|||
wrapping_impl!(WrappingAdd, wrapping_add, i128);
|
||||
|
||||
/// Performs subtraction that wraps around on overflow.
|
||||
pub trait WrappingSub: Sized + Sub<Self, Output=Self> {
|
||||
pub trait WrappingSub: Sized + Sub<Self, Output = Self> {
|
||||
/// Wrapping (modular) subtraction. Computes `self - other`, wrapping around at the boundary
|
||||
/// of the type.
|
||||
fn wrapping_sub(&self, v: &Self) -> Self;
|
||||
|
@ -67,7 +67,7 @@ wrapping_impl!(WrappingSub, wrapping_sub, isize);
|
|||
wrapping_impl!(WrappingSub, wrapping_sub, i128);
|
||||
|
||||
/// Performs multiplication that wraps around on overflow.
|
||||
pub trait WrappingMul: Sized + Mul<Self, Output=Self> {
|
||||
pub trait WrappingMul: Sized + Mul<Self, Output = Self> {
|
||||
/// Wrapping (modular) multiplication. Computes `self * other`, wrapping around at the boundary
|
||||
/// of the type.
|
||||
fn wrapping_mul(&self, v: &Self) -> Self;
|
||||
|
@ -89,35 +89,156 @@ wrapping_impl!(WrappingMul, wrapping_mul, isize);
|
|||
#[cfg(has_i128)]
|
||||
wrapping_impl!(WrappingMul, wrapping_mul, i128);
|
||||
|
||||
macro_rules! wrapping_shift_impl {
|
||||
($trait_name:ident, $method:ident, $t:ty) => {
|
||||
impl $trait_name for $t {
|
||||
#[inline]
|
||||
fn $method(&self, rhs: u32) -> $t {
|
||||
<$t>::$method(*self, rhs)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Performs a left shift that does not panic.
|
||||
pub trait WrappingShl: Sized + Shl<usize, Output = Self> {
|
||||
/// Panic-free bitwise shift-left; yields `self << mask(rhs)`,
|
||||
/// where `mask` removes any high order bits of `rhs` that would
|
||||
/// cause the shift to exceed the bitwidth of the type.
|
||||
///
|
||||
/// ```
|
||||
/// use num_traits::WrappingShl;
|
||||
///
|
||||
/// let x: u16 = 0x0001;
|
||||
///
|
||||
/// assert_eq!(WrappingShl::wrapping_shl(&x, 0), 0x0001);
|
||||
/// assert_eq!(WrappingShl::wrapping_shl(&x, 1), 0x0002);
|
||||
/// assert_eq!(WrappingShl::wrapping_shl(&x, 15), 0x8000);
|
||||
/// assert_eq!(WrappingShl::wrapping_shl(&x, 16), 0x0001);
|
||||
/// ```
|
||||
fn wrapping_shl(&self, rhs: u32) -> Self;
|
||||
}
|
||||
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, u8);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, u16);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, u32);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, u64);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, usize);
|
||||
#[cfg(has_i128)]
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, u128);
|
||||
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, i8);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, i16);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, i32);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, i64);
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, isize);
|
||||
#[cfg(has_i128)]
|
||||
wrapping_shift_impl!(WrappingShl, wrapping_shl, i128);
|
||||
|
||||
/// Performs a right shift that does not panic.
|
||||
pub trait WrappingShr: Sized + Shr<usize, Output = Self> {
|
||||
/// Panic-free bitwise shift-right; yields `self >> mask(rhs)`,
|
||||
/// where `mask` removes any high order bits of `rhs` that would
|
||||
/// cause the shift to exceed the bitwidth of the type.
|
||||
///
|
||||
/// ```
|
||||
/// use num_traits::WrappingShr;
|
||||
///
|
||||
/// let x: u16 = 0x8000;
|
||||
///
|
||||
/// assert_eq!(WrappingShr::wrapping_shr(&x, 0), 0x8000);
|
||||
/// assert_eq!(WrappingShr::wrapping_shr(&x, 1), 0x4000);
|
||||
/// assert_eq!(WrappingShr::wrapping_shr(&x, 15), 0x0001);
|
||||
/// assert_eq!(WrappingShr::wrapping_shr(&x, 16), 0x8000);
|
||||
/// ```
|
||||
fn wrapping_shr(&self, rhs: u32) -> Self;
|
||||
}
|
||||
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, u8);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, u16);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, u32);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, u64);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, usize);
|
||||
#[cfg(has_i128)]
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, u128);
|
||||
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, i8);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, i16);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, i32);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, i64);
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, isize);
|
||||
#[cfg(has_i128)]
|
||||
wrapping_shift_impl!(WrappingShr, wrapping_shr, i128);
|
||||
|
||||
// Well this is a bit funny, but all the more appropriate.
|
||||
impl<T: WrappingAdd> WrappingAdd for Wrapping<T> where Wrapping<T>: Add<Output = Wrapping<T>> {
|
||||
impl<T: WrappingAdd> WrappingAdd for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Add<Output = Wrapping<T>>,
|
||||
{
|
||||
fn wrapping_add(&self, v: &Self) -> Self {
|
||||
Wrapping(self.0.wrapping_add(&v.0))
|
||||
}
|
||||
}
|
||||
impl<T: WrappingSub> WrappingSub for Wrapping<T> where Wrapping<T>: Sub<Output = Wrapping<T>> {
|
||||
impl<T: WrappingSub> WrappingSub for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Sub<Output = Wrapping<T>>,
|
||||
{
|
||||
fn wrapping_sub(&self, v: &Self) -> Self {
|
||||
Wrapping(self.0.wrapping_sub(&v.0))
|
||||
}
|
||||
}
|
||||
impl<T: WrappingMul> WrappingMul for Wrapping<T> where Wrapping<T>: Mul<Output = Wrapping<T>> {
|
||||
impl<T: WrappingMul> WrappingMul for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Mul<Output = Wrapping<T>>,
|
||||
{
|
||||
fn wrapping_mul(&self, v: &Self) -> Self {
|
||||
Wrapping(self.0.wrapping_mul(&v.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: WrappingShl> WrappingShl for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Shl<usize, Output = Wrapping<T>>,
|
||||
{
|
||||
fn wrapping_shl(&self, rhs: u32) -> Self {
|
||||
Wrapping(self.0.wrapping_shl(rhs))
|
||||
}
|
||||
}
|
||||
impl<T: WrappingShr> WrappingShr for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Shr<usize, Output = Wrapping<T>>,
|
||||
{
|
||||
fn wrapping_shr(&self, rhs: u32) -> Self {
|
||||
Wrapping(self.0.wrapping_shr(rhs))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrapping_traits() {
|
||||
fn wrapping_add<T: WrappingAdd>(a: T, b: T) -> T { a.wrapping_add(&b) }
|
||||
fn wrapping_sub<T: WrappingSub>(a: T, b: T) -> T { a.wrapping_sub(&b) }
|
||||
fn wrapping_mul<T: WrappingMul>(a: T, b: T) -> T { a.wrapping_mul(&b) }
|
||||
fn wrapping_add<T: WrappingAdd>(a: T, b: T) -> T {
|
||||
a.wrapping_add(&b)
|
||||
}
|
||||
fn wrapping_sub<T: WrappingSub>(a: T, b: T) -> T {
|
||||
a.wrapping_sub(&b)
|
||||
}
|
||||
fn wrapping_mul<T: WrappingMul>(a: T, b: T) -> T {
|
||||
a.wrapping_mul(&b)
|
||||
}
|
||||
fn wrapping_shl<T: WrappingShl>(a: T, b: u32) -> T {
|
||||
a.wrapping_shl(b)
|
||||
}
|
||||
fn wrapping_shr<T: WrappingShr>(a: T, b: u32) -> T {
|
||||
a.wrapping_shr(b)
|
||||
}
|
||||
assert_eq!(wrapping_add(255, 1), 0u8);
|
||||
assert_eq!(wrapping_sub(0, 1), 255u8);
|
||||
assert_eq!(wrapping_mul(255, 2), 254u8);
|
||||
assert_eq!(wrapping_shl(255, 8), 255u8);
|
||||
assert_eq!(wrapping_shr(255, 8), 255u8);
|
||||
assert_eq!(wrapping_add(255, 1), (Wrapping(255u8) + Wrapping(1u8)).0);
|
||||
assert_eq!(wrapping_sub(0, 1), (Wrapping(0u8) - Wrapping(1u8)).0);
|
||||
assert_eq!(wrapping_mul(255, 2), (Wrapping(255u8) * Wrapping(2u8)).0);
|
||||
assert_eq!(wrapping_shl(255, 8), (Wrapping(255u8) << 8).0);
|
||||
assert_eq!(wrapping_shr(255, 8), (Wrapping(255u8) >> 8).0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -137,3 +258,15 @@ fn wrapping_is_wrappingmul() {
|
|||
fn require_wrappingmul<T: WrappingMul>(_: &T) {}
|
||||
require_wrappingmul(&Wrapping(42));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapping_is_wrappingshl() {
|
||||
fn require_wrappingshl<T: WrappingShl>(_: &T) {}
|
||||
require_wrappingshl(&Wrapping(42));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapping_is_wrappingshr() {
|
||||
fn require_wrappingshr<T: WrappingShr>(_: &T) {}
|
||||
require_wrappingshr(&Wrapping(42));
|
||||
}
|
||||
|
|
65
src/pow.rs
65
src/pow.rs
|
@ -1,6 +1,6 @@
|
|||
use core::ops::Mul;
|
||||
use core::num::Wrapping;
|
||||
use {One, CheckedMul};
|
||||
use core::ops::Mul;
|
||||
use {CheckedMul, One};
|
||||
|
||||
/// Binary operator for raising a value to a power.
|
||||
pub trait Pow<RHS> {
|
||||
|
@ -152,27 +152,30 @@ pow_impl!(Wrapping<isize>);
|
|||
// pow_impl!(usize, u64);
|
||||
// pow_impl!(isize, u64);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", feature = "libm"))]
|
||||
mod float_impls {
|
||||
use super::Pow;
|
||||
use Float;
|
||||
|
||||
pow_impl!(f32, i8, i32, f32::powi);
|
||||
pow_impl!(f32, u8, i32, f32::powi);
|
||||
pow_impl!(f32, i16, i32, f32::powi);
|
||||
pow_impl!(f32, u16, i32, f32::powi);
|
||||
pow_impl!(f32, i32, i32, f32::powi);
|
||||
pow_impl!(f64, i8, i32, f64::powi);
|
||||
pow_impl!(f64, u8, i32, f64::powi);
|
||||
pow_impl!(f64, i16, i32, f64::powi);
|
||||
pow_impl!(f64, u16, i32, f64::powi);
|
||||
pow_impl!(f64, i32, i32, f64::powi);
|
||||
pow_impl!(f32, f32, f32, f32::powf);
|
||||
pow_impl!(f64, f32, f64, f64::powf);
|
||||
pow_impl!(f64, f64, f64, f64::powf);
|
||||
pow_impl!(f32, i8, i32, <f32 as Float>::powi);
|
||||
pow_impl!(f32, u8, i32, <f32 as Float>::powi);
|
||||
pow_impl!(f32, i16, i32, <f32 as Float>::powi);
|
||||
pow_impl!(f32, u16, i32, <f32 as Float>::powi);
|
||||
pow_impl!(f32, i32, i32, <f32 as Float>::powi);
|
||||
pow_impl!(f64, i8, i32, <f64 as Float>::powi);
|
||||
pow_impl!(f64, u8, i32, <f64 as Float>::powi);
|
||||
pow_impl!(f64, i16, i32, <f64 as Float>::powi);
|
||||
pow_impl!(f64, u16, i32, <f64 as Float>::powi);
|
||||
pow_impl!(f64, i32, i32, <f64 as Float>::powi);
|
||||
pow_impl!(f32, f32, f32, <f32 as Float>::powf);
|
||||
pow_impl!(f64, f32, f64, <f64 as Float>::powf);
|
||||
pow_impl!(f64, f64, f64, <f64 as Float>::powf);
|
||||
}
|
||||
|
||||
/// Raises a value to the power of exp, using exponentiation by squaring.
|
||||
///
|
||||
/// Note that `0⁰` (`pow(0, 0)`) returns `1`. Mathematically this is undefined.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
|
@ -180,16 +183,21 @@ mod float_impls {
|
|||
///
|
||||
/// assert_eq!(pow(2i8, 4), 16);
|
||||
/// assert_eq!(pow(6u8, 3), 216);
|
||||
/// assert_eq!(pow(0u8, 0), 1); // Be aware if this case affects you
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
|
||||
if exp == 0 { return T::one() }
|
||||
if exp == 0 {
|
||||
return T::one();
|
||||
}
|
||||
|
||||
while exp & 1 == 0 {
|
||||
base = base.clone() * base;
|
||||
exp >>= 1;
|
||||
}
|
||||
if exp == 1 { return base }
|
||||
if exp == 1 {
|
||||
return base;
|
||||
}
|
||||
|
||||
let mut acc = base.clone();
|
||||
while exp > 1 {
|
||||
|
@ -204,6 +212,8 @@ pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) ->
|
|||
|
||||
/// Raises a value to the power of exp, returning `None` if an overflow occurred.
|
||||
///
|
||||
/// Note that `0⁰` (`checked_pow(0, 0)`) returns `Some(1)`. Mathematically this is undefined.
|
||||
///
|
||||
/// Otherwise same as the `pow` function.
|
||||
///
|
||||
/// # Example
|
||||
|
@ -214,22 +224,31 @@ pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) ->
|
|||
/// assert_eq!(checked_pow(2i8, 4), Some(16));
|
||||
/// assert_eq!(checked_pow(7i8, 8), None);
|
||||
/// assert_eq!(checked_pow(7u32, 8), Some(5_764_801));
|
||||
/// assert_eq!(checked_pow(0u32, 0), Some(1)); // Be aware if this case affect you
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> {
|
||||
if exp == 0 { return Some(T::one()) }
|
||||
if exp == 0 {
|
||||
return Some(T::one());
|
||||
}
|
||||
|
||||
macro_rules! optry {
|
||||
( $ expr : expr ) => {
|
||||
if let Some(val) = $expr { val } else { return None }
|
||||
}
|
||||
($expr:expr) => {
|
||||
if let Some(val) = $expr {
|
||||
val
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
while exp & 1 == 0 {
|
||||
base = optry!(base.checked_mul(&base));
|
||||
exp >>= 1;
|
||||
}
|
||||
if exp == 1 { return Some(base) }
|
||||
if exp == 1 {
|
||||
return Some(base);
|
||||
}
|
||||
|
||||
let mut acc = base.clone();
|
||||
while exp > 1 {
|
||||
|
|
22
src/real.rs
22
src/real.rs
|
@ -1,6 +1,8 @@
|
|||
use std::ops::Neg;
|
||||
#![cfg(any(feature = "std", feature = "libm"))]
|
||||
|
||||
use {Num, NumCast, Float};
|
||||
use core::ops::Neg;
|
||||
|
||||
use {Float, Num, NumCast};
|
||||
|
||||
// NOTE: These doctests have the same issue as those in src/float.rs.
|
||||
// They're testing the inherent methods directly, and not those of `Real`.
|
||||
|
@ -11,14 +13,8 @@ use {Num, NumCast, Float};
|
|||
/// See [this Wikipedia article](https://en.wikipedia.org/wiki/Real_data_type)
|
||||
/// for a list of data types that could meaningfully implement this trait.
|
||||
///
|
||||
/// This trait is only available with the `std` feature.
|
||||
pub trait Real
|
||||
: Num
|
||||
+ Copy
|
||||
+ NumCast
|
||||
+ PartialOrd
|
||||
+ Neg<Output = Self>
|
||||
{
|
||||
/// This trait is only available with the `std` feature, or with the `libm` feature otherwise.
|
||||
pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
|
||||
/// Returns the smallest finite value that this type can represent.
|
||||
///
|
||||
/// ```
|
||||
|
@ -215,8 +211,10 @@ pub trait Real
|
|||
fn is_sign_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.
|
||||
/// error, yielding a more accurate result than an unfused multiply-add.
|
||||
///
|
||||
/// Using `mul_add` can be more performant than an unfused multiply-add if
|
||||
/// the target architecture has a dedicated `fma` CPU instruction.
|
||||
///
|
||||
/// ```
|
||||
/// use num_traits::real::Real;
|
||||
|
|
37
src/sign.rs
37
src/sign.rs
|
@ -1,8 +1,8 @@
|
|||
use core::ops::Neg;
|
||||
use core::num::Wrapping;
|
||||
use core::ops::Neg;
|
||||
|
||||
use Num;
|
||||
use float::FloatCore;
|
||||
use Num;
|
||||
|
||||
/// Useful functions for signed numbers (i.e. numbers that can be negative).
|
||||
pub trait Signed: Sized + Num + Neg<Output = Self> {
|
||||
|
@ -77,7 +77,9 @@ signed_impl!(isize i8 i16 i32 i64);
|
|||
#[cfg(has_i128)]
|
||||
signed_impl!(i128);
|
||||
|
||||
impl<T: Signed> Signed for Wrapping<T> where Wrapping<T>: Num + Neg<Output=Wrapping<T>>
|
||||
impl<T: Signed> Signed for Wrapping<T>
|
||||
where
|
||||
Wrapping<T>: Num + Neg<Output = Wrapping<T>>,
|
||||
{
|
||||
#[inline]
|
||||
fn abs(&self) -> Self {
|
||||
|
@ -95,10 +97,14 @@ impl<T: Signed> Signed for Wrapping<T> where Wrapping<T>: Num + Neg<Output=Wrapp
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn is_positive(&self) -> bool { self.0.is_positive() }
|
||||
fn is_positive(&self) -> bool {
|
||||
self.0.is_positive()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_negative(&self) -> bool { self.0.is_negative() }
|
||||
fn is_negative(&self) -> bool {
|
||||
self.0.is_negative()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! signed_float_impl {
|
||||
|
@ -115,7 +121,11 @@ macro_rules! signed_float_impl {
|
|||
/// and `other` is returned.
|
||||
#[inline]
|
||||
fn abs_sub(&self, other: &$t) -> $t {
|
||||
if *self <= *other { 0. } else { *self - *other }
|
||||
if *self <= *other {
|
||||
0.
|
||||
} else {
|
||||
*self - *other
|
||||
}
|
||||
}
|
||||
|
||||
/// # Returns
|
||||
|
@ -130,13 +140,17 @@ macro_rules! signed_float_impl {
|
|||
|
||||
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
|
||||
#[inline]
|
||||
fn is_positive(&self) -> bool { FloatCore::is_sign_positive(*self) }
|
||||
fn is_positive(&self) -> bool {
|
||||
FloatCore::is_sign_positive(*self)
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
|
||||
#[inline]
|
||||
fn is_negative(&self) -> bool { FloatCore::is_sign_negative(*self) }
|
||||
fn is_negative(&self) -> bool {
|
||||
FloatCore::is_sign_negative(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
signed_float_impl!(f32);
|
||||
|
@ -174,7 +188,10 @@ pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
|
|||
/// * `0` if the number is zero
|
||||
/// * `1` if the number is positive
|
||||
/// * `-1` if the number is negative
|
||||
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
|
||||
#[inline(always)]
|
||||
pub fn signum<T: Signed>(value: T) -> T {
|
||||
value.signum()
|
||||
}
|
||||
|
||||
/// A trait for values which cannot be negative
|
||||
pub trait Unsigned: Num {}
|
||||
|
|
|
@ -9,13 +9,15 @@ extern crate std;
|
|||
extern crate num_traits;
|
||||
|
||||
use num_traits::cast::*;
|
||||
use num_traits::Bounded;
|
||||
|
||||
use core::{i8, i16, i32, i64, isize};
|
||||
use core::{u8, u16, u32, u64, usize};
|
||||
use core::{f32, f64};
|
||||
#[cfg(has_i128)]
|
||||
use core::{i128, u128};
|
||||
use core::{i16, i32, i64, i8, isize};
|
||||
use core::{u16, u32, u64, u8, usize};
|
||||
|
||||
use core::fmt::Debug;
|
||||
use core::mem;
|
||||
use core::num::Wrapping;
|
||||
|
||||
|
@ -317,3 +319,78 @@ fn cast_int_to_128_edge_cases() {
|
|||
test_edge!(usize u8 u16 u32 u64 u128);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newtype_from_primitive() {
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct New<T>(T);
|
||||
|
||||
// minimal impl
|
||||
impl<T: FromPrimitive> FromPrimitive for New<T> {
|
||||
fn from_i64(n: i64) -> Option<Self> {
|
||||
T::from_i64(n).map(New)
|
||||
}
|
||||
|
||||
fn from_u64(n: u64) -> Option<Self> {
|
||||
T::from_u64(n).map(New)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! assert_eq_from {
|
||||
($( $from:ident )+) => {$(
|
||||
assert_eq!(T::$from(Bounded::min_value()).map(New),
|
||||
New::<T>::$from(Bounded::min_value()));
|
||||
assert_eq!(T::$from(Bounded::max_value()).map(New),
|
||||
New::<T>::$from(Bounded::max_value()));
|
||||
)+}
|
||||
}
|
||||
|
||||
fn check<T: PartialEq + Debug + FromPrimitive>() {
|
||||
assert_eq_from!(from_i8 from_i16 from_i32 from_i64 from_isize);
|
||||
assert_eq_from!(from_u8 from_u16 from_u32 from_u64 from_usize);
|
||||
assert_eq_from!(from_f32 from_f64);
|
||||
}
|
||||
|
||||
macro_rules! check {
|
||||
($( $ty:ty )+) => {$( check::<$ty>(); )+}
|
||||
}
|
||||
check!(i8 i16 i32 i64 isize);
|
||||
check!(u8 u16 u32 u64 usize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newtype_to_primitive() {
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct New<T>(T);
|
||||
|
||||
// minimal impl
|
||||
impl<T: ToPrimitive> ToPrimitive for New<T> {
|
||||
fn to_i64(&self) -> Option<i64> {
|
||||
self.0.to_i64()
|
||||
}
|
||||
|
||||
fn to_u64(&self) -> Option<u64> {
|
||||
self.0.to_u64()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! assert_eq_to {
|
||||
($( $to:ident )+) => {$(
|
||||
assert_eq!(T::$to(&Bounded::min_value()),
|
||||
New::<T>::$to(&New(Bounded::min_value())));
|
||||
assert_eq!(T::$to(&Bounded::max_value()),
|
||||
New::<T>::$to(&New(Bounded::max_value())));
|
||||
)+}
|
||||
}
|
||||
|
||||
fn check<T: PartialEq + Debug + Bounded + ToPrimitive>() {
|
||||
assert_eq_to!(to_i8 to_i16 to_i32 to_i64 to_isize);
|
||||
assert_eq_to!(to_u8 to_u16 to_u32 to_u64 to_usize);
|
||||
assert_eq_to!(to_f32 to_f64);
|
||||
}
|
||||
|
||||
macro_rules! check {
|
||||
($( $ty:ty )+) => {$( check::<$ty>(); )+}
|
||||
}
|
||||
check!(i8 i16 i32 i64 isize);
|
||||
check!(u8 u16 u32 u64 usize);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue