Trait Float
trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self>
Generic trait for floating point numbers
This trait is only available with the std feature, or with the libm feature otherwise.
Required Methods
fn nan() -> SelfReturns the
NaNvalue.use Float; let nan: f32 = nan; assert!;fn infinity() -> SelfReturns the infinite value.
use Float; use f32; let infinity: f32 = infinity; assert!; assert!; assert!;fn neg_infinity() -> SelfReturns the negative infinite value.
use Float; use f32; let neg_infinity: f32 = neg_infinity; assert!; assert!; assert!;fn neg_zero() -> SelfReturns
-0.0.use ; let inf: f32 = infinity; let zero: f32 = zero; let neg_zero: f32 = neg_zero; assert_eq!; assert_eq!; assert_eq!;fn min_value() -> SelfReturns the smallest finite value that this type can represent.
use Float; use f64; let x: f64 = min_value; assert_eq!;fn min_positive_value() -> SelfReturns the smallest positive, normalized value that this type can represent.
use Float; use f64; let x: f64 = min_positive_value; assert_eq!;fn max_value() -> SelfReturns the largest finite value that this type can represent.
use Float; use f64; let x: f64 = max_value; assert_eq!;fn is_nan(self: Self) -> boolReturns
trueif this value isNaNand false otherwise.use Float; use f64; let nan = f64NAN; let f = 7.0; assert!; assert!;fn is_infinite(self: Self) -> boolReturns
trueif this value is positive infinity or negative infinity and false otherwise.use Float; use f32; let f = 7.0f32; let inf: f32 = infinity; let neg_inf: f32 = neg_infinity; let nan: f32 = f32NAN; assert!; assert!; assert!; assert!;fn is_finite(self: Self) -> boolReturns
trueif this number is neither infinite norNaN.use Float; use f32; let f = 7.0f32; let inf: f32 = infinity; let neg_inf: f32 = neg_infinity; let nan: f32 = f32NAN; assert!; assert!; assert!; assert!;fn is_normal(self: Self) -> boolReturns
trueif the number is neither zero, infinite, subnormal, orNaN.use Float; use f32; let min = f32MIN_POSITIVE; // 1.17549435e-38f32 let max = f32MAX; let lower_than_min = 1.0e-40_f32; let zero = 0.0f32; assert!; assert!; assert!; assert!; assert!; // Values between `0` and `min` are Subnormal. assert!;fn classify(self: Self) -> FpCategoryReturns 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.
use Float; use FpCategory; use f32; let num = 12.4f32; let inf = f32INFINITY; assert_eq!; assert_eq!;fn floor(self: Self) -> SelfReturns the largest integer less than or equal to a number.
use Float; let f = 3.99; let g = 3.0; assert_eq!; assert_eq!;fn ceil(self: Self) -> SelfReturns the smallest integer greater than or equal to a number.
use Float; let f = 3.01; let g = 4.0; assert_eq!; assert_eq!;fn round(self: Self) -> SelfReturns the nearest integer to a number. Round half-way cases away from
0.0.use Float; let f = 3.3; let g = -3.3; assert_eq!; assert_eq!;fn trunc(self: Self) -> SelfReturn the integer part of a number.
use Float; let f = 3.3; let g = -3.7; assert_eq!; assert_eq!;fn fract(self: Self) -> SelfReturns the fractional part of a number.
use Float; let x = 3.5; let y = -3.5; let abs_difference_x = .abs; let abs_difference_y = .abs; assert!; assert!;fn abs(self: Self) -> SelfComputes the absolute value of
self. ReturnsFloat::nan()if the number isFloat::nan().use Float; use f64; let x = 3.5; let y = -3.5; let abs_difference_x = .abs; let abs_difference_y = .abs; assert!; assert!; assert!;fn signum(self: Self) -> SelfReturns a number that represents the sign of
self.1.0if the number is positive,+0.0orFloat::infinity()-1.0if the number is negative,-0.0orFloat::neg_infinity()Float::nan()if the number isFloat::nan()
use Float; use f64; let f = 3.5; assert_eq!; assert_eq!; assert!;fn is_sign_positive(self: Self) -> boolReturns
trueifselfis positive, including+0.0,Float::infinity(), andFloat::nan().use Float; use f64; let nan: f64 = f64NAN; let neg_nan: f64 = -f64NAN; let f = 7.0; let g = -7.0; assert!; assert!; assert!; assert!;fn is_sign_negative(self: Self) -> boolReturns
trueifselfis negative, including-0.0,Float::neg_infinity(), and-Float::nan().use Float; use f64; let nan: f64 = f64NAN; let neg_nan: f64 = -f64NAN; let f = 7.0; let g = -7.0; assert!; assert!; assert!; assert!;fn mul_add(self: Self, a: Self, b: Self) -> SelfFused multiply-add. Computes
(self * a) + bwith only one rounding error, yielding a more accurate result than an unfused multiply-add.Using
mul_addcan be more performant than an unfused multiply-add if the target architecture has a dedicatedfmaCPU instruction.use Float; let m = 10.0; let x = 4.0; let b = 60.0; // 100.0 let abs_difference = .abs; assert!;fn recip(self: Self) -> SelfTake the reciprocal (inverse) of a number,
1/x.use Float; let x = 2.0; let abs_difference = .abs; assert!;fn powi(self: Self, n: i32) -> SelfRaise a number to an integer power.
Using this function is generally faster than using
powfuse Float; let x = 2.0; let abs_difference = .abs; assert!;fn powf(self: Self, n: Self) -> SelfRaise a number to a floating point power.
use Float; let x = 2.0; let abs_difference = .abs; assert!;fn sqrt(self: Self) -> SelfTake the square root of a number.
Returns NaN if
selfis a negative number.use Float; let positive = 4.0; let negative = -4.0; let abs_difference = .abs; assert!; assert!;fn exp(self: Self) -> SelfReturns
e^(self), (the exponential function).use Float; let one = 1.0; // e^1 let e = one.exp; // ln(e) - 1 == 0 let abs_difference = .abs; assert!;fn exp2(self: Self) -> SelfReturns
2^(self).use Float; let f = 2.0; // 2^2 - 4 == 0 let abs_difference = .abs; assert!;fn ln(self: Self) -> SelfReturns the natural logarithm of the number.
use Float; let one = 1.0; // e^1 let e = one.exp; // ln(e) - 1 == 0 let abs_difference = .abs; assert!;fn log(self: Self, base: Self) -> SelfReturns the logarithm of the number with respect to an arbitrary base.
use Float; let ten = 10.0; let two = 2.0; // log10(10) - 1 == 0 let abs_difference_10 = .abs; // log2(2) - 1 == 0 let abs_difference_2 = .abs; assert!; assert!;fn log2(self: Self) -> SelfReturns the base 2 logarithm of the number.
use Float; let two = 2.0; // log2(2) - 1 == 0 let abs_difference = .abs; assert!;fn log10(self: Self) -> SelfReturns the base 10 logarithm of the number.
use Float; let ten = 10.0; // log10(10) - 1 == 0 let abs_difference = .abs; assert!;fn max(self: Self, other: Self) -> SelfReturns the maximum of the two numbers.
use Float; let x = 1.0; let y = 2.0; assert_eq!;fn min(self: Self, other: Self) -> SelfReturns the minimum of the two numbers.
use Float; let x = 1.0; let y = 2.0; assert_eq!;fn abs_sub(self: Self, other: Self) -> SelfThe positive difference of two numbers.
- If
self <= other:0:0 - Else:
self - other
use Float; let x = 3.0; let y = -3.0; let abs_difference_x = .abs; let abs_difference_y = .abs; assert!; assert!;- If
fn cbrt(self: Self) -> SelfTake the cubic root of a number.
use Float; let x = 8.0; // x^(1/3) - 2 == 0 let abs_difference = .abs; assert!;fn hypot(self: Self, other: Self) -> SelfCalculate the length of the hypotenuse of a right-angle triangle given legs of length
xandy.use Float; let x = 2.0; let y = 3.0; // sqrt(x^2 + y^2) let abs_difference = .abs; assert!;fn sin(self: Self) -> SelfComputes the sine of a number (in radians).
use Float; use f64; let x = f64PI/2.0; let abs_difference = .abs; assert!;fn cos(self: Self) -> SelfComputes the cosine of a number (in radians).
use Float; use f64; let x = 2.0*f64PI; let abs_difference = .abs; assert!;fn tan(self: Self) -> SelfComputes the tangent of a number (in radians).
use Float; use f64; let x = f64PI/4.0; let abs_difference = .abs; assert!;fn asin(self: Self) -> SelfComputes 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].
use Float; use f64; let f = f64PI / 2.0; // asin(sin(pi/2)) let abs_difference = .abs; assert!;fn acos(self: Self) -> SelfComputes 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].
use Float; use f64; let f = f64PI / 4.0; // acos(cos(pi/4)) let abs_difference = .abs; assert!;fn atan(self: Self) -> SelfComputes the arctangent of a number. Return value is in radians in the range [-pi/2, pi/2];
use Float; let f = 1.0; // atan(tan(1)) let abs_difference = .abs; assert!;fn atan2(self: Self, other: Self) -> SelfComputes the four quadrant arctangent of
self(y) andother(x).x = 0,y = 0:0x >= 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)
use Float; use f64; let pi = f64PI; // All angles from horizontal right (+x) // 45 deg counter-clockwise let x1 = 3.0; let y1 = -3.0; // 135 deg clockwise let x2 = -3.0; let y2 = 3.0; let abs_difference_1 = .abs; let abs_difference_2 = .abs; assert!; assert!;fn sin_cos(self: Self) -> (Self, Self)Simultaneously computes the sine and cosine of the number,
x. Returns(sin(x), cos(x)).use Float; use f64; let x = f64PI/4.0; let f = x.sin_cos; let abs_difference_0 = .abs; let abs_difference_1 = .abs; assert!; assert!;fn exp_m1(self: Self) -> SelfReturns
e^(self) - 1in a way that is accurate even if the number is close to zero.use Float; let x = 7.0; // e^(ln(7)) - 1 let abs_difference = .abs; assert!;fn ln_1p(self: Self) -> SelfReturns
ln(1+n)(natural logarithm) more accurately than if the operations were performed separately.use Float; use f64; let x = f64E - 1.0; // ln(1 + (e - 1)) == ln(e) == 1 let abs_difference = .abs; assert!;fn sinh(self: Self) -> SelfHyperbolic sine function.
use Float; use f64; let e = f64E; let x = 1.0; let f = x.sinh; // Solving sinh() at 1 gives `(e^2-1)/(2e)` let g = /; let abs_difference = .abs; assert!;fn cosh(self: Self) -> SelfHyperbolic cosine function.
use Float; use f64; let e = f64E; let x = 1.0; let f = x.cosh; // Solving cosh() at 1 gives this result let g = /; let abs_difference = .abs; // Same result assert!;fn tanh(self: Self) -> SelfHyperbolic tangent function.
use Float; use f64; let e = f64E; let x = 1.0; let f = x.tanh; // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))` let g = /; let abs_difference = .abs; assert!;fn asinh(self: Self) -> SelfInverse hyperbolic sine function.
use Float; let x = 1.0; let f = x.sinh.asinh; let abs_difference = .abs; assert!;fn acosh(self: Self) -> SelfInverse hyperbolic cosine function.
use Float; let x = 1.0; let f = x.cosh.acosh; let abs_difference = .abs; assert!;fn atanh(self: Self) -> SelfInverse hyperbolic tangent function.
use Float; use f64; let e = f64E; let f = e.tanh.atanh; let abs_difference = .abs; assert!;fn integer_decode(self: Self) -> (u64, i16, i8)Returns the mantissa, base 2 exponent, and sign as integers, respectively. The original number can be recovered by
sign * mantissa * 2 ^ exponent.use Float; let num = 2.0f32; // (8388608, -22, 1) let = integer_decode; let sign_f = sign as f32; let mantissa_f = mantissa as f32; let exponent_f = num.powf; // 1 * 8388608 * 2^(-22) == 2 let abs_difference = .abs; assert!;
Provided Methods
fn epsilon() -> SelfReturns epsilon, a small positive value.
use Float; use f64; let x: f64 = epsilon; assert_eq!;Panics
The default implementation will panic if
f32::EPSILONcannot be cast toSelf.fn is_subnormal(self: Self) -> boolReturns
trueif the number is subnormal.use Float; use f64; let min = f64MIN_POSITIVE; // 2.2250738585072014e-308_f64 let max = f64MAX; let lower_than_min = 1.0e-308_f64; let zero = 0.0_f64; assert!; assert!; assert!; assert!; assert!; // Values between `0` and `min` are Subnormal. assert!;fn to_degrees(self: Self) -> SelfConverts radians to degrees.
use consts; let angle = PI; let abs_difference = .abs; assert!;fn to_radians(self: Self) -> SelfConverts degrees to radians.
use consts; let angle = 180.0_f64; let abs_difference = .abs; assert!;fn clamp(self: Self, min: Self, max: Self) -> SelfClamps a value between a min and max.
Panics in debug mode if
!(min <= max).use Float; let x = 1.0; let y = 2.0; let z = 3.0; assert_eq!;fn copysign(self: Self, sign: Self) -> SelfReturns a number composed of the magnitude of
selfand the sign ofsign.Equal to
selfif the sign ofselfandsignare the same, otherwise equal to-self. Ifselfis aNAN, then aNANwith the sign ofsignis returned.Examples
use Float; let f = 3.5_f32; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert!;
Implementors
impl Float for f64impl Float for f32