eunomia 0.8.0

Atlas datatype law: the Single Source of Truth for numeric/scalar datatype vocabulary (scalars, complex, packed formats, conversions)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Relative-equality assertion helpers for the Atlas numeric SSOT.
//!
//! These helpers mirror the common `approx::assert_relative_eq!` API while
//! remaining `no_std`-friendly.
//!
//! Combined `epsilon`/`max_relative` checks use OR semantics matching `approx`:
//! a value passes if it is within `epsilon` **or** within `max_relative * scale`.

use core::fmt;

/// Types that can be compared with a blended absolute/relative tolerance.
///
/// The macro [`assert_relative_eq!`](macro@crate::assert_relative_eq) is the
/// primary consumer of this trait; callers generally do not need to implement
/// it themselves unless they are introducing a new numeric scalar type into
/// the Atlas ecosystem.
pub trait RelativeEq {
    /// Native tolerance type for this value.
    type Epsilon: Copy + fmt::Debug;

    /// Returns the absolute tolerance to use when the caller does not specify
    /// an `epsilon`.
    fn default_epsilon() -> Self::Epsilon;

    /// Returns the relative tolerance to use when the caller does not specify
    /// a `max_relative`.
    fn default_max_relative() -> Self::Epsilon;

    /// Returns `true` if `self` and `other` are within the given tolerances.
    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool;
}

impl RelativeEq for f32 {
    type Epsilon = Self;

    fn default_epsilon() -> Self::Epsilon {
        Self::EPSILON
    }

    fn default_max_relative() -> Self::Epsilon {
        Self::EPSILON
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        if self == other {
            return true;
        }
        if self.is_infinite() || other.is_infinite() {
            return false;
        }
        let diff = (*self - *other).abs();
        if diff <= epsilon {
            return true;
        }
        let scale = self.abs().max(other.abs());
        diff <= max_relative * scale
    }
}

impl RelativeEq for f64 {
    type Epsilon = Self;

    fn default_epsilon() -> Self::Epsilon {
        Self::EPSILON
    }

    fn default_max_relative() -> Self::Epsilon {
        Self::EPSILON
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        if self == other {
            return true;
        }
        if self.is_infinite() || other.is_infinite() {
            return false;
        }
        let diff = (*self - *other).abs();
        if diff <= epsilon {
            return true;
        }
        let scale = self.abs().max(other.abs());
        diff <= max_relative * scale
    }
}

impl<T: RelativeEq, const N: usize> RelativeEq for [T; N] {
    type Epsilon = T::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        T::default_epsilon()
    }

    fn default_max_relative() -> Self::Epsilon {
        T::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        self.iter()
            .zip(other.iter())
            .all(|(a, b)| a.relative_eq(b, epsilon, max_relative))
    }
}

impl<T: RelativeEq> RelativeEq for [T] {
    type Epsilon = T::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        T::default_epsilon()
    }

    fn default_max_relative() -> Self::Epsilon {
        T::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        if self.len() != other.len() {
            return false;
        }
        self.iter()
            .zip(other.iter())
            .all(|(a, b)| a.relative_eq(b, epsilon, max_relative))
    }
}

/// Blanket reference impl so `assert_relative_eq!(xi, di)` works when the
/// caller already holds `&f64` (e.g. from `iter().zip()`). The macro wraps each
/// operand in `&($expr)`, producing `&&T` when `$expr: &T`; this impl
/// dereferences one layer so the underlying `T: RelativeEq` is consulted.
///
/// The comparison delegates through a fully qualified `<T as RelativeEq>::`
/// call to avoid recursion through this same `&T` impl.
impl<T: RelativeEq + ?Sized> RelativeEq for &T {
    type Epsilon = T::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        T::default_epsilon()
    }

    fn default_max_relative() -> Self::Epsilon {
        T::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        <T as RelativeEq>::relative_eq(*self, *other, epsilon, max_relative)
    }
}

/// Complex-number relative equality. Two `Complex<T>` are equal iff both the
/// real and imaginary parts satisfy `T: RelativeEq` with the same tolerances.
/// This mirrors `approx`'s `RelativeEq for Complex64` behaviour and lets the
/// Atlas transform tests (Apollo, Kwavers) drop `approx` entirely.
impl<T: RelativeEq> RelativeEq for crate::Complex<T> {
    type Epsilon = T::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        T::default_epsilon()
    }

    fn default_max_relative() -> Self::Epsilon {
        T::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        self.re.relative_eq(&other.re, epsilon, max_relative)
            && self.im.relative_eq(&other.im, epsilon, max_relative)
    }
}

/// Panic helper used by [`assert_relative_eq!`](macro@crate::assert_relative_eq).
#[doc(hidden)]
#[cold]
#[inline(never)]
pub fn __relative_eq_panic<T: fmt::Debug, E: fmt::Debug>(
    left: &T,
    right: &T,
    epsilon: E,
    max_relative: E,
) -> ! {
    panic!(
        "assert_relative_eq failed\n  left: {:?}\n right: {:?}\n epsilon: {:?}\n max_relative: {:?}",
        left, right, epsilon, max_relative
    )
}

/// Resolves a value's native default absolute tolerance for exported macros.
#[doc(hidden)]
pub fn __default_epsilon<T: RelativeEq + ?Sized>(_: &T) -> T::Epsilon {
    T::default_epsilon()
}

/// Resolves a value's native default relative tolerance for exported macros.
#[doc(hidden)]
pub fn __default_max_relative<T: RelativeEq + ?Sized>(_: &T) -> T::Epsilon {
    T::default_max_relative()
}

/// Asserts that two numeric values are approximately equal.
///
/// This is a drop-in replacement for `approx::assert_relative_eq!` that is
/// implemented entirely within the Atlas `eunomia` crate and works in
/// `no_std` environments.
///
/// # Supported call patterns
///
/// ```rust
/// use eunomia::assert_relative_eq;
///
/// assert_relative_eq!(1.0_f64, 1.0000000000000001);
/// assert_relative_eq!(1.0_f64, 1.1, epsilon = 0.2);
/// assert_relative_eq!(1.0_f64, 1.1, max_relative = 0.2);
/// assert_relative_eq!(1.0_f64, 1.00000000001, epsilon = 1e-10, max_relative = 1e-10);
/// assert_relative_eq!(1.0_f64, 1.00000000001, max_relative = 1e-10, epsilon = 1e-10);
/// ```
#[macro_export]
macro_rules! assert_relative_eq {
    ($left:expr, $right:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $crate::relative_eq::__default_epsilon(left);
        let max_relative = $crate::relative_eq::__default_max_relative(left);
        if !$crate::RelativeEq::relative_eq(left, right, epsilon, max_relative) {
            $crate::relative_eq::__relative_eq_panic(left, right, epsilon, max_relative);
        }
    }};

    ($left:expr, $right:expr, epsilon = $epsilon:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $epsilon;
        let max_relative = $crate::relative_eq::__default_max_relative(left);
        if !$crate::RelativeEq::relative_eq(left, right, epsilon, max_relative) {
            $crate::relative_eq::__relative_eq_panic(left, right, epsilon, max_relative);
        }
    }};

    ($left:expr, $right:expr, max_relative = $max_relative:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $crate::relative_eq::__default_epsilon(left);
        let max_relative = $max_relative;
        if !$crate::RelativeEq::relative_eq(left, right, epsilon, max_relative) {
            $crate::relative_eq::__relative_eq_panic(left, right, epsilon, max_relative);
        }
    }};

    ($left:expr, $right:expr, epsilon = $epsilon:expr, max_relative = $max_relative:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $epsilon;
        let max_relative = $max_relative;
        if !$crate::RelativeEq::relative_eq(left, right, epsilon, max_relative) {
            $crate::relative_eq::__relative_eq_panic(left, right, epsilon, max_relative);
        }
    }};

    ($left:expr, $right:expr, max_relative = $max_relative:expr, epsilon = $epsilon:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $epsilon;
        let max_relative = $max_relative;
        if !$crate::RelativeEq::relative_eq(left, right, epsilon, max_relative) {
            $crate::relative_eq::__relative_eq_panic(left, right, epsilon, max_relative);
        }
    }};
}

/// Asserts that two numeric values are within an absolute tolerance of each
/// other.
///
/// This is a drop-in replacement for `approx::assert_abs_diff_eq!` that is
/// implemented entirely within the Atlas `eunomia` crate.
///
/// # Supported call patterns
///
/// ```rust
/// use eunomia::assert_abs_diff_eq;
///
/// assert_abs_diff_eq!(1.0_f64, 1.0000000000000001);
/// assert_abs_diff_eq!(1.0_f64, 1.1, epsilon = 0.2);
/// ```
#[macro_export]
macro_rules! assert_abs_diff_eq {
    ($left:expr, $right:expr $(,)?) => {{
        $crate::assert_relative_eq!($left, $right, max_relative = 0.0)
    }};

    ($left:expr, $right:expr, epsilon = $epsilon:expr $(,)?) => {{
        $crate::assert_relative_eq!($left, $right, epsilon = $epsilon, max_relative = 0.0)
    }};
}

/// Boolean relative-equality check (drop-in for `approx::relative_eq!`).
///
/// Returns `true` if `left` and `right` are within tolerance, `false` otherwise.
/// Unlike [`assert_relative_eq!`](macro@crate::assert_relative_eq), this macro
/// does **not** panic — callers use it inside
/// `assert!(relative_eq!(...), "custom message")` when they need a context-rich
/// failure message, matching the `approx::relative_eq!` API.
///
/// # Supported call patterns
///
/// ```rust
/// use eunomia::relative_eq;
///
/// assert!(relative_eq!(1.0_f64, 1.0000000000000001));
/// assert!(relative_eq!(1.0_f64, 1.1, epsilon = 0.2));
/// assert!(relative_eq!(1.0_f64, 1.1, max_relative = 0.2));
/// assert!(relative_eq!(1.0_f64, 1.00000000001, epsilon = 1e-10, max_relative = 1e-10));
/// ```
#[macro_export]
macro_rules! relative_eq {
    ($left:expr, $right:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $crate::relative_eq::__default_epsilon(left);
        let max_relative = $crate::relative_eq::__default_max_relative(left);
        $crate::RelativeEq::relative_eq(left, right, epsilon, max_relative)
    }};

    ($left:expr, $right:expr, epsilon = $epsilon:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $epsilon;
        let max_relative = $crate::relative_eq::__default_max_relative(left);
        $crate::RelativeEq::relative_eq(left, right, epsilon, max_relative)
    }};

    ($left:expr, $right:expr, max_relative = $max_relative:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $crate::relative_eq::__default_epsilon(left);
        let max_relative = $max_relative;
        $crate::RelativeEq::relative_eq(left, right, epsilon, max_relative)
    }};

    ($left:expr, $right:expr, epsilon = $epsilon:expr, max_relative = $max_relative:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $epsilon;
        let max_relative = $max_relative;
        $crate::RelativeEq::relative_eq(left, right, epsilon, max_relative)
    }};

    ($left:expr, $right:expr, max_relative = $max_relative:expr, epsilon = $epsilon:expr $(,)?) => {{
        let left = &($left);
        let right = &($right);
        let epsilon = $epsilon;
        let max_relative = $max_relative;
        $crate::RelativeEq::relative_eq(left, right, epsilon, max_relative)
    }};
}

/// Boolean absolute-difference equality check (drop-in for
/// `approx::abs_diff_eq!`).
///
/// Returns `true` if `|left - right| <= epsilon`, `false` otherwise. Unlike
/// [`assert_abs_diff_eq!`](macro@crate::assert_abs_diff_eq), this macro does
/// **not** panic — callers use it inside
/// `assert!(abs_diff_eq!(...), "custom message")`.
///
/// # Supported call patterns
///
/// ```rust
/// use eunomia::abs_diff_eq;
///
/// assert!(abs_diff_eq!(1.0_f64, 1.0000000000000001));
/// assert!(abs_diff_eq!(1.0_f64, 1.1, epsilon = 0.2));
/// ```
#[macro_export]
macro_rules! abs_diff_eq {
    ($left:expr, $right:expr $(,)?) => {{
        $crate::relative_eq!($left, $right, max_relative = 0.0)
    }};

    ($left:expr, $right:expr, epsilon = $epsilon:expr $(,)?) => {{
        $crate::relative_eq!($left, $right, epsilon = $epsilon, max_relative = 0.0)
    }};
}

#[cfg(test)]
mod tests;