Skip to main content

Module prec

Module prec 

Source
Expand description

Provides utility functions for working with floating point precision.

This module is intended for internal use within the statrs crate to ensure consistent precision checking across all statistical computations. While it is currently public for historical reasons, it will be made private in a future breaking release.

§Usage

The module provides three main types of precision checks:

  1. Absolute difference checks (abs_diff_eq!) - Use when comparing values that should be close in absolute terms, e.g., when checking if a value is close to zero

  2. Relative difference checks (relative_eq!) - Use when comparing values that scale with the input, e.g., when comparing probability densities or statistical moments

  3. ULPs (Units in Last Place) checks (ulps_eq!) - Use for comparing values that should be close in terms of floating-point representation

Each check type has both a non-asserting version (e.g., abs_diff_eq!) and an asserting version (e.g., assert_abs_diff_eq!).

§Default Precision Levels

The module defines default precision levels that are carefully chosen to balance correctness and performance:

  • DEFAULT_RELATIVE_ACC: 1e-14 for relative comparisons
  • DEFAULT_EPS: 1e-9 for absolute comparisons
  • DEFAULT_ULPS: 5 for ULPs comparisons

These defaults should be used unless there is a specific reason to use different precision levels.

§Module-Specific Precision

Some modules may require different precision levels than the crate defaults. In such cases, the module should define its own precision constants using the same names as defined here (e.g., MODULE_RELATIVE_ACC, MODULE_EPS) to maintain consistency and searchability.

§Deprecated Functionality

The following items are deprecated and will be removed in a future release:

  • almost_eq function - Use abs_diff_eq! macro instead
  • assert_almost_eq! macro - Use assert_abs_diff_eq! macro instead

Constants§

DEFAULT_EPS
Default and target absolute accuracy for f64 operations
DEFAULT_F64_ACC
Default accuracy for f64, equivalent to 0.0 * F64_PREC
DEFAULT_RELATIVE_ACC
Default and target relative accuracy for f64 operations
DEFAULT_ULPS
Default and target ULPs accuracy for f64 operations
F64_PREC
Standard epsilon, maximum relative precision of IEEE 754 double-precision floating point numbers (64 bit) e.g. 2^-53

Functions§

almost_eqDeprecated
Compares if two floats are close via approx::abs_diff_eq using a maximum absolute difference (epsilon) of acc.