Skip to content

A library for doing approximate comparisons of finite precision numbers.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

lambdaxymox/approx_cmp

Repository files navigation

Approximate Comparison Library

Introduction

approx_cmp is a library that provides a comprehensive feature set for doing finite precision numeric comparisons, specifically for collections of IEEE 754 floating point numbers, but for other number systems as well.

Getting Started

To use the library in your project, add the following line to your Cargo.toml file

[dependencies]
approx_cmp = "1.0.0"

and then place the crate declaration in either your lib.rs or main.rs file

extern crate approx_cmp;

The library aims to be as platform agnostic as possible, including no_std environments. By default, the library is compatible with any environment that supports the full Rust std library. However, the library can operate with just the core or alloc libraries as well. You can explicitly support either core or alloc by adding

[dependencies.approx_cmp]
features = "core"

for core crate support, and

[dependencies.approx_cmp]
features = "alloc"

for alloc crate support. The core feature configured the library to provide implementations of approx_cmp's features for data types found in the core crate, and mutatis mutandis for the alloc and std crates. In particular, alloc and std provide implementations for data structures found in the respective crates. Data types found in std but not alloc are not available using the alloc feature, and similarly for alloc and core.

Features

The approx_cmp crate provides a rich set of features for doing finite precision arithmetic comparisons using the following comparison algorithms:

  • Absolute difference equality comparisons
  • Relative difference equality comparisons
  • Units In Last Place equality comparisons
  • Debugging traits for error reporting in case of failed comparisons
  • The capacity to define approximate comparison operations on custom data types.
  • A set of macros for each comparison algorithm making approximate comparisons tidier, and making debugging and logging and more understandable.

The library interfaces are designed to be number system agnostic. Typically one would be interested in IEEE 754 floating point number comparisons, but the comparison traits can be implemented for other numeric representations too, such as posits.

For more details about the specifics of the comparison algorithms provided by the library, see the relevant documentation for that comparison algorithm trait.

Prior Work

The design of the comparison algorithms in this crate is heavily informed by the contents of the website Comparing Floating Point Numbers, 2012 Edition as well as the approx and float-eq crates. See the references section.

References