Skip to main content

Reciprocal

Trait Reciprocal 

Source
pub trait Reciprocal: Sized {
    type Error: Error;

    // Required methods
    fn try_reciprocal(self) -> Result<Self, <Self as Reciprocal>::Error>;
    fn reciprocal(self) -> Self;
}
Expand description

A trait for computing the reciprocal (1/x) of a number.

This trait provides an interface for calculating the reciprocal of a number, which can be real or complex. It includes both a fallible version (try_reciprocal) that performs validation and an infallible version (reciprocal) that may panic in debug builds if validation fails.

§Implementors

This trait is implemented for:

  • f64
  • Complex<f64>
  • RealRugStrictFinite<PRECISION> (when the rug feature is enabled)
  • ComplexRugStrictFinite<PRECISION> (when the rug feature is enabled)

§Validation Policy

Implementations typically use a StrictFinitePolicy for validating inputs and outputs. This means that NaN, Infinity, and subnormal numbers (for f64-based types) will generally result in an error or panic. Division by zero is also explicitly handled.

Required Associated Types§

Source

type Error: Error

The error type that can be returned by the try_reciprocal method.

This is typically an instantiation of ReciprocalErrors.

Required Methods§

Source

fn try_reciprocal(self) -> Result<Self, <Self as Reciprocal>::Error>

Attempts to compute the reciprocal of self (1/self), returning a Result.

This method first validates the input self using StrictFinitePolicy and also checks if it is zero. If the input is valid (finite, non-zero, normal), it computes the reciprocal and then validates the result using the same policy.

Source

fn reciprocal(self) -> Self

Returns the reciprocal of self (1/self).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§