pub trait ExtFloat: ExtFloatConst + Sized {
Show 67 methods
// Required methods
fn floor(self) -> Self;
fn ceil(self) -> Self;
fn round(self) -> Self;
fn round_ties_away(self) -> Self;
fn round_ties_even(self) -> Self;
fn round_ties_odd(self) -> Self;
fn trunc(self) -> Self;
fn fract(self) -> Self;
fn split(self) -> (Self, Self) ⓘ;
fn abs(self) -> Self;
fn neg_abs(self) -> Self;
fn sign(self) -> Sign;
fn sign_nonzero(self) -> Sign;
fn signum(self) -> Self;
fn flip_sign(self) -> Self;
fn is_sign_positive(self) -> bool;
fn is_sign_negative(self) -> bool;
fn is_zero(self) -> bool;
fn is_sign_positive_nonzero(self) -> bool;
fn is_sign_negative_nonzero(self) -> bool;
fn copysign(self, sign: Self) -> Self;
fn mul_add(self, mul: Self, add: Self) -> Self;
fn div_euclid(self, rhs: Self) -> Self;
fn rem_euclid(self, rhs: Self) -> Self;
fn scale(self, min: Self, max: Self, u: Self, v: Self) -> Self;
fn lerp(self, u: Self, v: Self) -> Self;
fn powf(self, y: Self) -> Self;
fn powi(self, p: i32) -> Self;
fn sqrt(self) -> Self;
fn sqrt_fisr(self) -> Self;
fn fisr(self) -> Self;
fn cbrt(self) -> Self;
fn hypot(self, rhs: Self) -> Self;
fn exp(self) -> Self;
fn exp2(self) -> Self;
fn exp_m1(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 factorial(n: u32) -> Self;
fn sin(self) -> Self;
fn cos(self) -> Self;
fn sin_cos(self) -> (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 sinh(self) -> Self;
fn cosh(self) -> Self;
fn tanh(self) -> Self;
fn asinh(self) -> Self;
fn acosh(self) -> Self;
fn atanh(self) -> Self;
fn clamp_nan(self, min: Self, max: Self) -> Self;
fn max_nan(self, other: Self) -> Self;
fn min_nan(self, other: Self) -> Self;
fn clamp_total(self, min: Self, max: Self) -> Self;
fn max_total(self, other: Self) -> Self;
fn min_total(self, other: Self) -> Self;
fn eval_poly(self, coefficients: &[Self]) -> Self;
fn derivative<F>(f: F, x: Self, h: Self) -> Self
where F: Fn(Self) -> Self;
fn integrate<F>(f: F, x: Self, y: Self, n: usize) -> Self
where F: Fn(Self) -> Self;
fn partial_derivative_x<F>(f: F, x: Self, y: Self, h: Self) -> Self
where F: Fn(Self, Self) -> Self;
fn partial_derivative_y<F>(f: F, x: Self, y: Self, h: Self) -> Self
where F: Fn(Self, Self) -> Self;
}
Expand description
Extension trait for floating-point types. Associated methods.
This trait can be more convenient to use than the Float
struct,
for non-const operations over primitive floating-point types.
§Features
It depends on having any _float_f[32|64]
features enabled.
Float
has a few more methods implemented if the dep_libm
feature is enabled.
Required Methods§
Sourcefn floor(self) -> Self
fn floor(self) -> Self
The largest integer less than or equal to self
.
§Formula
$$ \large \lfloor x \rfloor = \max { n \in \mathbb{Z} ,|, n \leq x } $$
Sourcefn ceil(self) -> Self
fn ceil(self) -> Self
The smallest integer greater than or equal to self
.
§Formula
$$ $$ \lceil x \rceil = \min { n \in \mathbb{Z} ,|, n \geq x } $$
Sourcefn round(self) -> Self
fn round(self) -> Self
The nearest integer to self
, default rounding, same as
round_ties_away
Sourcefn round_ties_away(self) -> Self
fn round_ties_away(self) -> Self
The nearest integer to self
, rounding ties away from 0.0
.
§Formula
$$ \text{round\_ties\_away}(x) = \begin{cases} \lceil x \rceil, & \text{if } x - \lfloor x \rfloor > 0.5 \text{ or } (x - \lfloor x \rfloor = 0.5 \text{ and } x > 0) \cr \lfloor x \rfloor, & \text{if } x - \lfloor x \rfloor < 0.5 \text{ or } (x - \lfloor x \rfloor = 0.5 \text{ and } x < 0) \end{cases} $$
Sourcefn round_ties_even(self) -> Self
fn round_ties_even(self) -> Self
The nearest integer to self
, rounding ties to the nearest even integer.
§Formula
$$ \text{round\_ties\_even}(x) = \begin{cases} \lceil x \rceil, & \text{if } x - \lfloor x \rfloor > 0.5 \cr \lfloor x \rfloor, & \text{if } x - \lfloor x \rfloor < 0.5 \cr \lfloor x \rfloor, & \text{if } x - \lfloor x \rfloor = 0.5 \text{ and } \lfloor x \rfloor \text{ is even} \cr \lceil x \rceil, & \text{if } x - \lfloor x \rfloor = 0.5 \text{ and } \lfloor x \rfloor \text{ is odd} \end{cases} $$
Sourcefn round_ties_odd(self) -> Self
fn round_ties_odd(self) -> Self
The nearest integer to self
, rounding ties to the nearest odd integer.
§Formula
$$ \text{round\_ties\_odd}(x) = \begin{cases} \lceil x \rceil, & \text{if } x - \lfloor x \rfloor > 0.5 \cr \lfloor x \rfloor, & \text{if } x - \lfloor x \rfloor < 0.5 \cr \lfloor x \rfloor, & \text{if } x - \lfloor x \rfloor = 0.5 \text{ and } \lfloor x \rfloor \text{ is odd} \cr \lceil x \rceil, & \text{if } x - \lfloor x \rfloor = 0.5 \text{ and } \lfloor x \rfloor \text{ is even} \end{cases} $$
Sourcefn trunc(self) -> Self
fn trunc(self) -> Self
The integral part of self
.
§Formula
$$ \text{trunc}(x) = \begin{cases} \lfloor x \rfloor, & \text{if } x \geq 0 \ \lceil x \rceil, & \text{if } x < 0 \end{cases} $$
Sourcefn split(self) -> (Self, Self) ⓘ
fn split(self) -> (Self, Self) ⓘ
The integral and fractional parts ox self
.
§Formula
$$ \text{split}(x) = (\text{trunc}(x), \text{fract}(x)) $$
Sourcefn sign_nonzero(self) -> Sign
fn sign_nonzero(self) -> Sign
Returns the Sign
of self
, except for zero.
Sourcefn is_sign_positive(self) -> bool
fn is_sign_positive(self) -> bool
Returns true
if self
has a positive sign.
Sourcefn is_sign_negative(self) -> bool
fn is_sign_negative(self) -> bool
Returns true
if self
has a negative sign.
Sourcefn is_sign_positive_nonzero(self) -> bool
fn is_sign_positive_nonzero(self) -> bool
Returns true
if self
has a positive sign and is not zero.
Sourcefn is_sign_negative_nonzero(self) -> bool
fn is_sign_negative_nonzero(self) -> bool
Returns true
if self
has a negative sign and is not zero.
Sourcefn copysign(self, sign: Self) -> Self
fn copysign(self, sign: Self) -> Self
A number composed of a magnitude of self
and the sign of sign
.
Sourcefn mul_add(self, mul: Self, add: Self) -> Self
fn mul_add(self, mul: Self, add: Self) -> Self
Fused multiply-add. Computes (self * mul) + add
with only one rounding error.
With either std
or dep_libm
enabled it leverages compiler intrinsics,
otherwise it uses mul_add_fallback
.
Sourcefn div_euclid(self, rhs: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
The euclidean division.
Sourcefn rem_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
The least nonnegative remainder of self % rhs
.
Sourcefn scale(self, min: Self, max: Self, u: Self, v: Self) -> Self
fn scale(self, min: Self, max: Self, u: Self, v: Self) -> Self
Returns self
between [min..=max]
scaled to a new range [u..=v]
.
Values of self
outside [min..=max]
are not clamped and will result in extrapolation.
§Formula
$$ \large \text{scale}(x, min, max, u, v) = (v - u) \frac{x - min}{max - min} + u $$
Sourcefn lerp(self, u: Self, v: Self) -> Self
fn lerp(self, u: Self, v: Self) -> Self
Calculates a linearly interpolated value between u..=v
based on the percentage self
between [0..=1]
.
Values of self
outside [0..=1]
are not clamped and will result in extrapolation.
§Formula
$$ \large \text{lerp}(x, u, v) = (1 - x) \cdot u + x \cdot v $$
Sourcefn powf(self, y: Self) -> Self
fn powf(self, y: Self) -> Self
Raises self
to the y
floating point power.
With either std
or dep_libm
enabled it leverages compiler intrinsics,
otherwise it’s equal to powf_series
.
Sourcefn sqrt(self) -> Self
fn sqrt(self) -> Self
The square root.
With either std
or dep_libm
enabled it leverages compiler intrinsics,
otherwise it’s equal to sqrt_nr
.
Sourcefn sqrt_fisr(self) -> Self
fn sqrt_fisr(self) -> Self
The square root calculated using the fast inverse square root algorithm.
Sourcefn fisr(self) -> Self
fn fisr(self) -> Self
$ 1 / \sqrt{x} $ the fast inverse square root algorithm.
Sourcefn cbrt(self) -> Self
fn cbrt(self) -> Self
The cubic root.
With either std
or dep_libm
enabled it leverages compiler intrinsics,
otherwise it’s equal to cbrt_nr
.
Sourcefn hypot(self, rhs: Self) -> Self
fn hypot(self, rhs: Self) -> Self
The hypothenuse (the euclidean distance).
With either std
or dep_libm
enabled it leverages compiler intrinsics,
otherwise it’s equal to hypot_nr
.
Sourcefn exp(self) -> Self
fn exp(self) -> Self
$e^x$ (the exponential function).
The maximum values with a representable result are:
88.722… for f32
and 709.782… for f64
.
With both std
and dep_libm
disabled it leverages exp_series
with exp_series_terms
.
Sourcefn exp2(self) -> Self
fn exp2(self) -> Self
$2^x$.
With both std
and dep_libm
disabled it leverages exp2_series
with exp2_series_terms
.
Sourcefn exp_m1(self) -> Self
fn exp_m1(self) -> Self
The exponential minus 1, more accurately.
With both std
and dep_libm
disabled it leverages exp_m1_series
with exp_series_terms
.
Sourcefn ln(self) -> Self
fn ln(self) -> Self
The natural logarithm of self
.
With both std
and dep_libm
disabled it leverages ln_series
with ln_series_terms
.
Sourcefn ln_1p(self) -> Self
fn ln_1p(self) -> Self
The natural logarithm of self
plus 1, more accurately.
With both std
and dep_libm
disabled it leverages ln_1p_series
with ln_series_terms
.
Sourcefn log(self, base: Self) -> Self
fn log(self, base: Self) -> Self
The logarithm of self
with respect to an arbitrary base
.
With both std
and dep_libm
disabled it leverages log_series
with ln_series_terms
.
Sourcefn log2(self) -> Self
fn log2(self) -> Self
The base 2 logarithm of self
.
With both std
and dep_libm
disabled it leverages log2_series
with ln_series_terms
.
Sourcefn log10(self) -> Self
fn log10(self) -> Self
The base 10 logarithm of self
.
With both std
and dep_libm
disabled it leverages log10_series
with ln_series_terms
.
Sourcefn factorial(n: u32) -> Self
fn factorial(n: u32) -> Self
The factorial.
The maximum values with a representable result are:
34 for f32
and 170 for f64
.
Sourcefn sin(self) -> Self
fn sin(self) -> Self
The sine.
With both std
and dep_libm
disabled it leverages
sin_series
with 8 terms.
Sourcefn cos(self) -> Self
fn cos(self) -> Self
The cosine.
With both std
and dep_libm
disabled it leverages
cos_series
with 8 terms.
Sourcefn sin_cos(self) -> (Self, Self) ⓘ
fn sin_cos(self) -> (Self, Self) ⓘ
Both the sine and cosine.
With both std
and dep_libm
disabled it leverages
sin_cos_series
with 8 terms.
Sourcefn tan(self) -> Self
fn tan(self) -> Self
The tangent.
With both std
and dep_libm
disabled it leverages
tan_series
with 8 terms.
Sourcefn asin(self) -> Self
fn asin(self) -> Self
The arc sine.
With both std
and dep_libm
disabled it leverages asin_series
with asin_series_terms
.
Sourcefn acos(self) -> Self
fn acos(self) -> Self
The arc cosine.
With both std
and dep_libm
disabled it leverages acos_series
with acos_series_terms
.
Sourcefn atan(self) -> Self
fn atan(self) -> Self
The arc tangent.
With both std
and dep_libm
disabled it leverages atan_series
with atan_series_terms
.
Sourcefn atan2(self, other: Self) -> Self
fn atan2(self, other: Self) -> Self
The arc tangent of two variables.
With both std
and dep_libm
disabled it leverages atan2_series
with atan_series_terms
.
Sourcefn sinh(self) -> Self
fn sinh(self) -> Self
The hyperbolic sine.
With both std
and dep_libm
disabled it leverages sinh_series
with exp_series_terms
.
Sourcefn cosh(self) -> Self
fn cosh(self) -> Self
The hyperbolic cosine.
With both std
and dep_libm
disabled it leverages cosh_series
with exp_series_terms
.
Sourcefn tanh(self) -> Self
fn tanh(self) -> Self
The hyperbolic tangent.
With both std
and dep_libm
disabled it leverages cosh_series
with exp_series_terms
.
Sourcefn asinh(self) -> Self
fn asinh(self) -> Self
The inverse hyperbolic sine of self
.
With both std
and dep_libm
disabled it leverages asinh_series
with ln_series_terms
.
Sourcefn acosh(self) -> Self
fn acosh(self) -> Self
The inverse hyperbolic cosine of self
.
With both std
and dep_libm
disabled it leverages acosh_series
with ln_series_terms
.
Sourcefn atanh(self) -> Self
fn atanh(self) -> Self
The inverse hyperbolic tangent of self
.
With both std
and dep_libm
disabled it leverages atanh_series
with ln_series_terms
.
Sourcefn clamp_total(self, min: Self, max: Self) -> Self
fn clamp_total(self, min: Self, max: Self) -> Self
The clamped value, using total order.
§Features
This will only work if the corresponding _cmp_[f32|f64]
feature is enabled,
otherwise it will return NaN
.
Sourcefn max_total(self, other: Self) -> Self
fn max_total(self, other: Self) -> Self
The maximum of two numbers using total order.
§Features
This will only work if the corresponding _cmp_[f32|f64]
feature is enabled,
otherwise it will return NaN
.
Sourcefn min_total(self, other: Self) -> Self
fn min_total(self, other: Self) -> Self
The minimum of two numbers using total order.
§Features
This will only work if the corresponding _cmp_[f32|f64]
feature is enabled,
otherwise it will return NaN
.
Sourcefn eval_poly(self, coefficients: &[Self]) -> Self
fn eval_poly(self, coefficients: &[Self]) -> Self
Evaluates a polynomial at the self
point value, using Horner’s method.
Sourcefn derivative<F>(f: F, x: Self, h: Self) -> Selfwhere
F: Fn(Self) -> Self,
fn derivative<F>(f: F, x: Self, h: Self) -> Selfwhere
F: Fn(Self) -> Self,
Approximates the derivative of the 1D function f
at x
point using step size h
.
Uses the finite difference method.
See also the [autodiff
] attr macro, enabled with the nightly_autodiff
feature.
Sourcefn integrate<F>(f: F, x: Self, y: Self, n: usize) -> Selfwhere
F: Fn(Self) -> Self,
fn integrate<F>(f: F, x: Self, y: Self, n: usize) -> Selfwhere
F: Fn(Self) -> Self,
Approximates the integral of the 1D function f
over the range [x, y]
using n
subdivisions.
Uses the Riemann Sum.
Sourcefn partial_derivative_x<F>(f: F, x: Self, y: Self, h: Self) -> Selfwhere
F: Fn(Self, Self) -> Self,
fn partial_derivative_x<F>(f: F, x: Self, y: Self, h: Self) -> Selfwhere
F: Fn(Self, Self) -> Self,
Approximates the partial derivative of the 2D function f
at point (x
, y
)
with step size h
, differentiating over x
.
Sourcefn partial_derivative_y<F>(f: F, x: Self, y: Self, h: Self) -> Selfwhere
F: Fn(Self, Self) -> Self,
fn partial_derivative_y<F>(f: F, x: Self, y: Self, h: Self) -> Selfwhere
F: Fn(Self, Self) -> Self,
Approximates the partial derivative of the 2D function f
at point (x
, y
)
with step size h
, differentiating over 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 ExtFloat for f32
Available on crate feature _float_f32
only.
impl ExtFloat for f32
_float_f32
only.fn floor(self) -> Self
fn ceil(self) -> Self
fn round(self) -> Self
fn round_ties_away(self) -> Self
fn round_ties_even(self) -> Self
fn round_ties_odd(self) -> Self
fn trunc(self) -> Self
fn fract(self) -> Self
fn split(self) -> (Self, Self) ⓘ
fn abs(self) -> Self
fn neg_abs(self) -> Self
fn sign(self) -> Sign
fn sign_nonzero(self) -> Sign
fn signum(self) -> Self
fn flip_sign(self) -> Self
fn is_sign_positive(self) -> bool
fn is_sign_negative(self) -> bool
fn is_zero(self) -> bool
fn is_sign_positive_nonzero(self) -> bool
fn is_sign_negative_nonzero(self) -> bool
fn copysign(self, sign: Self) -> Self
fn mul_add(self, mul: Self, add: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
fn scale(self, min: Self, max: Self, u: Self, v: Self) -> Self
fn lerp(self, u: Self, v: Self) -> Self
fn powf(self, y: Self) -> Self
fn powi(self, p: i32) -> Self
fn sqrt(self) -> Self
fn sqrt_fisr(self) -> Self
fn fisr(self) -> Self
fn cbrt(self) -> Self
fn hypot(self, rhs: Self) -> Self
fn exp(self) -> Self
fn exp2(self) -> Self
fn exp_m1(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 factorial(a: u32) -> Self
fn sin(self) -> Self
fn cos(self) -> Self
fn sin_cos(self) -> (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 sinh(self) -> Self
fn cosh(self) -> Self
fn tanh(self) -> Self
fn asinh(self) -> Self
fn acosh(self) -> Self
fn atanh(self) -> Self
fn clamp_nan(self, min: Self, max: Self) -> Self
fn max_nan(self, other: Self) -> Self
fn min_nan(self, other: Self) -> Self
fn clamp_total(self, min: Self, max: Self) -> Self
fn max_total(self, other: Self) -> Self
fn min_total(self, other: Self) -> Self
fn eval_poly(self, coefficients: &[Self]) -> Self
fn derivative<F>(f: F, x: Self, h: Self) -> Selfwhere
F: Fn(Self) -> Self,
fn integrate<F>(f: F, x: Self, y: Self, n: usize) -> Selfwhere
F: Fn(Self) -> Self,
fn partial_derivative_x<F>(f: F, x: Self, y: Self, h: Self) -> Selfwhere
F: Fn(Self, Self) -> Self,
fn partial_derivative_y<F>(f: F, x: Self, y: Self, h: Self) -> Selfwhere
F: Fn(Self, Self) -> Self,
Source§impl ExtFloat for f64
Available on crate feature _float_f64
only.
impl ExtFloat for f64
_float_f64
only.