Skip to content

Commit

Permalink
Add more functions (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelmas authored Aug 9, 2023
1 parent 83102ae commit f457372
Show file tree
Hide file tree
Showing 3 changed files with 458 additions and 23 deletions.
108 changes: 103 additions & 5 deletions typed_floats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@
#![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);
Expand All @@ -123,7 +123,7 @@ macro_rules! assert_relative_eq {
let left_val = $left;
let right_val = $right;
let diff = (left_val - right_val).abs();

assert!(
diff <= $epsilon,
"assertion failed: `(left ~= right)` \
Expand All @@ -136,6 +136,104 @@ macro_rules! assert_relative_eq {
}};
}

/// This macros assert that the value is NaN.
///
/// # Examples
///
/// ```
/// # use typed_floats::*;
/// assert_is_nan!(0.0_f64 / 0.0);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_is_nan!(2.0_f64);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_is_nan!(core::f64::INFINITY);
/// ```
#[macro_export]
macro_rules! assert_is_nan {
($left:expr) => {{
let left_val = $left;

assert!(
left_val.is_nan(),
"assertion failed: `(value is NaN)` \
(value: `{:?}`)",
left_val,
);
}};
}

/// This macros assert that the value is positive zero.
///
/// # Examples
///
/// ```
/// # use typed_floats::*;
/// assert_is_positive_zero!(0.0_f64);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_is_positive_zero!(-0.0_f64);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_is_positive_zero!(core::f64::INFINITY);
/// ```
#[macro_export]
macro_rules! assert_is_positive_zero {
($left:expr) => {{
let val = $left;

assert!(
val == 0.0 && val.is_sign_positive(),
"assertion failed: `(value is positive zero)` \
(value: `{:?}`)",
val,
);
}};
}


/// This macros assert that the value is negative zero.
///
/// # Examples
///
/// ```
/// # use typed_floats::*;
/// assert_is_negative_zero!(-0.0_f64);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_is_negative_zero!(0.0_f64);
/// ```
///
/// ```should_panic
/// # use typed_floats::*;
/// assert_is_negative_zero!(core::f64::NEG_INFINITY);
/// ```
#[macro_export]
macro_rules! assert_is_negative_zero {
($left:expr) => {{
let val = $left;

assert!(
val == 0.0 && val.is_sign_negative(),
"assertion failed: `(value is negative zero)` \
(value: `{:?}`)",
val,
);
}};
}


/// 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
Loading

0 comments on commit f457372

Please sign in to comment.