Skip to content

Commit

Permalink
Improve doc examples withassert_relative_eq
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Delmas <[email protected]>
  • Loading branch information
tdelmas committed Aug 8, 2023
1 parent 816db3f commit 98c5245
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
43 changes: 43 additions & 0 deletions typed_floats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,49 @@
#![warn(clippy::indexing_slicing)]
#![warn(missing_docs)]

/// This macros assert that two values are close to each other.
///
/// # Examples
///
/// ```
/// # use typed_floats::*;
/// assert_relative_eq!(1.0_f64, 1.0);
/// assert_relative_eq!(1.0_f64, 1.000000001, 1e-7);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_relative_eq!(2.0_f64, 1.0);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_relative_eq!(1.0_f64, 1.000001, 1e-7);
/// ```
#[macro_export]
macro_rules! assert_relative_eq {
($left:expr, $right:expr) => {{
let epsilon = 1e-7;

assert_relative_eq!($left, $right, epsilon);
}};
($left:expr, $right:expr, $epsilon:expr) => {{
let left_val = $left;
let right_val = $right;
let diff = (left_val - right_val).abs();

assert!(
diff <= $epsilon,
"assertion failed: `(left ~= right)` \
(left: `{:?}`, right: `{:?}`, (left - right): `{:?}`, epsilon: `{:?}`)",
left_val,
right_val,
diff,
$epsilon
);
}};
}

/// This trait is used to specify the return type of the [`Hypot::hypot()`] function.
pub trait Hypot<T> {
/// The resulting type after applying [`Hypot::hypot()`].
Expand Down
26 changes: 13 additions & 13 deletions typed_floats_macros/src/impl_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,12 @@ pub(crate) fn get_impl_self() -> Vec<Op> {
/// let e: NonNaNFinite = core::f64::consts::FRAC_PI_2.try_into().unwrap();
/// let f: NonNaNFinite = (-core::f64::consts::FRAC_PI_2).try_into().unwrap();
///
/// assert!(a.sin().get().abs() < 1.0e-10);
/// assert!(b.sin().get().abs() < 1.0e-10);
/// assert!(c.sin().get().abs() < 1.0e-10);
/// assert!(d.sin().get().abs() < 1.0e-10);
/// assert!((e.sin().get() - 1.0).abs() < 1.0e-10);
/// assert!((f.sin().get() + 1.0).abs() < 1.0e-10);
/// assert_relative_eq!(a.sin().get(), 0.0);
/// assert_relative_eq!(b.sin().get(), 0.0);
/// assert_relative_eq!(c.sin().get(), 0.0);
/// assert_relative_eq!(d.sin().get(), 0.0);
/// assert_relative_eq!(e.sin().get(), 1.0);
/// assert_relative_eq!(f.sin().get(), -1.0);
///
/// assert!(tf64::INFINITY.sin().is_nan());
/// assert!(tf64::NEG_INFINITY.sin().is_nan());
Expand Down Expand Up @@ -678,12 +678,12 @@ pub(crate) fn get_impl_self() -> Vec<Op> {
/// let e: NonNaNFinite = core::f64::consts::FRAC_PI_2.try_into().unwrap();
/// let f: NonNaNFinite = (-core::f64::consts::FRAC_PI_2).try_into().unwrap();
///
/// assert!((a.cos().get() + 1.0).abs() < 1.0e-10);
/// assert!((b.cos().get() - 1.0).abs() < 1.0e-10);
/// assert!((c.cos().get() - 1.0).abs() < 1.0e-10);
/// assert!((d.cos().get() + 1.0).abs() < 1.0e-10);
/// assert!(e.cos().get().abs() < 1.0e-10);
/// assert!(f.cos().get().abs() < 1.0e-10);
/// assert_relative_eq!(a.cos().get(), -1.0);
/// assert_relative_eq!(b.cos().get(), 1.0);
/// assert_relative_eq!(c.cos().get(), 1.0);
/// assert_relative_eq!(d.cos().get(), -1.0);
/// assert_relative_eq!(e.cos().get(), 0.0);
/// assert_relative_eq!(f.cos().get(), 0.0);
///
/// assert!(tf64::INFINITY.cos().is_nan());
/// assert!(tf64::NEG_INFINITY.cos().is_nan());
Expand Down Expand Up @@ -718,7 +718,7 @@ pub(crate) fn get_impl_self() -> Vec<Op> {
/// # use typed_floats::*;
///
/// assert_eq!(tf64::ZERO.tan(), 0.0);
/// assert!((tf64::consts::FRAC_PI_4.tan().get() - 1.0).abs() < 1.0e-10);
/// assert_relative_eq!(tf64::consts::FRAC_PI_4.tan().get(), 1.0);
///
/// assert!(tf64::INFINITY.tan().is_nan());
/// assert!(tf64::NEG_INFINITY.tan().is_nan());
Expand Down

0 comments on commit 98c5245

Please sign in to comment.