pub trait Abs: Sized {
type Output: Sized;
type Error: Error;
// Required methods
fn try_abs(self) -> Result<Self::Output, Self::Error>;
fn abs(self) -> Self::Output;
fn try_abs_squared(self) -> Result<Self::Output, Self::Error>;
fn abs_squared(self) -> Self::Output;
}Expand description
This trait provides the interface for the function used to compute the absolute value (also known as modulus or magnitude) of a number, which can be real or complex.
The absolute value of a real number x is |x|, which is x if x is non-negative,
and -x if x is negative.
The absolute value (or modulus) of a complex number z = a + b*i is sqrt(a^2 + b^2).
In both cases, the result is always a non-negative real number.
This trait provides four methods:
try_abs: A fallible version that performs validation on the input (and potentially the output) and returns aResult. This is the preferred method when robust error handling is required.abs: A convenient infallible version that panics iftry_abswould return an error in debug builds, or directly computes the value in release builds. Use this when the input is known to be valid or when panicking on error is acceptable.try_abs_squared: A fallible version that computes the squared absolute value (avoiding the square root). For complex numbers, this isa^2 + b^2.abs_squared: A convenient infallible version oftry_abs_squared.
§Associated types
Output: The type of the result of the absolute value operation. This is constrained byRealScalarto ensure it’s a real number type.Error: The error type that can be returned by thetry_absmethod.
Required Associated Types§
Sourcetype Output: Sized
type Output: Sized
The output type of the absolute value function.
This is always a real number type, even when the input Self is a complex number.
It must implement the RealScalar trait.
Sourcetype Error: Error
type Error: Error
The error type that can be returned by the try_abs method.
This type must implement std::error::Error. Specific implementations will
use types like AbsRealErrors or AbsComplexErrors to provide detailed
information about the failure.
Required Methods§
Sourcefn try_abs(self) -> Result<Self::Output, Self::Error>
fn try_abs(self) -> Result<Self::Output, Self::Error>
Attempts to compute the absolute value of self.
Sourcefn try_abs_squared(self) -> Result<Self::Output, Self::Error>
fn try_abs_squared(self) -> Result<Self::Output, Self::Error>
Attempts to compute the squared absolute value of self.
For real numbers, this computes x^2.
For complex numbers z = a + b*i, this computes a^2 + b^2 (equivalent to |z|^2).
This method is useful when you need the squared magnitude without the expense of computing the square root, such as for distance comparisons or vector norms.
Sourcefn abs_squared(self) -> Self::Output
fn abs_squared(self) -> Self::Output
Returns the squared absolute value of self.
For real numbers, this computes x^2.
For complex numbers z = a + b*i, this computes a^2 + b^2.
§Behavior
- In debug builds (
#[cfg(debug_assertions)]): This method callstry_abs_squared()and unwraps the result. It will panic iftry_abs_squaredreturns an error. - In release builds (
#[cfg(not(debug_assertions))]): This method directly computes the squared value for performance, bypassing the validations.
§Panics
This method will panic in debug builds if try_abs_squared() would return an Err.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".