Trait FloatingNumber

Source
pub trait FloatingNumber:
    Floating
    + CastPrimitive
    + PrefixDecade
    + PrefixHundred
    + PrefixThousand
    + PrefixMillion
    + PrefixBillion
    + PrefixQuadrillion
    + Absolute {
Show 31 associated constants and 30 methods const PI: Self; const TWO_PI: Self; const HALF_PI: Self; const ANGLE_FULL_DEGREE: Self; const ANGLE_HALF_DEGREE: Self; const ANGLE_RIGHT_DEGREE: Self; const ANGLE_FULL_TURN: Self; const ANGLE_HALF_TURN: Self; const ANGLE_RIGHT_TURN: Self; const SIXTY: Self; const TWENTY_FOUR: Self; const COLOR_30_DIV_360: Self; const COLOR_60_DIV_360: Self; const COLOR_90_DIV_360: Self; const COLOR_120_DIV_360: Self; const COLOR_150_DIV_360: Self; const COLOR_180_DIV_360: Self; const COLOR_210_DIV_360: Self; const COLOR_240_DIV_360: Self; const COLOR_270_DIV_360: Self; const COLOR_300_DIV_360: Self; const COLOR_330_DIV_360: Self; const ANGLE_ZERO_RADIAN: Self = Self::ZERO; const ANGLE_FULL_RADIAN: Self = Self::TWO_PI; const ANGLE_HALF_RADIAN: Self = Self::PI; const ANGLE_FLAT_RADIAN: Self = Self::PI; const ANGLE_RIGHT_RADIAN: Self = Self::ANGLE_HALF_RADIAN; const ANGLE_ZERO_DEGREE: Self = Self::ZERO; const ANGLE_FLAT_DEGREE: Self = Self::ANGLE_HALF_DEGREE; const ANGLE_ZERO_TURN: Self = Self::ZERO; const ANGLE_FLAT_TURN: Self = Self::ANGLE_HALF_TURN; // Required methods fn sqrt(self) -> Self; fn fract(self) -> Self; fn signum(self) -> Self; fn copysign(self, sign: Self) -> Self; fn pow(self, n: 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 sin(self) -> Self; fn asin(self) -> Self; fn sinh(self) -> Self; fn cos(self) -> Self; fn acos(self) -> Self; fn cosh(self) -> Self; fn tan(self) -> Self; fn atan(self) -> Self; fn tanh(self) -> Self; fn atan2(self, other: Self) -> Self; fn sin_cos(self) -> (Self, Self); fn floor(self) -> Self; fn round(self) -> Self; fn ceil(self) -> Self; fn trunc(self) -> Self; fn total_cmp(&self, other: &Self) -> Ordering; // Provided methods fn normalize(self) -> Self where Self: One { ... } fn round_toward_zero(self) -> Self where Self: PositiveOrNegative { ... } fn round_away_from_zero(self) -> Self where Self: PositiveOrNegative { ... }
}
Expand description

Generalized function and constant for floating point like f32, f64

The func impl and documentation are copied from the Rust std because those are the same function, generalized in this trait

Required Associated Constants§

Source

const PI: Self

Archimedes’ constant (π)

Source

const TWO_PI: Self

2.0 * PI

Source

const HALF_PI: Self

0.5 * PI

Source

const ANGLE_FULL_DEGREE: Self

Source

const ANGLE_HALF_DEGREE: Self

Source

const ANGLE_RIGHT_DEGREE: Self

Source

const ANGLE_FULL_TURN: Self

Source

const ANGLE_HALF_TURN: Self

Source

const ANGLE_RIGHT_TURN: Self

Source

const SIXTY: Self

Source

const TWENTY_FOUR: Self

Source

const COLOR_30_DIV_360: Self

Source

const COLOR_60_DIV_360: Self

Source

const COLOR_90_DIV_360: Self

Source

const COLOR_120_DIV_360: Self

Source

const COLOR_150_DIV_360: Self

Source

const COLOR_180_DIV_360: Self

Source

const COLOR_210_DIV_360: Self

Source

const COLOR_240_DIV_360: Self

Source

const COLOR_270_DIV_360: Self

Source

const COLOR_300_DIV_360: Self

Source

const COLOR_330_DIV_360: Self

Provided Associated Constants§

Source

const ANGLE_ZERO_RADIAN: Self = Self::ZERO

Source

const ANGLE_FULL_RADIAN: Self = Self::TWO_PI

Source

const ANGLE_HALF_RADIAN: Self = Self::PI

Source

const ANGLE_FLAT_RADIAN: Self = Self::PI

Source

const ANGLE_RIGHT_RADIAN: Self = Self::ANGLE_HALF_RADIAN

Source

const ANGLE_ZERO_DEGREE: Self = Self::ZERO

Source

const ANGLE_FLAT_DEGREE: Self = Self::ANGLE_HALF_DEGREE

Source

const ANGLE_ZERO_TURN: Self = Self::ZERO

Source

const ANGLE_FLAT_TURN: Self = Self::ANGLE_HALF_TURN

Required Methods§

Source

fn sqrt(self) -> Self

Returns the square root of a number.

Returns NaN if self is a negative number other than -0.0.

§Precision

The result of this operation is guaranteed to be the rounded infinite-precision result. It is specified by IEEE 754 as squareRoot and guaranteed not to change.

§Examples
let positive = 4.0_f32;
let negative = -4.0_f32;
let negative_zero = -0.0_f32;

assert_eq!(positive.sqrt(), 2.0);
assert!(negative.sqrt().is_nan());
assert!(negative_zero.sqrt() == negative_zero);
Source

fn fract(self) -> Self

Computes the absolute value of self.

This function always returns the precise result.

§Examples
let x = 3.5_f32;
let y = -3.5_f32;

assert_eq!(x.abs(), x);
assert_eq!(y.abs(), -y);

assert!(f32::NAN.abs().is_nan());
Source

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
§Examples
let f = 3.5_f32;

assert_eq!(f.signum(), 1.0);
assert_eq!(f32::NEG_INFINITY.signum(), -1.0);

assert!(f32::NAN.signum().is_nan());
Source

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 same payload as self and the sign bit of sign is returned.

If sign is a NaN, then this operation will still carry over its sign into the result. Note that IEEE 754 doesn’t assign any meaning to the sign bit in case of a NaN, and as Rust doesn’t guarantee that the bit pattern of NaNs are conserved over arithmetic operations, the result of copysign with sign being a NaN might produce an unexpected or non-portable result. See the specification of NaN bit patterns for more info.

§Examples
let f = 3.5_f32;

assert_eq!(f.copysign(0.42), 3.5_f32);
assert_eq!(f.copysign(-0.42), -3.5_f32);
assert_eq!((-f).copysign(0.42), 3.5_f32);
assert_eq!((-f).copysign(-0.42), -3.5_f32);

assert!(f32::NAN.copysign(1.0).is_nan());
Source

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

Raises a number to a floating point power.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let x = 2.0_f32;
let abs_difference = (x.powf(2.0) - (x * x)).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn exp(self) -> Self

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

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let one = 1.0f32;
// e^1
let e = one.exp();

// ln(e) - 1 == 0
let abs_difference = (e.ln() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn exp2(self) -> Self

Returns 2^(self).

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let f = 2.0f32;

// 2^2 - 4 == 0
let abs_difference = (f.exp2() - 4.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn ln(self) -> Self

Returns the natural logarithm of the number.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let one = 1.0f32;
// e^1
let e = one.exp();

// ln(e) - 1 == 0
let abs_difference = (e.ln() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

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

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

The result might 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.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let five = 5.0f32;

// log5(5) - 1 == 0
let abs_difference = (five.log(5.0) - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn log2(self) -> Self

Returns the base 2 logarithm of the number.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let two = 2.0f32;

// log2(2) - 1 == 0
let abs_difference = (two.log2() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn log10(self) -> Self

Returns the base 10 logarithm of the number.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let ten = 10.0f32;

// log10(10) - 1 == 0
let abs_difference = (ten.log10() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn sin(self) -> Self

Computes the sine of a number (in radians).

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let x = std::f32::consts::FRAC_PI_2;

let abs_difference = (x.sin() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

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].

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the asinf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let f = std::f32::consts::FRAC_PI_2;

// asin(sin(pi/2))
let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn sinh(self) -> Self

Hyperbolic sine function.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the sinhf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let e = std::f32::consts::E;
let x = 1.0f32;

let f = x.sinh();
// Solving sinh() at 1 gives `(e^2-1)/(2e)`
let g = ((e * e) - 1.0) / (2.0 * e);
let abs_difference = (f - g).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn cos(self) -> Self

Computes the cosine of a number (in radians).

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.

§Examples
let x = 2.0 * std::f32::consts::PI;

let abs_difference = (x.cos() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

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].

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the acosf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let f = std::f32::consts::FRAC_PI_4;

// acos(cos(pi/4))
let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn cosh(self) -> Self

Hyperbolic cosine function.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the coshf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let e = std::f32::consts::E;
let x = 1.0f32;
let f = x.cosh();
// Solving cosh() at 1 gives this result
let g = ((e * e) + 1.0) / (2.0 * e);
let abs_difference = (f - g).abs();

// Same result
assert!(abs_difference <= f32::EPSILON);
Source

fn tan(self) -> Self

Computes the tangent of a number (in radians).

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the tanf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let x = std::f32::consts::FRAC_PI_4;
let abs_difference = (x.tan() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn atan(self) -> Self

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

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the atanf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let f = 1.0f32;

// atan(tan(1))
let abs_difference = (f.tan().atan() - 1.0).abs();

assert!(abs_difference <= f32::EPSILON);
Source

fn tanh(self) -> Self

Hyperbolic tangent function.

§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the tanhf from libc on Unix and Windows. Note that this might change in the future.

§Examples
let e = std::f32::consts::E;
let x = 1.0f32;

let f = x.tanh();
// Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
let abs_difference = (f - g).abs();

assert!(abs_difference <= f32::EPSILON);
Source

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)
§Unspecified precision

The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next. This function currently corresponds to the atan2f from libc on Unix and Windows. Note that this might change in the future.

§Examples
// Positive angles measured counter-clockwise
// from positive x axis
// -pi/4 radians (45 deg clockwise)
let x1 = 3.0f32;
let y1 = -3.0f32;

// 3pi/4 radians (135 deg counter-clockwise)
let x2 = -3.0f32;
let y2 = 3.0f32;

let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();

assert!(abs_difference_1 <= f32::EPSILON);
assert!(abs_difference_2 <= f32::EPSILON);
Source

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

Source

fn floor(self) -> Self

Source

fn round(self) -> Self

Source

fn ceil(self) -> Self

Source

fn trunc(self) -> Self

Source

fn total_cmp(&self, other: &Self) -> Ordering

Provided Methods§

Source

fn normalize(self) -> Self
where Self: One,

between [0., 1.]

Source

fn round_toward_zero(self) -> Self
where Self: PositiveOrNegative,

Source

fn round_away_from_zero(self) -> Self
where Self: PositiveOrNegative,

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 FloatingNumber for f32

Source§

const PI: Self = 3.14159274f32

Source§

const TWO_PI: Self = 6.28318548f32

Source§

const HALF_PI: Self = 1.57079637f32

Source§

const ANGLE_FULL_DEGREE: Self = 360f32

Source§

const ANGLE_HALF_DEGREE: Self = 180f32

Source§

const ANGLE_RIGHT_DEGREE: Self = 90f32

Source§

const ANGLE_FULL_TURN: Self = 1f32

Source§

const ANGLE_HALF_TURN: Self = 0.5f32

Source§

const ANGLE_RIGHT_TURN: Self = 0.25f32

Source§

const COLOR_30_DIV_360: Self = 0.0833333358f32

Source§

const COLOR_60_DIV_360: Self = 0.166666672f32

Source§

const COLOR_90_DIV_360: Self = 0.25f32

Source§

const COLOR_120_DIV_360: Self = 0.333333343f32

Source§

const COLOR_150_DIV_360: Self = 0.416666657f32

Source§

const COLOR_180_DIV_360: Self = 0.5f32

Source§

const COLOR_210_DIV_360: Self = 0.583333313f32

Source§

const COLOR_240_DIV_360: Self = 0.666666686f32

Source§

const COLOR_270_DIV_360: Self = 0.75f32

Source§

const COLOR_300_DIV_360: Self = 0.833333313f32

Source§

const COLOR_330_DIV_360: Self = 0.916666686f32

Source§

const TWENTY_FOUR: Self = 24f32

Source§

const SIXTY: Self = 60f32

Source§

fn floor(self) -> Self

Source§

fn ceil(self) -> Self

Source§

fn round(self) -> Self

Source§

fn trunc(self) -> Self

Source§

fn sqrt(self) -> Self

Source§

fn cos(self) -> Self

Source§

fn acos(self) -> Self

Source§

fn cosh(self) -> Self

Source§

fn sin(self) -> Self

Source§

fn asin(self) -> Self

Source§

fn sinh(self) -> Self

Source§

fn tan(self) -> Self

Source§

fn atan(self) -> Self

Source§

fn tanh(self) -> Self

Source§

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

Source§

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

Source§

fn fract(self) -> Self

Source§

fn signum(self) -> Self

Source§

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

Source§

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

Source§

fn exp(self) -> Self

Source§

fn exp2(self) -> Self

Source§

fn ln(self) -> Self

Source§

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

Source§

fn log2(self) -> Self

Source§

fn log10(self) -> Self

Source§

fn total_cmp(&self, other: &Self) -> Ordering

Source§

impl FloatingNumber for f64

Source§

const PI: Self = 3.1415926535897931f64

Source§

const TWO_PI: Self = 6.2831853071795862f64

Source§

const HALF_PI: Self = 1.5707963267948966f64

Source§

const ANGLE_FULL_DEGREE: Self = 360f64

Source§

const ANGLE_HALF_DEGREE: Self = 180f64

Source§

const ANGLE_RIGHT_DEGREE: Self = 90f64

Source§

const ANGLE_FULL_TURN: Self = 1f64

Source§

const ANGLE_HALF_TURN: Self = 0.5f64

Source§

const ANGLE_RIGHT_TURN: Self = 0.25f64

Source§

const COLOR_30_DIV_360: Self = 0.083333333333333329f64

Source§

const COLOR_60_DIV_360: Self = 0.16666666666666666f64

Source§

const COLOR_90_DIV_360: Self = 0.25f64

Source§

const COLOR_120_DIV_360: Self = 0.33333333333333331f64

Source§

const COLOR_150_DIV_360: Self = 0.41666666666666669f64

Source§

const COLOR_180_DIV_360: Self = 0.5f64

Source§

const COLOR_210_DIV_360: Self = 0.58333333333333337f64

Source§

const COLOR_240_DIV_360: Self = 0.66666666666666663f64

Source§

const COLOR_270_DIV_360: Self = 0.75f64

Source§

const COLOR_300_DIV_360: Self = 0.83333333333333337f64

Source§

const COLOR_330_DIV_360: Self = 0.91666666666666663f64

Source§

const TWENTY_FOUR: Self = 24f64

Source§

const SIXTY: Self = 60f64

Source§

fn floor(self) -> Self

Source§

fn ceil(self) -> Self

Source§

fn round(self) -> Self

Source§

fn trunc(self) -> Self

Source§

fn sqrt(self) -> Self

Source§

fn cos(self) -> Self

Source§

fn acos(self) -> Self

Source§

fn cosh(self) -> Self

Source§

fn sin(self) -> Self

Source§

fn asin(self) -> Self

Source§

fn sinh(self) -> Self

Source§

fn tan(self) -> Self

Source§

fn atan(self) -> Self

Source§

fn tanh(self) -> Self

Source§

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

Source§

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

Source§

fn fract(self) -> Self

Source§

fn signum(self) -> Self

Source§

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

Source§

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

Source§

fn exp(self) -> Self

Source§

fn exp2(self) -> Self

Source§

fn ln(self) -> Self

Source§

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

Source§

fn log2(self) -> Self

Source§

fn log10(self) -> Self

Source§

fn total_cmp(&self, other: &Self) -> Ordering

Implementors§