pub trait ExactF64Conversion {
type Output;
// Required methods
fn try_to_f64(&self) -> Result<Self::Output, LaError>;
fn to_rounded_f64(&self) -> Result<Self::Output, LaError>;
}Expand description
Convert an already-computed exact result to finite binary64 output.
This extension trait is implemented for BigRational determinants and
[BigRational; D] exact solutions. It lets callers retain the exact value,
try the strict no-rounding contract, and recover with explicit rounding
without repeating determinant evaluation or linear-system elimination.
BigRational::new_raw values are interpreted by their mathematical
quotient: denominator signs and common factors do not change the result. A
zero denominator is rejected as UnrepresentableReason::NotFinite.
§Examples
use la_stack::prelude::*;
let matrix = Matrix::<2>::try_from_rows([
[1.0 + f64::EPSILON, 0.0],
[0.0, 1.0 - f64::EPSILON],
])?;
let exact = matrix.det_exact()?;
let rounded = match exact.try_to_f64() {
Ok(value) => value,
Err(error) if error.requires_rounding() => exact.to_rounded_f64()?,
Err(error) => return Err(error),
};
assert_eq!(rounded.to_bits(), 1.0_f64.to_bits());
let system = Matrix::<1>::try_from_rows([[3.0]])?;
let rhs = Vector::<1>::try_new([3.0])?;
let exact_solution = system.solve_exact(rhs)?;
assert_eq!(exact_solution.try_to_f64()?.into_array(), [1.0]);Required Associated Types§
Required Methods§
Sourcefn try_to_f64(&self) -> Result<Self::Output, LaError>
fn try_to_f64(&self) -> Result<Self::Output, LaError>
Convert only when every exact value already has an exact finite binary64 representation.
The candidate conversion follows IEEE 754 round-to-nearest, ties-to-even, but this strict method returns it only when no rounding is required.
§Errors
Returns LaError::Unrepresentable with
UnrepresentableReason::RequiresRounding when finite binary64 output
would require rounding, or UnrepresentableReason::NotFinite when
rounding cannot produce finite output. Exact solution errors include the
first failing component index.
Sourcefn to_rounded_f64(&self) -> Result<Self::Output, LaError>
fn to_rounded_f64(&self) -> Result<Self::Output, LaError>
Round the exact value to finite binary64 output.
Rounding follows IEEE 754 round-to-nearest, ties-to-even.
§Errors
Returns LaError::Unrepresentable with
UnrepresentableReason::NotFinite when rounding cannot produce finite
output. Exact solution errors include the first failing component index.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".