[][src]Trait funty::IsFloat

pub trait IsFloat: IsNumber + LowerExp + UpperExp + Neg + From<f32> + From<i8> + From<i16> + From<u8> + From<u16> {
    type Raw: IsUnsigned;

    const RADIX: u32;
    const MANTISSA_DIGITS: u32;
    const DIGITS: u32;
    const EPSILON: Self;
    const MIN: Self;
    const MIN_POSITIVE: Self;
    const MAX: Self;
    const MIN_EXP: i32;
    const MAX_EXP: i32;
    const MIN_10_EXP: i32;
    const MAX_10_EXP: i32;
    const NAN: Self;
    const INFINITY: Self;
    const NEG_INFINITY: Self;
    const PI: Self;
    const FRAC_PI_2: Self;
    const FRAC_PI_3: Self;
    const FRAC_PI_4: Self;
    const FRAC_PI_6: Self;
    const FRAC_PI_8: Self;
    const FRAC_1_PI: Self;
    const FRAC_2_PI: Self;
    const FRAC_2_SQRT_PI: Self;
    const SQRT_2: Self;
    const FRAC_1_SQRT_2: Self;
    const E: Self;
    const LOG2_E: Self;
    const LOG10_E: Self;
    const LN_2: Self;
    const LN_10: Self;

    fn floor(self) -> Self;
fn ceil(self) -> Self;
fn round(self) -> Self;
fn trunc(self) -> Self;
fn fract(self) -> Self;
fn abs(self) -> Self;
fn signum(self) -> Self;
fn copysign(self, sign: Self) -> Self;
fn mul_add(self, a: Self, b: Self) -> Self;
fn div_euclid(self, rhs: Self) -> Self;
fn rem_euclid(self, rhs: Self) -> Self;
fn powi(self, n: i32) -> Self;
fn powf(self, n: Self) -> Self;
fn sqrt(self) -> Self;
fn exp(self) -> Self;
fn exp2(self) -> Self;
fn ln(self) -> Self;
fn log(self, base: Self) -> Self;
fn log2(self) -> Self;
fn log10(self) -> Self;
fn cbrt(self) -> Self;
fn hypot(self, other: Self) -> Self;
fn sin(self) -> Self;
fn cos(self) -> Self;
fn tan(self) -> Self;
fn asin(self) -> Self;
fn acos(self) -> Self;
fn atan(self) -> Self;
fn atan2(self, other: Self) -> Self;
fn sin_cos(self) -> (Self, Self);
fn exp_m1(self) -> Self;
fn ln_1p(self) -> Self;
fn sinh(self) -> Self;
fn cosh(self) -> Self;
fn tanh(self) -> Self;
fn asinh(self) -> Self;
fn acosh(self) -> Self;
fn atanh(self) -> Self;
fn is_nan(self) -> bool;
fn is_infinite(self) -> bool;
fn is_finite(self) -> bool;
fn is_normal(self) -> bool;
fn classify(self) -> FpCategory;
fn is_sign_positive(self) -> bool;
fn is_sign_negative(self) -> bool;
fn recip(self) -> Self;
fn to_degrees(self) -> Self;
fn to_radians(self) -> Self;
fn max(self, other: Self) -> Self;
fn min(self, other: Self) -> Self;
fn to_bits(self) -> Self::Raw;
fn from_bits(bits: Self::Raw) -> Self; }

Declare that a type is a floating-point number.

Associated Types

type Raw: IsUnsigned

The unsigned integer type of the same width as Self.

Loading content...

Associated Constants

const RADIX: u32

The radix or base of the internal representation of f32.

const MANTISSA_DIGITS: u32

Number of significant digits in base 2.

const DIGITS: u32

Approximate number of significant digits in base 10.

const EPSILON: Self

Machine epsilon value for f32.

This is the difference between 1.0 and the next larger representable number.

const MIN: Self

Smallest finite f32 value.

const MIN_POSITIVE: Self

Smallest positive normal f32 value.

const MAX: Self

Largest finite f32 value.

const MIN_EXP: i32

One greater than the minimum possible normal power of 2 exponent.

const MAX_EXP: i32

Maximum possible power of 2 exponent.

const MIN_10_EXP: i32

Minimum possible normal power of 10 exponent.

const MAX_10_EXP: i32

Maximum possible power of 10 exponent.

const NAN: Self

Not a Number (NaN).

const INFINITY: Self

Infinity (∞).

const NEG_INFINITY: Self

Negative infinity (−∞).

const PI: Self

Archimedes' constant (π)

const FRAC_PI_2: Self

π/2

const FRAC_PI_3: Self

π/3

const FRAC_PI_4: Self

π/4

const FRAC_PI_6: Self

π/6

const FRAC_PI_8: Self

π/8

const FRAC_1_PI: Self

1/π

const FRAC_2_PI: Self

2/π

const FRAC_2_SQRT_PI: Self

2/sqrt(π)

const SQRT_2: Self

sqrt(2)

const FRAC_1_SQRT_2: Self

1/sqrt(2)

const E: Self

Euler’s number (e)

const LOG2_E: Self

log2(e)

const LOG10_E: Self

log10(e)

const LN_2: Self

ln(2)

const LN_10: Self

ln(10)

Loading content...

Required methods

fn floor(self) -> Self

Returns the largest integer less than or equal to a number.

fn ceil(self) -> Self

Returns the smallest integer greater than or equal to a number.

fn round(self) -> Self

Returns the nearest integer to a number. Round half-way cases away from 0.0.

fn trunc(self) -> Self

Returns the integer part of a number.

fn fract(self) -> Self

Returns the fractional part of a number.

fn abs(self) -> Self

Computes the absolute value of self. Returns NAN if the number is NAN.

fn signum(self) -> Self

Returns a number that represents the sign of self.

  • 1.0 if the number is positive, +0.0 or INFINITY
  • -1.0 if the number is negative, -0.0 or NEG_INFINITY
  • NAN if the number is NAN

fn copysign(self, sign: Self) -> Self

Returns a number composed of the magnitude of self and the sign of sign.

Equal to self if the sign of self and sign are the same, otherwise equal to -self. If self is a NAN, then a NAN with the sign of sign is returned.

fn mul_add(self, a: Self, b: Self) -> Self

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.

fn div_euclid(self, rhs: Self) -> Self

Calculates Euclidean division, the matching method for rem_euclid.

This computes the integer n such that self = n * rhs + self.rem_euclid(rhs). In other words, the result is self / rhs rounded to the integer n such that self >= n * rhs.

fn rem_euclid(self, rhs: Self) -> Self

Calculates the least nonnegative remainder of self (mod rhs).

In particular, the return value r satisfies 0.0 <= r < rhs.abs() in most cases. However, due to a floating point round-off error it can result in r == rhs.abs(), violating the mathematical definition, if self is much smaller than rhs.abs() in magnitude and self < 0.0. This result is not an element of the function's codomain, but it is the closest floating point number in the real numbers and thus fulfills the property self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs) approximatively.

fn powi(self, n: i32) -> Self

Raises a number to an integer power.

Using this function is generally faster than using powf

fn powf(self, n: Self) -> Self

Raises a number to a floating point power.

fn sqrt(self) -> Self

Returns the square root of a number.

Returns NaN if self is a negative number.

fn exp(self) -> Self

Returns e^(self), (the exponential function).

fn exp2(self) -> Self

Returns 2^(self).

fn ln(self) -> Self

Returns the natural logarithm of the number.

fn log(self, base: Self) -> Self

Returns the logarithm of the number with respect to an arbitrary base.

The result may not be correctly rounded owing to implementation details; self.log2() can produce more accurate results for base 2, and self.log10() can produce more accurate results for base 10.

fn log2(self) -> Self

Returns the base 2 logarithm of the number.

fn log10(self) -> Self

Returns the base 10 logarithm of the number.

fn cbrt(self) -> Self

Returns the cubic root of a number.

fn hypot(self, other: Self) -> Self

Computes the sine of a number (in radians).

fn sin(self) -> Self

Computes the sine of a number (in radians).

fn cos(self) -> Self

Computes the cosine of a number (in radians).

fn tan(self) -> Self

Computes the tangent of a number (in radians).

fn asin(self) -> Self

Computes the arcsine of a number. Return value is in radians in the range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1].

fn acos(self) -> Self

Computes the arccosine of a number. Return value is in radians in the range [0, pi] or NaN if the number is outside the range [-1, 1].

fn atan(self) -> Self

Computes the arctangent of a number. Return value is in radians in the range [-pi/2, pi/2];

fn atan2(self, other: Self) -> Self

Computes the four quadrant arctangent of self (y) and other (x) in radians.

  • x = 0, y = 0: 0
  • x >= 0: arctan(y/x) -> [-pi/2, pi/2]
  • y >= 0: arctan(y/x) + pi -> (pi/2, pi]
  • y < 0: arctan(y/x) - pi -> (-pi, -pi/2)

fn sin_cos(self) -> (Self, Self)

Simultaneously computes the sine and cosine of the number, x. Returns (sin(x), cos(x)).

fn exp_m1(self) -> Self

Returns e^(self) - 1 in a way that is accurate even if the number is close to zero.

fn ln_1p(self) -> Self

Returns ln(1+n) (natural logarithm) more accurately than if the operations were performed separately.

fn sinh(self) -> Self

Hyperbolic sine function.

fn cosh(self) -> Self

Hyperbolic cosine function.

fn tanh(self) -> Self

Hyperbolic tangent function.

fn asinh(self) -> Self

Inverse hyperbolic sine function.

fn acosh(self) -> Self

Inverse hyperbolic cosine function.

fn atanh(self) -> Self

Inverse hyperbolic tangent function.

fn is_nan(self) -> bool

Returns true if this value is NaN.

fn is_infinite(self) -> bool

Returns true if this value is positive infinity or negative infinity, and false otherwise.

fn is_finite(self) -> bool

Returns true if this number is neither infinite nor NaN.

fn is_normal(self) -> bool

Returns true if the number is neither zero, infinite, subnormal, or NaN.

fn classify(self) -> FpCategory

Returns the floating point category of the number. If only one property is going to be tested, it is generally faster to use the specific predicate instead.

fn is_sign_positive(self) -> bool

Returns true if self has a positive sign, including +0.0, NaNs with positive sign bit and positive infinity.

fn is_sign_negative(self) -> bool

Returns true if self has a negative sign, including -0.0, NaNs with negative sign bit and negative infinity.

fn recip(self) -> Self

Takes the reciprocal (inverse) of a number, 1/x.

fn to_degrees(self) -> Self

Converts radians to degrees.

fn to_radians(self) -> Self

Converts degrees to radians.

fn max(self, other: Self) -> Self

Returns the maximum of the two numbers.

fn min(self, other: Self) -> Self

Returns the minimum of the two numbers.

fn to_bits(self) -> Self::Raw

Raw transmutation to u32.

This is currently identical to transmute::<f32, u32>(self) on all platforms.

See from_bits for some discussion of the portability of this operation (there are almost no issues).

Note that this function is distinct from as casting, which attempts to preserve the numeric value, and not the bitwise value.

fn from_bits(bits: Self::Raw) -> Self

Raw transmutation from u32.

This is currently identical to transmute::<u32, f32>(v) on all platforms. It turns out this is incredibly portable, for two reasons:

  • Floats and Ints have the same endianness on all supported platforms.
  • IEEE-754 very precisely specifies the bit layout of floats.

However there is one caveat: prior to the 2008 version of IEEE-754, how to interpret the NaN signaling bit wasn't actually specified. Most platforms (notably x86 and ARM) picked the interpretation that was ultimately standardized in 2008, but some didn't (notably MIPS). As a result, all signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.

Rather than trying to preserve signaling-ness cross-platform, this implementation favors preserving the exact bits. This means that any payloads encoded in NaNs will be preserved even if the result of this method is sent over the network from an x86 machine to a MIPS one.

If the results of this method are only manipulated by the same architecture that produced them, then there is no portability concern.

If the input isn't NaN, then there is no portability concern.

If you don't care about signalingness (very likely), then there is no portability concern.

Note that this function is distinct from as casting, which attempts to preserve the numeric value, and not the bitwise value.

Loading content...

Implementations on Foreign Types

impl IsFloat for f32[src]

type Raw = u32

impl IsFloat for f64[src]

type Raw = u64

Loading content...

Implementors

Loading content...