pub trait Exp: Sized {
type Error: Error;
// Required methods
fn try_exp(self) -> Result<Self, <Self as Exp>::Error>;
fn exp(self) -> Self;
}
Expand description
A trait for computing the exponential function (e^x
).
This trait provides an interface for calculating the exponential of a number,
which can be real or complex. It includes both a fallible version (try_exp
)
that performs validation and an infallible version (exp
) that may panic
in debug builds if validation fails.
§Implementors
This trait is implemented for:
f64
Complex<f64>
RealRugStrictFinite<PRECISION>
(when therug
feature is enabled)ComplexRugStrictFinite<PRECISION>
(when therug
feature is enabled)
The implementations use a StrictFinitePolicy
for validating inputs and outputs,
meaning that NaN, Infinity, and subnormal numbers (for f64
) will typically result
in an error or panic.
Required Associated Types§
Required Methods§
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 Exp for f64
impl Exp for f64
Source§fn try_exp(self) -> Result<Self, Self::Error>
fn try_exp(self) -> Result<Self, Self::Error>
Attempts to compute the exponential of self
(e^self
), returning a Result
.
This method first validates the input self
using StrictFinitePolicy
.
If the input is valid, it computes the exponential and then validates the result
using the same policy.
§Returns
Ok(Self)
: If both the input and the computed exponential are valid (finite).Err(Self::Error)
: If the input is invalid (e.g., NaN, Infinity) or if the computed exponential is invalid (e.g., NaN, Infinity, overflow).
§Examples
use num_valid::functions::Exp;
use num::Complex;
// For f64
let x = 2.0_f64;
match x.try_exp() {
Ok(val) => println!("e^{} = {}", x, val), // e^2.0 = 7.389...
Err(e) => println!("Error: {:?}", e),
}
assert!(f64::NAN.try_exp().is_err());
// For Complex<f64>
let z = Complex::new(1.0, std::f64::consts::PI); // e^(1 + iπ) = e * e^(iπ) = e * (-1) = -e
match z.try_exp() {
Ok(val) => println!("e^({:?}) = {:?}", z, val), // e^Complex { re: 1.0, im: 3.14... } = Complex { re: -2.718..., im: tiny }
Err(e) => println!("Error: {:?}", e),
}
Source§fn exp(self) -> Self
fn exp(self) -> Self
Computes and returns the exponential of self
(e^self
).
§Behavior
- Debug Builds (
#[cfg(debug_assertions)]
): This method internally callstry_exp().unwrap()
. It will panic if the inputself
is invalid (e.g., NaN, Infinity) or if the computed exponential is invalid. - Release Builds (
#[cfg(not(debug_assertions))]
): This method calls the underlying exponential function directly (e.g.,f64::exp
,num::Complex::exp
). The behavior for non-finite inputs or outputs (like NaN propagation or overflow resulting in Infinity) depends on the underlying implementation for the specific type.
§Panics
In debug builds, this method will panic if try_exp()
would return an Err
.
§Examples
use num_valid::functions::Exp;
use num::Complex;
let x = 1.0_f64;
println!("e^{} = {}", x, x.exp()); // e^1.0 = 2.718...
let z = Complex::new(0.0, std::f64::consts::PI / 2.0); // e^(iπ/2) = i
println!("e^({:?}) = {:?}", z, z.exp()); // e^Complex { re: 0.0, im: 1.57... } = Complex { re: tiny, im: 1.0 }
type Error = FunctionErrors<ExpInputErrors<f64>, <f64 as RawScalarTrait>::ValidationErrors>
Source§impl Exp for Complex<f64>
impl Exp for Complex<f64>
Source§fn try_exp(self) -> Result<Self, Self::Error>
fn try_exp(self) -> Result<Self, Self::Error>
Attempts to compute the exponential of self
(e^self
), returning a Result
.
This method first validates the input self
using StrictFinitePolicy
.
If the input is valid, it computes the exponential and then validates the result
using the same policy.
§Returns
Ok(Self)
: If both the input and the computed exponential are valid (finite).Err(Self::Error)
: If the input is invalid (e.g., NaN, Infinity) or if the computed exponential is invalid (e.g., NaN, Infinity, overflow).
§Examples
use num_valid::functions::Exp;
use num::Complex;
// For f64
let x = 2.0_f64;
match x.try_exp() {
Ok(val) => println!("e^{} = {}", x, val), // e^2.0 = 7.389...
Err(e) => println!("Error: {:?}", e),
}
assert!(f64::NAN.try_exp().is_err());
// For Complex<f64>
let z = Complex::new(1.0, std::f64::consts::PI); // e^(1 + iπ) = e * e^(iπ) = e * (-1) = -e
match z.try_exp() {
Ok(val) => println!("e^({:?}) = {:?}", z, val), // e^Complex { re: 1.0, im: 3.14... } = Complex { re: -2.718..., im: tiny }
Err(e) => println!("Error: {:?}", e),
}
Source§fn exp(self) -> Self
fn exp(self) -> Self
Computes and returns the exponential of self
(e^self
).
§Behavior
- Debug Builds (
#[cfg(debug_assertions)]
): This method internally callstry_exp().unwrap()
. It will panic if the inputself
is invalid (e.g., NaN, Infinity) or if the computed exponential is invalid. - Release Builds (
#[cfg(not(debug_assertions))]
): This method calls the underlying exponential function directly (e.g.,f64::exp
,num::Complex::exp
). The behavior for non-finite inputs or outputs (like NaN propagation or overflow resulting in Infinity) depends on the underlying implementation for the specific type.
§Panics
In debug builds, this method will panic if try_exp()
would return an Err
.
§Examples
use num_valid::functions::Exp;
use num::Complex;
let x = 1.0_f64;
println!("e^{} = {}", x, x.exp()); // e^1.0 = 2.718...
let z = Complex::new(0.0, std::f64::consts::PI / 2.0); // e^(iπ/2) = i
println!("e^({:?}) = {:?}", z, z.exp()); // e^Complex { re: 0.0, im: 1.57... } = Complex { re: tiny, im: 1.0 }