#assert #testing #numeric #macro #no-std

no-std assert-within

Macro for testing that (generic) floating point numbers are within some tolerance

4 releases

Uses new Rust 2024

new 0.1.3 Apr 17, 2025
0.1.2 Apr 17, 2025
0.1.1 Apr 17, 2025
0.1.0 Apr 17, 2025

#1900 in Rust patterns

Download history

122 downloads per month
Used in owens-t

MIT/Apache

7KB
143 lines

Provides a macro assert_within! for tests involving floating point numbers.

assert_within!(+0.001, val, target, "Value was not within additive 0.001: {more} {context}");
assert_within!(~0.05, val, target, "Value was not within 5% of target: {additional} {information:?}");

Highlights include:

  • Pass arguments by reference or value
  • Sigils (+, ~) indicate additive or relative error
  • Traps Nan in any of the arguments
  • Errors cause both the stringified expressions and their values to be displayed
  • Arbitrary additional format args
  • Generic over num_traits::FloatCore
  • no_std compatible

Dependencies

~150KB