Use a single global guard in mod real

This commit is contained in:
Josh Stone 2019-09-27 10:51:21 -07:00
parent 27b9202ff3
commit b64ee3809c
1 changed files with 51 additions and 102 deletions

View File

@ -1,9 +1,8 @@
#![cfg(any(feature = "std", feature = "libm"))]
use core::ops::Neg;
use {Num, NumCast};
#[cfg(any(feature = "std", feature = "libm"))]
use Float;
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`.
@ -14,14 +13,11 @@ use 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 always available, however it requires either `std` or `libm`
/// in order for `f32` and `f64` to implement it.
#[cfg(any(feature = "std", feature = "libm"))]
/// 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.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -33,8 +29,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the smallest positive, normalized value that this type can represent.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -46,8 +41,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns epsilon, a small positive value.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -64,8 +58,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the largest finite value that this type can represent.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -76,8 +69,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the largest integer less than or equal to a number.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let f = 3.99;
@ -90,8 +82,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the smallest integer greater than or equal to a number.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let f = 3.01;
@ -105,8 +96,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the nearest integer to a number. Round half-way cases away from
/// `0.0`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let f = 3.3;
@ -119,8 +109,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Return the integer part of a number.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let f = 3.3;
@ -133,8 +122,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the fractional part of a number.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 3.5;
@ -150,8 +138,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Computes the absolute value of `self`. Returns `Float::nan()` if the
/// number is `Float::nan()`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -174,8 +161,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
/// - `Float::nan()` if the number is `Float::nan()`
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -191,8 +177,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns `true` if `self` is positive, including `+0.0`,
/// `Float::infinity()`, and with newer versions of Rust `f64::NAN`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -210,8 +195,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns `true` if `self` is negative, including `-0.0`,
/// `Float::neg_infinity()`, and with newer versions of Rust `-f64::NAN`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -232,8 +216,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Using `mul_add` can be more performant than an unfused multiply-add if
/// the target architecture has a dedicated `fma` CPU instruction.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let m = 10.0;
@ -249,8 +232,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Take the reciprocal (inverse) of a number, `1/x`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 2.0;
@ -264,8 +246,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
///
/// Using this function is generally faster than using `powf`
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 2.0;
@ -277,8 +258,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Raise a number to a real number power.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 2.0;
@ -296,8 +276,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
///
/// If the implementing type doesn't support NaN, this method should panic if `self < 0`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let positive = 4.0;
@ -312,8 +291,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns `e^(self)`, (the exponential function).
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let one = 1.0;
@ -329,8 +307,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns `2^(self)`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let f = 2.0;
@ -348,8 +325,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
///
/// If `self <= 0` and this type does not support a NaN representation, this function should panic.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let one = 1.0;
@ -369,8 +345,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
///
/// If `self <= 0` and this type does not support a NaN representation, this function should panic.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let ten = 10.0;
@ -393,8 +368,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
///
/// If `self <= 0` and this type does not support a NaN representation, this function should panic.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let two = 2.0;
@ -413,8 +387,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// If `self <= 0` and this type does not support a NaN representation, this function should panic.
///
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let ten = 10.0;
@ -428,8 +401,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Converts radians to degrees.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use std::f64::consts;
///
/// let angle = consts::PI;
@ -442,8 +414,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Converts degrees to radians.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use std::f64::consts;
///
/// let angle = 180.0_f64;
@ -456,8 +427,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the maximum of the two numbers.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 1.0;
@ -469,8 +439,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the minimum of the two numbers.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 1.0;
@ -485,8 +454,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// * If `self <= other`: `0:0`
/// * Else: `self - other`
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 3.0;
@ -502,8 +470,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Take the cubic root of a number.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 8.0;
@ -518,8 +485,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Calculate the length of the hypotenuse of a right-angle triangle given
/// legs of length `x` and `y`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 2.0;
@ -534,8 +500,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Computes the sine of a number (in radians).
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -549,8 +514,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Computes the cosine of a number (in radians).
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -564,8 +528,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Computes the tangent of a number (in radians).
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -585,8 +548,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// If this type does not support a NaN representation, this function should panic
/// if the number is outside the range [-1, 1].
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -608,8 +570,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// If this type does not support a NaN representation, this function should panic
/// if the number is outside the range [-1, 1].
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -625,8 +586,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Computes the arctangent of a number. Return value is in radians in the
/// range [-pi/2, pi/2];
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let f = 1.0;
@ -645,8 +605,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
/// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -671,8 +630,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Simultaneously computes the sine and cosine of the number, `x`. Returns
/// `(sin(x), cos(x))`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -690,8 +648,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns `e^(self) - 1` in a way that is accurate even if the
/// number is close to zero.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 7.0;
@ -711,8 +668,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// If this type does not support a NaN representation, this function should panic
/// if `self-1 <= 0`.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -727,8 +683,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Hyperbolic sine function.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -746,8 +701,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Hyperbolic cosine function.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -765,8 +719,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Hyperbolic tangent function.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -784,8 +737,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Inverse hyperbolic sine function.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 1.0;
@ -799,8 +751,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Inverse hyperbolic cosine function.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
///
/// let x = 1.0;
@ -814,8 +765,7 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// Inverse hyperbolic tangent function.
///
#[cfg_attr(any(feature = "std", feature = "libm"), doc = " ```")]
#[cfg_attr(not(any(feature = "std", feature = "libm")), doc = " ```ignore")]
/// ```
/// use num_traits::real::Real;
/// use std::f64;
///
@ -829,7 +779,6 @@ pub trait Real: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
fn atanh(self) -> Self;
}
#[cfg(any(feature = "std", feature = "libm"))]
impl<T: Float> Real for T {
forward! {
Float::min_value() -> Self;