pub trait Float: Sized {
Show 40 methods
// Required methods
fn floor(self) -> Self;
fn ceil(self) -> Self;
fn round(self) -> Self;
fn round_ties_even(self) -> Self;
fn trunc(self) -> Self;
fn fract(self) -> Self;
fn abs(self) -> Self;
fn signum(self) -> Self;
fn copysign(self, sign: Self) -> Self;
fn mul_add(self, a: Self, b: Self) -> Self;
fn div_euclid(self, rhs: Self) -> Self;
fn rem_euclid(self, rhs: Self) -> Self;
fn powi(self, n: i32) -> Self;
fn powf(self, n: Self) -> Self;
fn sqrt(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 abs_sub(self, other: Self) -> Self;
fn cbrt(self) -> Self;
fn hypot(self, other: Self) -> Self;
fn sin(self) -> Self;
fn cos(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 sin_cos(self) -> (Self, Self);
fn exp_m1(self) -> Self;
fn ln_1p(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;
}
Expand description
Floating-point math functions
This extension trait defines the missing implementations of floating point
math in core
present in rust’s std
crate.
Required Methods§
Sourcefn floor(self) -> Self
fn floor(self) -> Self
Returns the largest integer less than or equal to self
.
This function always returns the precise result.
Sourcefn ceil(self) -> Self
fn ceil(self) -> Self
Returns the smallest integer greater than or equal to self
.
This function always returns the precise result.
Sourcefn round(self) -> Self
fn round(self) -> Self
Returns the nearest integer to self
. If a value is half-way between two
integers, round away from 0.0
.
This function always returns the precise result.
Sourcefn round_ties_even(self) -> Self
fn round_ties_even(self) -> Self
Returns the nearest integer to a number. Rounds half-way cases to the number with an even least significant digit.
This function always returns the precise result.
Sourcefn trunc(self) -> Self
fn trunc(self) -> Self
Returns the integer part of self
.
This means that non-integer numbers are always truncated towards zero.
This function always returns the precise result.
Sourcefn fract(self) -> Self
fn fract(self) -> Self
Returns the fractional part of self
.
This function always returns the precise result.
Sourcefn abs(self) -> Self
fn abs(self) -> Self
Computes the absolute value of self
.
This function always returns the precise result.
Sourcefn signum(self) -> Self
fn signum(self) -> Self
Returns a number that represents the sign of self
.
1.0
if the number is positive,+0.0
orINFINITY
-1.0
if the number is negative,-0.0
orNEG_INFINITY
- NaN if the number is NaN
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
.
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 sign bit of
sign
is returned. Note, however, that conserving the sign bit on NaN
across arithmetical operations is not generally guaranteed.
See explanation of NaN as a special value for more info.
Sourcefn mul_add(self, a: Self, b: Self) -> Self
fn mul_add(self, a: Self, b: Self) -> Self
Fused multiply-add. Computes (self * a) + b
with only one rounding
error, yielding a more accurate result than an unfused multiply-add.
Using mul_add
may be more performant than an unfused multiply-add if
the target architecture has a dedicated fma
CPU instruction. However,
this is not always true, and will be heavily dependant on designing
algorithms with specific target hardware in mind.
Sourcefn div_euclid(self, rhs: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
Calculates Euclidean division, the matching method for rem_euclid
.
This computes the integer n
such that
self = n * rhs + self.rem_euclid(rhs)
.
In other words, the result is self / rhs
rounded to the integer n
such that self >= n * rhs
.
Sourcefn rem_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
Calculates the least nonnegative remainder of self (mod rhs)
.
In particular, the return value r
satisfies 0.0 <= r < rhs.abs()
in
most cases. However, due to a floating point round-off error it can
result in r == rhs.abs()
, violating the mathematical definition, if
self
is much smaller than rhs.abs()
in magnitude and self < 0.0
.
This result is not an element of the function’s codomain, but it is the
closest floating point number in the real numbers and thus fulfills the
property self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)
approximately.
§Precision
The result of this operation is guaranteed to be the rounded infinite-precision result.
Sourcefn powi(self, n: i32) -> Self
fn powi(self, n: i32) -> Self
Raises a number to an integer power.
Using this function is generally faster than using powf
.
It might have a different sequence of rounding operations than powf
,
so the results are not guaranteed to agree.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn powf(self, n: Self) -> Self
fn powf(self, n: Self) -> Self
Raises a number to a floating point power.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn sqrt(self) -> Self
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.
Sourcefn exp(self) -> Self
fn exp(self) -> Self
Returns e^(self)
, (the exponential function).
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn exp2(self) -> Self
fn exp2(self) -> Self
Returns 2^(self)
.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn ln(self) -> Self
fn ln(self) -> Self
Returns the natural logarithm of the number.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn log(self, base: Self) -> Self
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.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn log2(self) -> Self
fn log2(self) -> Self
Returns the base 2 logarithm of the number.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn log10(self) -> Self
fn log10(self) -> Self
Returns the base 10 logarithm of the number.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn abs_sub(self, other: Self) -> Self
👎Deprecated since 0.2.0: you probably meant (self - other).abs()
: this operation is (self - other).max(0.0)
except that abs_sub
also propagates NaNs (also known as fdim
in C). If you truly need the positive difference, consider using that expression or the C function fdim
, depending on how you wish to handle NaN.
fn abs_sub(self, other: Self) -> Self
(self - other).abs()
: this operation is (self - other).max(0.0)
except that abs_sub
also propagates NaNs (also known as fdim
in C). If you truly need the positive difference, consider using that expression or the C function fdim
, depending on how you wish to handle NaN.The positive difference of two numbers.
- If
self <= other
:0.0
- Else:
self - other
§Platform-specific precision
The precision of this function varies by platform and Rust version.
This function currently corresponds to the fdim
function from libm.
Sourcefn cbrt(self) -> Self
fn cbrt(self) -> Self
Returns the cube root of a number.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn hypot(self, other: Self) -> Self
fn hypot(self, other: Self) -> Self
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()
.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn sin(self) -> Self
fn sin(self) -> Self
Computes the sine of a number (in radians).
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn cos(self) -> Self
fn cos(self) -> Self
Computes the cosine of a number (in radians).
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn tan(self) -> Self
fn tan(self) -> Self
Computes the tangent of a number (in radians).
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn asin(self) -> Self
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].
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn acos(self) -> Self
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].
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn atan(self) -> Self
fn atan(self) -> Self
Computes the arctangent of a number. Return value is in radians in the range [-pi/2, pi/2];
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn atan2(self, other: Self) -> Self
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)
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn sin_cos(self) -> (Self, Self)
fn sin_cos(self) -> (Self, Self)
Simultaneously computes the sine and cosine of the number, x
. Returns
(sin(x), cos(x))
.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn exp_m1(self) -> Self
fn exp_m1(self) -> Self
Returns e^(self) - 1
in a way that is accurate even if the
number is close to zero.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn ln_1p(self) -> Self
fn ln_1p(self) -> Self
Returns ln(1+n)
(natural logarithm) more accurately than if
the operations were performed separately.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn sinh(self) -> Self
fn sinh(self) -> Self
Hyperbolic sine function.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn cosh(self) -> Self
fn cosh(self) -> Self
Hyperbolic cosine function.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn tanh(self) -> Self
fn tanh(self) -> Self
Hyperbolic tangent function.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
Sourcefn asinh(self) -> Self
fn asinh(self) -> Self
Inverse hyperbolic sine function.
§Platform-specific precision
The precision of this function varies by platform and Rust version.
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
impl Float for f32
fn floor(self) -> Self
fn ceil(self) -> Self
fn round(self) -> Self
fn round_ties_even(self) -> Self
fn trunc(self) -> Self
fn fract(self) -> Self
fn abs(self) -> Self
fn signum(self) -> Self
fn copysign(self, sign: Self) -> Self
fn mul_add(self, a: Self, b: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
fn powi(self, exp: i32) -> Self
fn powf(self, n: Self) -> Self
fn sqrt(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
Source§fn abs_sub(self, other: Self) -> Self
fn abs_sub(self, other: Self) -> Self
(self - other).abs()
: this operation is (self - other).max(0.0)
except that abs_sub
also propagates NaNs (also known as fdim
in C). If you truly need the positive difference, consider using that expression or the C function fdim
, depending on how you wish to handle NaN.fn cbrt(self) -> Self
fn hypot(self, other: Self) -> Self
fn sin(self) -> Self
fn cos(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 sin_cos(self) -> (Self, Self)
fn exp_m1(self) -> Self
fn ln_1p(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
Source§impl Float for f64
impl Float for f64
fn floor(self) -> Self
fn ceil(self) -> Self
fn round(self) -> Self
fn round_ties_even(self) -> Self
fn trunc(self) -> Self
fn fract(self) -> Self
fn abs(self) -> Self
fn signum(self) -> Self
fn copysign(self, sign: Self) -> Self
fn mul_add(self, a: Self, b: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
fn powi(self, exp: i32) -> Self
fn powf(self, n: Self) -> Self
fn sqrt(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
Source§fn abs_sub(self, other: Self) -> Self
fn abs_sub(self, other: Self) -> Self
(self - other).abs()
: this operation is (self - other).max(0.0)
except that abs_sub
also propagates NaNs (also known as fdim
in C). If you truly need the positive difference, consider using that expression or the C function fdim
, depending on how you wish to handle NaN.