Skip to content

Commit

Permalink
Implement Debug by hand for Interval
Browse files Browse the repository at this point in the history
This is easier for the users of this create as they do not have to
know/remember that the interval [a, b] is actually stored as [-a; b].

Fixes #103
  • Loading branch information
Chris00 committed Aug 19, 2024
1 parent 9e519ce commit 1f86961
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ pub type Result<T> = result::Result<T, IntervalError>;
/// An interval with [`f64`] bounds.
///
/// It is sometimes referred to as a *bare* interval in contrast to a decorated interval ([`DecInterval`]).
#[derive(Clone, Copy, Debug)]
///
/// Note that, for convenience, the [`Debug`][std::fmt::Debug] output
/// for nonempty intervals will look like `Interval { inf: a, sup: b }`,
/// where `a` and `b` are the `f64` bounds of the interval,
/// although, internally, intervals are not defined as a struct.
/// Empty intervals will be printed as `Interval::EMPTY`.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Interval {
// An interval is stored in a SIMD vector in the neginf-sup-nan form:
Expand All @@ -55,16 +61,27 @@ pub struct Interval {
//
// Representations of zeros and NaNs are arbitrary; a zero can be either +0.0 or -0.0,
// and a NaN can be either a qNaN or a sNaN with an arbitrary payload.
//
// In Debug formatting, the value of `rep` is printed as either
// `__m128d(-a, b)` (on x86-64) or `float64x2_t(-a, b)` (on AArch64).
pub(crate) rep: F64X2,
}

unsafe impl Send for Interval {}
unsafe impl Sync for Interval {}
impl Unpin for Interval {}

impl fmt::Debug for Interval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_empty() {
write!(f, "Interval::EMPTY")
} else {
let [a, b] = extract(self.rep);
f.debug_struct("Interval")
.field("inf", &-a)
.field("sup", &b)
.finish()
}
}
}

impl Interval {
pub(crate) fn inf_raw(self) -> f64 {
-extract0(self.rep)
Expand Down

0 comments on commit 1f86961

Please sign in to comment.