Implementation:Avhz RustQuant Utils
| Knowledge Sources | |
|---|---|
| Domains | Utilities, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for shared utility constants and macros used across the RustQuant library.
Description
The RustQuant_utils module provides foundational testing and debugging utilities used throughout the library. It defines the RUSTQUANT_EPSILON constant, set to approximately sqrt(f64::EPSILON) (0.000_000_014_901_161_193_847_656), which serves as the default tolerance threshold for floating-point comparisons in tests. The assert_approx_equal! macro is the primary utility, accepting two floating-point expressions and a precision parameter, and asserting that the absolute difference between the two values does not exceed the precision threshold. On failure, it reports both values and the precision used. The macro is exported at the crate root level via #[macro_export] so it can be used across all RustQuant sub-crates. The module also contains a commented-out plot_vector! macro for plotting vectors using the plotters library, which supports both single-series and multi-series plotting.
Usage
Use RUSTQUANT_EPSILON and assert_approx_equal! in unit tests throughout the library when comparing floating-point results from financial calculations, numerical methods, or statistical computations where exact equality is not expected due to floating-point arithmetic.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_utils/src/lib.rs
- Lines: 1-221
Signature
pub const RUSTQUANT_EPSILON: f64 = 0.000_000_014_901_161_193_847_656;
#[macro_export]
macro_rules! assert_approx_equal {
($x:expr, $y:expr, $d:expr) => {
assert!(
($x - $y <= $d) && ($y - $x <= $d),
"\nLeft: \t\t{}, \nRight: \t\t{}, \nPrecision: \t{}\n",
$x, $y, $d
)
};
}
Import
use RustQuant::utils::{assert_approx_equal, RUSTQUANT_EPSILON};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| $x | f64 (expr) | Yes | First value to compare |
| $y | f64 (expr) | Yes | Second value to compare |
| $d | f64 (expr) | Yes | Maximum allowed absolute difference (precision) |
Outputs
| Name | Type | Description |
|---|---|---|
| () | unit | assert_approx_equal! passes silently on success |
| panic | panic | Panics with formatted message showing both values and precision on failure |
| RUSTQUANT_EPSILON | f64 | Constant approximately equal to sqrt(f64::EPSILON) |
Usage Examples
use RustQuant::utils::{assert_approx_equal, RUSTQUANT_EPSILON};
// Basic floating-point comparison
assert_approx_equal!(1.0_f64.exp(), std::f64::consts::E, f64::EPSILON);
// Using the library epsilon constant
let computed = 0.1 + 0.2;
assert_approx_equal!(computed, 0.3, RUSTQUANT_EPSILON);
// Financial calculation comparison
let expected_npv = 95.238095;
let computed_npv = 100.0 / 1.05;
assert_approx_equal!(expected_npv, computed_npv, 1e-6);
Related Pages
This is a standalone utility module with no associated principle page.