Skip to main content

Float

Trait Float 

Source
pub trait Float:
    PrimitiveSigned
    + CastPrimitive
    + Half
    + NaNValue {
Show 34 associated constants and 31 methods const TWO: Self; const THREE: Self; const SIX: Self; const SIXTY: Self; const TWENTY_FOUR: Self; 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 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 cot(self) -> Self { ... } 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 TWO: Self

Source

const THREE: Self

Source

const SIX: Self

Source

const SIXTY: Self

Source

const TWENTY_FOUR: Self

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 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 cot(self) -> Self

Computes the cotangent of a number (in radians).

Defined as 1 / tan(x) or equivalently cos(x) / sin(x).

§Examples
use hexga_math::number::*;

let x = std::f32::consts::FRAC_PI_4; // 45°

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

assert!(abs_difference <= f32::EPSILON);
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 Float for f32

Source§

const TWO: f32 = 2.0

Source§

const THREE: f32 = 3.0

Source§

const SIX: f32 = 6.0

Source§

const TWENTY_FOUR: f32 = 24.

Source§

const SIXTY: f32 = 60.

Source§

const PI: f32 = std::f32::consts::PI

Source§

const TWO_PI: f32

Source§

const HALF_PI: f32

Source§

const ANGLE_FULL_DEGREE: f32 = 360.

Source§

const ANGLE_HALF_DEGREE: f32 = 180.

Source§

const ANGLE_RIGHT_DEGREE: f32 = 90.

Source§

const ANGLE_FULL_TURN: f32 = Self::ONE

Source§

const ANGLE_HALF_TURN: f32 = 0.5

Source§

const ANGLE_RIGHT_TURN: f32 = 0.25

Source§

const COLOR_30_DIV_360: f32

Source§

const COLOR_60_DIV_360: f32

Source§

const COLOR_90_DIV_360: f32

Source§

const COLOR_120_DIV_360: f32

Source§

const COLOR_150_DIV_360: f32

Source§

const COLOR_180_DIV_360: f32

Source§

const COLOR_210_DIV_360: f32

Source§

const COLOR_240_DIV_360: f32

Source§

const COLOR_270_DIV_360: f32

Source§

const COLOR_300_DIV_360: f32

Source§

const COLOR_330_DIV_360: f32

Source§

fn floor(self) -> f32

Source§

fn ceil(self) -> f32

Source§

fn round(self) -> f32

Source§

fn trunc(self) -> f32

Source§

fn sqrt(self) -> f32

Source§

fn cos(self) -> f32

Source§

fn acos(self) -> f32

Source§

fn cosh(self) -> f32

Source§

fn sin(self) -> f32

Source§

fn asin(self) -> f32

Source§

fn sinh(self) -> f32

Source§

fn tan(self) -> f32

Source§

fn atan(self) -> f32

Source§

fn tanh(self) -> f32

Source§

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

Source§

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

Source§

fn fract(self) -> f32

Source§

fn signum(self) -> f32

Source§

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

Source§

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

Source§

fn exp(self) -> f32

Source§

fn exp2(self) -> f32

Source§

fn ln(self) -> f32

Source§

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

Source§

fn log2(self) -> f32

Source§

fn log10(self) -> f32

Source§

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

Source§

impl Float for f64

Source§

const TWO: f64 = 2.0

Source§

const THREE: f64 = 3.0

Source§

const SIX: f64 = 6.0

Source§

const TWENTY_FOUR: f64 = 24.

Source§

const SIXTY: f64 = 60.

Source§

const PI: f64 = std::f64::consts::PI

Source§

const TWO_PI: f64

Source§

const HALF_PI: f64

Source§

const ANGLE_FULL_DEGREE: f64 = 360.

Source§

const ANGLE_HALF_DEGREE: f64 = 180.

Source§

const ANGLE_RIGHT_DEGREE: f64 = 90.

Source§

const ANGLE_FULL_TURN: f64 = Self::ONE

Source§

const ANGLE_HALF_TURN: f64 = 0.5

Source§

const ANGLE_RIGHT_TURN: f64 = 0.25

Source§

const COLOR_30_DIV_360: f64

Source§

const COLOR_60_DIV_360: f64

Source§

const COLOR_90_DIV_360: f64

Source§

const COLOR_120_DIV_360: f64

Source§

const COLOR_150_DIV_360: f64

Source§

const COLOR_180_DIV_360: f64

Source§

const COLOR_210_DIV_360: f64

Source§

const COLOR_240_DIV_360: f64

Source§

const COLOR_270_DIV_360: f64

Source§

const COLOR_300_DIV_360: f64

Source§

const COLOR_330_DIV_360: f64

Source§

fn floor(self) -> f64

Source§

fn ceil(self) -> f64

Source§

fn round(self) -> f64

Source§

fn trunc(self) -> f64

Source§

fn sqrt(self) -> f64

Source§

fn cos(self) -> f64

Source§

fn acos(self) -> f64

Source§

fn cosh(self) -> f64

Source§

fn sin(self) -> f64

Source§

fn asin(self) -> f64

Source§

fn sinh(self) -> f64

Source§

fn tan(self) -> f64

Source§

fn atan(self) -> f64

Source§

fn tanh(self) -> f64

Source§

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

Source§

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

Source§

fn fract(self) -> f64

Source§

fn signum(self) -> f64

Source§

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

Source§

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

Source§

fn exp(self) -> f64

Source§

fn exp2(self) -> f64

Source§

fn ln(self) -> f64

Source§

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

Source§

fn log2(self) -> f64

Source§

fn log10(self) -> f64

Source§

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

Implementors§