Expand description
§Numerical Value Validation Module
This module provides a framework for validating numerical scalar values,
particularly floating-point and complex numbers, including those with arbitrary precision
using the rug
library (when the “rug” feature is enabled).
It defines:
- Error Types: Such as
ErrorsValidationRawReal
,ErrorsValidationRawComplex
, andErrorsTryFromf64
. These errors detail various validation failures like non-finite values, subnormal numbers (forf64
andrug::Float
), and precision mismatches forrug
types. - Validation Policies: Implemented via the
ValidationPolicy
trait from thetry_create
crate. The primary policy provided isStrictFinitePolicy
, which ensures that numbers are finite (not NaN or Infinity) and, forf64
andrug::Float
, not subnormal. - Floating-Point Checks Trait: The
FpChecks
trait, defined within this module, provides common classification methods likeis_finite
,is_nan
,is_infinite
, andis_normal
. This trait is implemented for standard andrug
-based scalar types. - Implementations: Validation logic for
StrictFinitePolicy
and implementations ofFpChecks
are provided forf64
,num::Complex<f64>
. When the “rug” feature is enabled, support extends torug::Float
,rug::Complex
, and indirectly to the wrapper typesRealRugStrictFinite
andComplexRugStrictFinite
(which utilize these underlyingrug
types).
The goal of this module is to ensure numerical stability and correctness by allowing functions to easily validate their inputs and outputs according to defined policies, and to provide a common interface for basic floating-point characteristic checks.
§Example: Using StrictFinitePolicy
use num_valid::validation::{StrictFinitePolicy, ErrorsValidationRawReal};
use try_create::ValidationPolicy; // For the validate method
let finite_val = 10.0f64;
let nan_val = f64::NAN;
let subnormal_val = f64::from_bits(1); // Smallest positive subnormal f64
assert!(StrictFinitePolicy::<f64, 53>::validate(finite_val).is_ok());
let nan_validation_result = StrictFinitePolicy::<f64, 53>::validate(nan_val);
assert!(nan_validation_result.is_err());
if let Err(ErrorsValidationRawReal::IsNaN { value, .. }) = nan_validation_result {
assert!(value.is_nan());
} else {
panic!("Expected NaN error for f64::NAN");
}
let subnormal_validation_result = StrictFinitePolicy::<f64, 53>::validate(subnormal_val);
assert!(subnormal_validation_result.is_err());
if let Err(ErrorsValidationRawReal::IsSubnormal { value, .. }) = subnormal_validation_result {
assert_eq!(value, subnormal_val);
} else {
panic!("Expected Subnormal error for subnormal f64");
}
§Example: Using FpChecks
use num_valid::validation::FpChecks;
use num::Complex;
let val_f64 = 1.0f64;
let nan_f64 = f64::NAN;
let inf_f64 = f64::INFINITY;
assert!(val_f64.is_finite());
assert!(val_f64.is_normal());
assert!(nan_f64.is_nan());
assert!(!nan_f64.is_finite());
assert!(inf_f64.is_infinite());
let val_complex = Complex::new(1.0, 2.0);
let nan_complex_real = Complex::new(f64::NAN, 2.0);
let inf_complex_imag = Complex::new(1.0, f64::NEG_INFINITY);
assert!(val_complex.is_finite());
assert!(val_complex.is_normal());
assert!(nan_complex_real.is_nan());
assert!(!nan_complex_real.is_finite());
assert!(inf_complex_imag.is_infinite());
assert!(!inf_complex_imag.is_finite());
assert!(!inf_complex_imag.is_nan());
Structs§
- Debug
Validation Policy - A validation policy that is active only in debug builds.
- Strict
Finite Policy - A validation policy that checks for strict finiteness.
Enums§
- Errors
TryFromf64 - Errors that can occur when trying to convert an
f64
to another real-type scalar of typeRawReal
. - Errors
Validation RawComplex - Errors that can occur during the validation of a complex number.
- Errors
Validation RawReal - Errors that can occur during the validation of a raw real number.
Traits§
- FpChecks
- Provides a set of fundamental floating-point classification checks.
- Validation
Policy Complex - A marker for policies that apply to complex types.
- Validation
Policy Real - A marker for policies that apply to real types.
Functions§
- debug_
validate - Validates a value using the given policy, but only in debug builds. In release builds, this function is a no-op and should be optimized away.
Type Aliases§
- Native64
RawComplex Strict Finite Policy - A type alias for a validation policy that enforces strict finiteness for the raw
num::Complex<f64>
type. - Native64
RawReal Strict Finite Policy - A type alias for a validation policy that enforces strict finiteness for the raw
f64
type.