pub trait PrimitiveFloat:
PrimitiveNumber
+ From<i8>
+ From<u8>
+ Neg<Output = Self> {
type Bits: PrimitiveUnsigned;
Show 33 associated constants and 58 methods
const DIGITS: u32;
const EPSILON: Self;
const INFINITY: Self;
const MANTISSA_DIGITS: u32;
const MAX: Self;
const MAX_10_EXP: i32;
const MAX_EXP: i32;
const MIN: Self;
const MIN_10_EXP: i32;
const MIN_EXP: i32;
const MIN_POSITIVE: Self;
const NAN: Self;
const NEG_INFINITY: Self;
const RADIX: u32;
const E: Self;
const FRAC_1_PI: Self;
const FRAC_1_SQRT_2: Self;
const FRAC_2_PI: Self;
const FRAC_2_SQRT_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 LN_2: Self;
const LN_10: Self;
const LOG2_10: Self;
const LOG2_E: Self;
const LOG10_2: Self;
const LOG10_E: Self;
const PI: Self;
const SQRT_2: Self;
const TAU: Self;
// Required methods
fn abs(self) -> Self;
fn clamp(self, min: Self, max: Self) -> Self;
fn classify(self) -> FpCategory;
fn copysign(self, sign: Self) -> Self;
fn from_bits(value: Self::Bits) -> Self;
fn is_finite(self) -> bool;
fn is_infinite(self) -> bool;
fn is_nan(self) -> bool;
fn is_normal(self) -> bool;
fn is_sign_negative(self) -> bool;
fn is_sign_positive(self) -> bool;
fn is_subnormal(self) -> bool;
fn max(self, other: Self) -> Self;
fn midpoint(self, other: Self) -> Self;
fn min(self, other: Self) -> Self;
fn recip(self) -> Self;
fn signum(self) -> Self;
fn to_bits(self) -> Self::Bits;
fn to_degrees(self) -> Self;
fn to_radians(self) -> Self;
fn total_cmp(&self, other: &Self) -> Ordering;
unsafe fn to_int_unchecked<Int>(self) -> Int
where Self: PrimitiveFloatToInt<Int>;
fn acos(self) -> Self;
fn acosh(self) -> Self;
fn asin(self) -> Self;
fn asinh(self) -> Self;
fn atan(self) -> Self;
fn atan2(self, other: Self) -> Self;
fn atanh(self) -> Self;
fn cbrt(self) -> Self;
fn ceil(self) -> Self;
fn cos(self) -> Self;
fn cosh(self) -> Self;
fn div_euclid(self, rhs: Self) -> Self;
fn exp(self) -> Self;
fn exp2(self) -> Self;
fn exp_m1(self) -> Self;
fn floor(self) -> Self;
fn fract(self) -> Self;
fn hypot(self, other: Self) -> Self;
fn ln(self) -> Self;
fn ln_1p(self) -> Self;
fn log(self, base: Self) -> Self;
fn log2(self) -> Self;
fn log10(self) -> Self;
fn mul_add(self, a: Self, b: Self) -> Self;
fn powf(self, n: Self) -> Self;
fn powi(self, n: i32) -> Self;
fn rem_euclid(self, rhs: Self) -> Self;
fn round(self) -> Self;
fn round_ties_even(self) -> Self;
fn sin(self) -> Self;
fn sin_cos(self) -> (Self, Self);
fn sinh(self) -> Self;
fn sqrt(self) -> Self;
fn tan(self) -> Self;
fn tanh(self) -> Self;
fn trunc(self) -> Self;
}Expand description
Trait for all primitive floating-point types, including the supertrait PrimitiveNumber.
This encapsulates trait implementations, constants, and inherent methods that are common among
the primitive floating-point types, f32 and f64. Unstable types f16 and f128
will be added once they are stabilized.
See the corresponding items on the individual types for more documentation and examples.
This trait is sealed with a private trait to prevent downstream implementations, so we may continue to expand along with the standard library without worrying about breaking changes for implementors.
§Examples
This example requires the std feature for powi and sqrt:
use num_primitive::PrimitiveFloat;
// Euclidean distance, √(∑(aᵢ - bᵢ)²)
fn distance<T: PrimitiveFloat>(a: &[T], b: &[T]) -> T {
assert_eq!(a.len(), b.len());
core::iter::zip(a, b).map(|(a, b)| (*a - b).powi(2)).sum::<T>().sqrt()
}
assert_eq!(distance::<f32>(&[0., 0.], &[3., 4.]), 5.);
assert_eq!(distance::<f64>(&[0., 1., 2.], &[1., 3., 0.]), 3.);This example works without any features:
use num_primitive::PrimitiveFloat;
// Squared Euclidean distance, ∑(aᵢ - bᵢ)²
fn distance_squared<T: PrimitiveFloat>(a: &[T], b: &[T]) -> T {
assert_eq!(a.len(), b.len());
core::iter::zip(a, b).map(|(a, b)| (*a - b)).map(|x| x * x).sum::<T>()
}
assert_eq!(distance_squared::<f32>(&[0., 0.], &[3., 4.]), 25.);
assert_eq!(distance_squared::<f64>(&[0., 1., 2.], &[1., 3., 0.]), 9.);Required Associated Constants§
Sourceconst MANTISSA_DIGITS: u32
const MANTISSA_DIGITS: u32
Number of significant digits in base 2.
Sourceconst MAX_10_EXP: i32
const MAX_10_EXP: i32
Maximum x for which 10x is normal.
Sourceconst MIN_10_EXP: i32
const MIN_10_EXP: i32
Minimum x for which 10x is normal.
Sourceconst MIN_POSITIVE: Self
const MIN_POSITIVE: Self
Smallest positive normal value.
Sourceconst NEG_INFINITY: Self
const NEG_INFINITY: Self
Negative infinity (−∞).
Sourceconst FRAC_1_SQRT_2: Self
const FRAC_1_SQRT_2: Self
1/sqrt(2)
Sourceconst FRAC_2_SQRT_PI: Self
const FRAC_2_SQRT_PI: Self
2/sqrt(π)
Required Associated Types§
Sourcetype Bits: PrimitiveUnsigned
type Bits: PrimitiveUnsigned
Required Methods§
Sourcefn clamp(self, min: Self, max: Self) -> Self
fn clamp(self, min: Self, max: Self) -> Self
Restrict a value to a certain interval unless it is NaN.
Sourcefn classify(self) -> FpCategory
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.
Sourcefn copysign(self, sign: Self) -> Self
fn copysign(self, sign: Self) -> Self
Returns a number composed of the magnitude of self and the sign of sign.
Sourcefn is_infinite(self) -> bool
fn is_infinite(self) -> bool
Returns true if this value is positive infinity or negative infinity.
Sourcefn is_normal(self) -> bool
fn is_normal(self) -> bool
Returns true if the number is neither zero, infinite, subnormal, or NaN.
Sourcefn is_sign_negative(self) -> bool
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.
Sourcefn is_sign_positive(self) -> bool
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.
Sourcefn is_subnormal(self) -> bool
fn is_subnormal(self) -> bool
Returns true if the number is subnormal.
Sourcefn to_degrees(self) -> Self
fn to_degrees(self) -> Self
Converts radians to degrees.
Sourcefn to_radians(self) -> Self
fn to_radians(self) -> Self
Converts degrees to radians.
Sourceunsafe fn to_int_unchecked<Int>(self) -> Intwhere
Self: PrimitiveFloatToInt<Int>,
unsafe fn to_int_unchecked<Int>(self) -> Intwhere
Self: PrimitiveFloatToInt<Int>,
Rounds toward zero and converts to any primitive integer type, assuming that the value is finite and fits in that type.
§Safety
The value must:
- Not be
NaN - Not be infinite
- Be representable in the return type
Int, after truncating off its fractional part
Sourcefn acos(self) -> Self
Available on crate feature std only.
fn acos(self) -> Self
std only.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].
Sourcefn asin(self) -> Self
Available on crate feature std only.
fn asin(self) -> Self
std only.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].
Sourcefn atan(self) -> Self
Available on crate feature std only.
fn atan(self) -> Self
std only.Computes the arctangent of a number. Return value is in radians in the range [-pi/2, pi/2];
Sourcefn atan2(self, other: Self) -> Self
Available on crate feature std only.
fn atan2(self, other: Self) -> Self
std only.Computes the four quadrant arctangent of self (y) and other (x) in radians.
Sourcefn atanh(self) -> Self
Available on crate feature std only.
fn atanh(self) -> Self
std only.Inverse hyperbolic tangent function.
Sourcefn ceil(self) -> Self
Available on crate feature std only.
fn ceil(self) -> Self
std only.Returns the smallest integer greater than or equal to self.
Sourcefn cos(self) -> Self
Available on crate feature std only.
fn cos(self) -> Self
std only.Computes the cosine of a number (in radians).
Sourcefn div_euclid(self, rhs: Self) -> Self
Available on crate feature std only.
fn div_euclid(self, rhs: Self) -> Self
std only.Calculates Euclidean division, the matching method for rem_euclid.
Sourcefn exp(self) -> Self
Available on crate feature std only.
fn exp(self) -> Self
std only.Returns e^(self), (the exponential function).
Sourcefn exp_m1(self) -> Self
Available on crate feature std only.
fn exp_m1(self) -> Self
std only.Returns e^(self) - 1 in a way that is accurate even if the number is close to zero.
Sourcefn floor(self) -> Self
Available on crate feature std only.
fn floor(self) -> Self
std only.Returns the largest integer less than or equal to self.
Sourcefn fract(self) -> Self
Available on crate feature std only.
fn fract(self) -> Self
std only.Returns the fractional part of self.
Sourcefn hypot(self, other: Self) -> Self
Available on crate feature std only.
fn hypot(self, other: Self) -> Self
std only.Compute the distance between the origin and a point (x, y) on the Euclidean plane.
Equivalently, compute the length of the hypotenuse of a right-angle triangle with other
sides having length x.abs() and y.abs().
Sourcefn ln(self) -> Self
Available on crate feature std only.
fn ln(self) -> Self
std only.Returns the natural logarithm of the number.
Sourcefn ln_1p(self) -> Self
Available on crate feature std only.
fn ln_1p(self) -> Self
std only.Returns ln(1+n) (natural logarithm) more accurately than if the operations were performed
separately.
Sourcefn log(self, base: Self) -> Self
Available on crate feature std only.
fn log(self, base: Self) -> Self
std only.Returns the logarithm of the number with respect to an arbitrary base.
Sourcefn log2(self) -> Self
Available on crate feature std only.
fn log2(self) -> Self
std only.Returns the base 2 logarithm of the number.
Sourcefn log10(self) -> Self
Available on crate feature std only.
fn log10(self) -> Self
std only.Returns the base 10 logarithm of the number.
Sourcefn mul_add(self, a: Self, b: Self) -> Self
Available on crate feature std only.
fn mul_add(self, a: Self, b: Self) -> Self
std only.Fused multiply-add. Computes (self * a) + b with only one rounding error, yielding a more
accurate result than an unfused multiply-add.
Sourcefn powf(self, n: Self) -> Self
Available on crate feature std only.
fn powf(self, n: Self) -> Self
std only.Raises a number to a floating point power.
Sourcefn powi(self, n: i32) -> Self
Available on crate feature std only.
fn powi(self, n: i32) -> Self
std only.Raises a number to an integer power.
Sourcefn rem_euclid(self, rhs: Self) -> Self
Available on crate feature std only.
fn rem_euclid(self, rhs: Self) -> Self
std only.Calculates the least nonnegative remainder of self (mod rhs).
Sourcefn round(self) -> Self
Available on crate feature std only.
fn round(self) -> Self
std only.Returns the nearest integer to self. If a value is half-way between two integers, round
away from 0.0.
Sourcefn round_ties_even(self) -> Self
Available on crate feature std only.
fn round_ties_even(self) -> Self
std only.Returns the nearest integer to a number. Rounds half-way cases to the number with an even least significant digit.
Sourcefn sin(self) -> Self
Available on crate feature std only.
fn sin(self) -> Self
std only.Computes the sine of a number (in radians).
Sourcefn sin_cos(self) -> (Self, Self)
Available on crate feature std only.
fn sin_cos(self) -> (Self, Self)
std only.Simultaneously computes the sine and cosine of the number, x. Returns (sin(x), cos(x)).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl PrimitiveFloat for f32
impl PrimitiveFloat for f32
Source§fn classify(self) -> FpCategory
fn classify(self) -> FpCategory
See the inherent classify method.
Source§fn is_infinite(self) -> bool
fn is_infinite(self) -> bool
See the inherent is_infinite method.
Source§fn is_sign_negative(self) -> bool
fn is_sign_negative(self) -> bool
See the inherent is_sign_negative method.
Source§fn is_sign_positive(self) -> bool
fn is_sign_positive(self) -> bool
See the inherent is_sign_positive method.
Source§fn is_subnormal(self) -> bool
fn is_subnormal(self) -> bool
See the inherent is_subnormal method.
Source§fn to_degrees(self) -> Self
fn to_degrees(self) -> Self
See the inherent to_degrees method.
Source§fn to_radians(self) -> Self
fn to_radians(self) -> Self
See the inherent to_radians method.
Source§unsafe fn to_int_unchecked<Int>(self) -> Intwhere
Self: PrimitiveFloatToInt<Int>,
unsafe fn to_int_unchecked<Int>(self) -> Intwhere
Self: PrimitiveFloatToInt<Int>,
See the inherent to_int_unchecked method.
Source§fn atan2(self, other: Self) -> Self
Available on crate feature std only.
fn atan2(self, other: Self) -> Self
std only.See the inherent atan2 method.
Source§fn div_euclid(self, rhs: Self) -> Self
Available on crate feature std only.
fn div_euclid(self, rhs: Self) -> Self
std only.See the inherent div_euclid method.
Source§fn hypot(self, other: Self) -> Self
Available on crate feature std only.
fn hypot(self, other: Self) -> Self
std only.See the inherent hypot method.
Source§fn log(self, base: Self) -> Self
Available on crate feature std only.
fn log(self, base: Self) -> Self
std only.See the inherent log method.
Source§fn mul_add(self, a: Self, b: Self) -> Self
Available on crate feature std only.
fn mul_add(self, a: Self, b: Self) -> Self
std only.See the inherent mul_add method.
Source§fn powf(self, n: Self) -> Self
Available on crate feature std only.
fn powf(self, n: Self) -> Self
std only.See the inherent powf method.
Source§fn powi(self, n: i32) -> Self
Available on crate feature std only.
fn powi(self, n: i32) -> Self
std only.See the inherent powi method.
Source§fn rem_euclid(self, rhs: Self) -> Self
Available on crate feature std only.
fn rem_euclid(self, rhs: Self) -> Self
std only.See the inherent rem_euclid method.
Source§fn round_ties_even(self) -> Self
Available on crate feature std only.
fn round_ties_even(self) -> Self
std only.See the inherent round_ties_even method.
Source§fn sin_cos(self) -> (Self, Self)
Available on crate feature std only.
fn sin_cos(self) -> (Self, Self)
std only.See the inherent sin_cos method.
const DIGITS: u32 = 6u32
const EPSILON: Self = 1.1920929E-7f32
const INFINITY: Self = +Inf_f32
const MANTISSA_DIGITS: u32 = 24u32
const MAX: Self = 3.40282347E+38f32
const MAX_10_EXP: i32 = 38i32
const MAX_EXP: i32 = 128i32
const MIN: Self = -3.40282347E+38f32
const MIN_10_EXP: i32 = -37i32
const MIN_EXP: i32 = -125i32
const MIN_POSITIVE: Self = 1.17549435E-38f32
const NAN: Self = NaN_f32
const NEG_INFINITY: Self = -Inf_f32
const RADIX: u32 = 2u32
const E: Self = 2.71828175f32
const FRAC_1_PI: Self = 0.318309873f32
const FRAC_1_SQRT_2: Self = 0.707106769f32
const FRAC_2_PI: Self = 0.636619746f32
const FRAC_2_SQRT_PI: Self = 1.12837923f32
const FRAC_PI_2: Self = 1.57079637f32
const FRAC_PI_3: Self = 1.04719758f32
const FRAC_PI_4: Self = 0.785398185f32
const FRAC_PI_6: Self = 0.52359879f32
const FRAC_PI_8: Self = 0.392699093f32
const LN_2: Self = 0.693147182f32
const LN_10: Self = 2.30258512f32
const LOG2_10: Self = 3.32192802f32
const LOG2_E: Self = 1.44269502f32
const LOG10_2: Self = 0.30103001f32
const LOG10_E: Self = 0.434294492f32
const PI: Self = 3.14159274f32
const SQRT_2: Self = 1.41421354f32
const TAU: Self = 6.28318548f32
type Bits = u32
Source§impl PrimitiveFloat for f64
impl PrimitiveFloat for f64
Source§fn classify(self) -> FpCategory
fn classify(self) -> FpCategory
See the inherent classify method.
Source§fn is_infinite(self) -> bool
fn is_infinite(self) -> bool
See the inherent is_infinite method.
Source§fn is_sign_negative(self) -> bool
fn is_sign_negative(self) -> bool
See the inherent is_sign_negative method.
Source§fn is_sign_positive(self) -> bool
fn is_sign_positive(self) -> bool
See the inherent is_sign_positive method.
Source§fn is_subnormal(self) -> bool
fn is_subnormal(self) -> bool
See the inherent is_subnormal method.
Source§fn to_degrees(self) -> Self
fn to_degrees(self) -> Self
See the inherent to_degrees method.
Source§fn to_radians(self) -> Self
fn to_radians(self) -> Self
See the inherent to_radians method.
Source§unsafe fn to_int_unchecked<Int>(self) -> Intwhere
Self: PrimitiveFloatToInt<Int>,
unsafe fn to_int_unchecked<Int>(self) -> Intwhere
Self: PrimitiveFloatToInt<Int>,
See the inherent to_int_unchecked method.
Source§fn atan2(self, other: Self) -> Self
Available on crate feature std only.
fn atan2(self, other: Self) -> Self
std only.See the inherent atan2 method.
Source§fn div_euclid(self, rhs: Self) -> Self
Available on crate feature std only.
fn div_euclid(self, rhs: Self) -> Self
std only.See the inherent div_euclid method.
Source§fn hypot(self, other: Self) -> Self
Available on crate feature std only.
fn hypot(self, other: Self) -> Self
std only.See the inherent hypot method.
Source§fn log(self, base: Self) -> Self
Available on crate feature std only.
fn log(self, base: Self) -> Self
std only.See the inherent log method.
Source§fn mul_add(self, a: Self, b: Self) -> Self
Available on crate feature std only.
fn mul_add(self, a: Self, b: Self) -> Self
std only.See the inherent mul_add method.
Source§fn powf(self, n: Self) -> Self
Available on crate feature std only.
fn powf(self, n: Self) -> Self
std only.See the inherent powf method.
Source§fn powi(self, n: i32) -> Self
Available on crate feature std only.
fn powi(self, n: i32) -> Self
std only.See the inherent powi method.
Source§fn rem_euclid(self, rhs: Self) -> Self
Available on crate feature std only.
fn rem_euclid(self, rhs: Self) -> Self
std only.See the inherent rem_euclid method.
Source§fn round_ties_even(self) -> Self
Available on crate feature std only.
fn round_ties_even(self) -> Self
std only.See the inherent round_ties_even method.
Source§fn sin_cos(self) -> (Self, Self)
Available on crate feature std only.
fn sin_cos(self) -> (Self, Self)
std only.See the inherent sin_cos method.