Abs

Trait Abs 

Source
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;
}
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 two methods:

  • try_abs: A fallible version that performs validation on the input (and potentially the output) and returns a Result. This is the preferred method when robust error handling is required.
  • abs: A convenient infallible version that panics if try_abs would 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.

§Associated types

  • Output: The type of the result of the absolute value operation. This is constrained by RealScalar to ensure it’s a real number type.
  • Error : The error type that can be returned by the try_abs method.

Required Associated Types§

Source

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.

Source

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§

Source

fn try_abs(self) -> Result<Self::Output, Self::Error>

Attempts to compute the absolute value of self.

Source

fn abs(self) -> Self::Output

Returns the absolute value of self.

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 Abs for f64

Source§

fn try_abs(self) -> Result<f64, Self::Error>

Attempts to compute the absolute value of self.

This method performs validation on the input value according to the StrictFinitePolicy (i.e., the input must not be NaN, Infinity, or subnormal). If the input is valid, it computes the absolute value. The result is also validated to ensure it is a finite real number.

§Returns
  • Ok(Self::Output): If the input is valid and the absolute value is successfully computed and validated.
  • Err(Self::Error): If the input fails validation, or if the computed absolute value fails validation. The specific error variant will indicate the cause of the failure.
Source§

fn abs(self) -> f64

Returns the absolute value of self.

This method provides a convenient way to compute the absolute value.

§Behavior
  • In debug builds (#[cfg(debug_assertions)]): This method calls try_abs() and unwraps the result. It will panic if try_abs returns an error (e.g., if the input is NaN or Infinity).
  • In release builds (#[cfg(not(debug_assertions))]): This method calls f64::abs() directly for performance, bypassing the validations performed by try_abs.
§Panics

This method will panic in debug builds if try_abs() would return an Err. In release builds, the behavior for invalid inputs (like NaN or Infinity) will match f64::abs() (e.g., f64::NAN.abs() is NAN).

Source§

type Output = f64

Source§

type Error = FunctionErrors<AbsInputErrors<f64>, <f64 as RawScalarTrait>::ValidationErrors>

Source§

impl Abs for Complex<f64>

Source§

fn try_abs(self) -> Result<f64, AbsComplexErrors<Complex<f64>>>

Attempts to compute the absolute value (modulus) of self.

This method performs validation on the input value according to the StrictFinitePolicy (i.e., both real and imaginary parts must be finite). If the input is valid, it computes the modulus. The result is also validated to ensure it is a finite real number.

Source§

fn abs(self) -> f64

Returns the absolute value of self.

§Behavior
  • In debug builds (#[cfg(debug_assertions)]): This method calls try_abs() and unwraps the result. It will panic if try_abs returns an error.
  • In release builds (#[cfg(not(debug_assertions))]): This method calls self.norm() directly for performance.
§Panics

This method will panic in debug builds if try_abs() would return an Err.

Source§

type Output = f64

Source§

type Error = FunctionErrors<AbsInputErrors<Complex<f64>>, <<Complex<f64> as RawComplexTrait>::RawReal as RawScalarTrait>::ValidationErrors>

Implementors§