hyperlattice 0.4.0

A small Rust linear algebra library with a crate-owned Scalar type and realistic or approximate backends.
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
use std::fmt;
use std::ops::{Add, Mul, Neg, Sub};

use crate::backend::{Backend, BackendScalar as BackendScalarTrait};
use crate::{
    AbortSignal, BlasResult, Problem, ScalarFacts, ScalarMagnitudeBits, ScalarSign, ZeroStatus,
};

const ROUNDING_EPSILON: f64 = f64::EPSILON;

#[derive(Clone, Debug)]
pub struct BackendScalar {
    pub(crate) value: f64,
    pub(crate) epsilon: f64,
}

/// Backend marker for approximate `f64` values with absolute error bounds.
#[derive(Clone, Debug, PartialEq)]
pub struct ApproxBackend;

impl Backend for ApproxBackend {
    type Repr = BackendScalar;
}

impl BackendScalar {
    pub(crate) fn new(value: f64, epsilon: f64) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "new");
        if value.is_nan() || epsilon.is_nan() || epsilon < 0.0 {
            return Err(Problem::NotANumber);
        }
        if value.is_infinite() || epsilon.is_infinite() {
            return Err(Problem::Infinity);
        }
        Ok(Self { value, epsilon })
    }

    fn rounded(value: f64) -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "rounded");
        Self {
            value,
            epsilon: ROUNDING_EPSILON * value.abs(),
        }
    }

    fn rounded_with_input(value: f64, input_epsilon: f64) -> Self {
        crate::trace_dispatch!(
            "hyperlattice_approx_backend",
            "constructor",
            "rounded-with-input"
        );
        Self {
            value,
            epsilon: input_epsilon + ROUNDING_EPSILON * value.abs(),
        }
    }

    fn from_unary(value: f64, propagated_epsilon: f64) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "from-unary");
        Self::new(
            value,
            propagated_epsilon.abs() + ROUNDING_EPSILON * value.abs(),
        )
    }
}

fn product_epsilon(left: &BackendScalar, right: &BackendScalar, product: f64) -> f64 {
    left.value.abs() * right.epsilon
        + right.value.abs() * left.epsilon
        + left.epsilon * right.epsilon
        + ROUNDING_EPSILON * product.abs()
}

impl BackendScalarTrait for BackendScalar {
    fn zero() -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "zero");
        Self {
            value: 0.0,
            epsilon: 0.0,
        }
    }

    fn one() -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "one");
        Self {
            value: 1.0,
            epsilon: 0.0,
        }
    }

    fn e() -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "e");
        Self::rounded(std::f64::consts::E)
    }

    fn pi() -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "pi");
        Self::rounded(std::f64::consts::PI)
    }

    fn inverse(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "inverse-owned");
        Self::one().div(self)
    }

    #[inline]
    fn inverse_ref(&self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "inverse-ref");
        Self::div_refs(&Self::one(), self)
    }

    fn pow(self, exponent: Self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "pow");
        let lower = self.value - self.epsilon;
        let upper = self.value + self.epsilon;
        let exponent_is_known_integer = exponent.epsilon == 0.0 && exponent.value.fract() == 0.0;

        if lower < 0.0 && !exponent_is_known_integer {
            return if upper < 0.0 {
                Err(Problem::NotANumber)
            } else {
                Err(Problem::UnknownZero)
            };
        }
        if exponent.value < 0.0 {
            match self.zero_status() {
                ZeroStatus::Zero => return Err(Problem::DivideByZero),
                ZeroStatus::Unknown => return Err(Problem::UnknownZero),
                ZeroStatus::NonZero => {}
            }
        }
        let center = self.value.powf(exponent.value);
        Self::from_unary(center, self.epsilon + exponent.epsilon)
    }

    #[inline]
    fn add_refs(left: &Self, right: &Self) -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "add-ref-ref");
        let value = left.value + right.value;
        Self {
            value,
            epsilon: left.epsilon + right.epsilon + ROUNDING_EPSILON * value.abs(),
        }
    }

    #[inline]
    fn add_owned_ref(left: Self, right: &Self) -> Self {
        Self::add_refs(&left, right)
    }

    #[inline]
    fn add_ref_owned(left: &Self, right: Self) -> Self {
        Self::add_refs(left, &right)
    }

    #[inline]
    fn sub_refs(left: &Self, right: &Self) -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "sub-ref-ref");
        let value = left.value - right.value;
        Self {
            value,
            epsilon: left.epsilon + right.epsilon + ROUNDING_EPSILON * value.abs(),
        }
    }

    #[inline]
    fn sub_owned_ref(left: Self, right: &Self) -> Self {
        Self::sub_refs(&left, right)
    }

    #[inline]
    fn sub_ref_owned(left: &Self, right: Self) -> Self {
        Self::sub_refs(left, &right)
    }

    #[inline]
    fn mul_refs(left: &Self, right: &Self) -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "mul-ref-ref");
        let value = left.value * right.value;
        let epsilon = product_epsilon(left, right, value);
        Self { value, epsilon }
    }

    #[inline]
    fn mul_owned_ref(left: Self, right: &Self) -> Self {
        Self::mul_refs(&left, right)
    }

    #[inline]
    fn mul_ref_owned(left: &Self, right: Self) -> Self {
        Self::mul_refs(left, &right)
    }

    #[inline]
    fn div_refs(left: &Self, right: &Self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "div-ref-ref");
        match right.zero_status() {
            ZeroStatus::Zero => return Err(Problem::DivideByZero),
            ZeroStatus::Unknown => return Err(Problem::UnknownZero),
            ZeroStatus::NonZero => {}
        }

        let center = left.value / right.value;
        let denom = right.value.abs() - right.epsilon;
        let epsilon = left.epsilon / denom + left.value.abs() * right.epsilon / (denom * denom);
        Self::new(center, epsilon + ROUNDING_EPSILON * center.abs())
    }

    #[inline]
    fn div_owned_ref(left: Self, right: &Self) -> BlasResult<Self> {
        Self::div_refs(&left, right)
    }

    #[inline]
    fn div_ref_owned(left: &Self, right: Self) -> BlasResult<Self> {
        Self::div_refs(left, &right)
    }

    #[inline]
    fn dot3(left: [&Self; 3], right: [&Self; 3]) -> Self {
        // Keep the approximate backend's dot product fully scalar and unrolled. This avoids
        // temporary arrays/iterators while still accumulating the uncertainty terms once.
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "dot3-specialized");
        let p0 = left[0].value * right[0].value;
        let p1 = left[1].value * right[1].value;
        let p2 = left[2].value * right[2].value;
        let sum01 = p0 + p1;
        let value = sum01 + p2;
        let epsilon = product_epsilon(left[0], right[0], p0)
            + product_epsilon(left[1], right[1], p1)
            + product_epsilon(left[2], right[2], p2)
            + ROUNDING_EPSILON * (sum01.abs() + value.abs());
        Self { value, epsilon }
    }

    #[inline]
    fn dot4(left: [&Self; 4], right: [&Self; 4]) -> Self {
        // Pairwise summation mirrors the hyperreal dot4 shape and keeps the rounding
        // envelope tighter than a longer left-associated chain.
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "dot4-specialized");
        let p0 = left[0].value * right[0].value;
        let p1 = left[1].value * right[1].value;
        let p2 = left[2].value * right[2].value;
        let p3 = left[3].value * right[3].value;
        let sum01 = p0 + p1;
        let sum23 = p2 + p3;
        let value = sum01 + sum23;
        let epsilon = product_epsilon(left[0], right[0], p0)
            + product_epsilon(left[1], right[1], p1)
            + product_epsilon(left[2], right[2], p2)
            + product_epsilon(left[3], right[3], p3)
            + ROUNDING_EPSILON * (sum01.abs() + sum23.abs() + value.abs());
        Self { value, epsilon }
    }

    #[inline]
    fn linear_combination3(left: [&Self; 3], right: [&Self; 3]) -> Self {
        // Keep approximate backend behavior as shared accumulation for the
        // coefficient/value matrix-vector common case while avoiding additional
        // temporary constructors.
        crate::trace_dispatch!(
            "hyperlattice_approx_backend",
            "op",
            "linear-combination3-specialized"
        );
        Self::dot3(left, right)
    }

    #[inline]
    fn linear_combination4(left: [&Self; 4], right: [&Self; 4]) -> Self {
        // Same shape as dot4; the dedicated hook keeps benchmarking and call
        // sites stable without changing numeric behavior.
        crate::trace_dispatch!(
            "hyperlattice_approx_backend",
            "op",
            "linear-combination4-specialized"
        );
        Self::dot4(left, right)
    }

    #[inline]
    fn affine_combination4(coeffs: [&Self; 4], values: [&Self; 4], offset: &Self) -> Self {
        let zero0 = coeffs[0].definitely_zero() || values[0].definitely_zero();
        let zero1 = coeffs[1].definitely_zero() || values[1].definitely_zero();
        let zero2 = coeffs[2].definitely_zero() || values[2].definitely_zero();
        let zero3 = coeffs[3].definitely_zero() || values[3].definitely_zero();
        if zero0 && zero1 && zero2 && zero3 {
            if offset.definitely_zero() {
                crate::trace_dispatch!(
                    "hyperlattice_approx_backend",
                    "op",
                    "affine-combination4-all-zero"
                );
                return Self::zero();
            }
            crate::trace_dispatch!(
                "hyperlattice_approx_backend",
                "op",
                "affine-combination4-all-zero-offset"
            );
            return offset.clone();
        }

        // Affine 4-arity is a linear 4-form plus pre-composed offset.
        if offset.definitely_zero() {
            // Preserve linear-specialized behavior for no-offset homogeneous
            // transforms where addition would only add rounding noise.
            crate::trace_dispatch!(
                "hyperlattice_approx_backend",
                "op",
                "affine-combination4-offset-zero"
            );
            return Self::dot4(coeffs, values);
        }

        // Keep the same exact shared accumulation from the linear helper and
        // only add the offset when it is definitely non-zero.
        crate::trace_dispatch!(
            "hyperlattice_approx_backend",
            "op",
            "affine-combination4-specialized"
        );
        Self::dot4(coeffs, values).add_ref(offset)
    }

    fn exp(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "exp");
        Self::from_unary(self.value.exp(), self.epsilon)
    }

    fn ln(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "ln");
        if self.value + self.epsilon <= 0.0 {
            return Err(Problem::NotANumber);
        }
        if self.value - self.epsilon <= 0.0 {
            return Err(Problem::UnknownZero);
        }
        Self::from_unary(self.value.ln(), self.epsilon / self.value.abs())
    }

    fn log10(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "log10");
        if self.value + self.epsilon <= 0.0 {
            return Err(Problem::NotANumber);
        }
        if self.value - self.epsilon <= 0.0 {
            return Err(Problem::UnknownZero);
        }
        Self::from_unary(
            self.value.log10(),
            self.epsilon / (self.value.abs() * std::f64::consts::LN_10),
        )
    }

    fn sqrt(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "sqrt");
        let lower = self.value - self.epsilon;
        let upper = self.value + self.epsilon;
        if upper < 0.0 {
            return Err(Problem::SqrtNegative);
        }
        if lower < 0.0 {
            return Err(Problem::UnknownZero);
        }

        let lower_sqrt = lower.sqrt();
        let upper_sqrt = upper.sqrt();
        let value = (lower_sqrt + upper_sqrt) / 2.0;
        let epsilon = (upper_sqrt - lower_sqrt) / 2.0;
        Self::from_unary(value, epsilon)
    }

    fn sin(self) -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "sin");
        Self::rounded_with_input(self.value.sin(), self.epsilon)
    }

    fn cos(self) -> Self {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "cos");
        Self::rounded_with_input(self.value.cos(), self.epsilon)
    }

    fn tan(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "tan");
        let cos = self.value.cos();
        if cos.abs() <= self.epsilon {
            return Err(Problem::NotANumber);
        }
        Self::from_unary(self.value.tan(), self.epsilon / (cos * cos).abs())
    }

    fn asin(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "asin");
        let lower = self.value - self.epsilon;
        let upper = self.value + self.epsilon;
        if upper < -1.0 || lower > 1.0 {
            return Err(Problem::NotANumber);
        }
        if lower < -1.0 || upper > 1.0 {
            return Err(Problem::UnknownZero);
        }
        if self.epsilon == 0.0 {
            return Self::from_unary(self.value.asin(), 0.0);
        }
        Self::from_unary(
            self.value.asin(),
            self.epsilon / (1.0 - self.value * self.value).sqrt(),
        )
    }

    fn acos(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "acos");
        let lower = self.value - self.epsilon;
        let upper = self.value + self.epsilon;
        if upper < -1.0 || lower > 1.0 {
            return Err(Problem::NotANumber);
        }
        if lower < -1.0 || upper > 1.0 {
            return Err(Problem::UnknownZero);
        }
        if self.epsilon == 0.0 {
            return Self::from_unary(self.value.acos(), 0.0);
        }
        Self::from_unary(
            self.value.acos(),
            self.epsilon / (1.0 - self.value * self.value).sqrt(),
        )
    }

    fn atan(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "atan");
        Self::from_unary(
            self.value.atan(),
            self.epsilon / (1.0 + self.value * self.value),
        )
    }

    fn asinh(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "asinh");
        Self::from_unary(
            self.value.asinh(),
            self.epsilon / (1.0 + self.value * self.value).sqrt(),
        )
    }

    fn acosh(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "acosh");
        let lower = self.value - self.epsilon;
        let upper = self.value + self.epsilon;
        if upper < 1.0 {
            return Err(Problem::NotANumber);
        }
        if lower < 1.0 {
            return Err(Problem::UnknownZero);
        }
        if self.epsilon == 0.0 {
            return Self::from_unary(self.value.acosh(), 0.0);
        }
        Self::from_unary(
            self.value.acosh(),
            self.epsilon / ((self.value - 1.0).sqrt() * (self.value + 1.0).sqrt()),
        )
    }

    fn atanh(self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "method", "atanh");
        let lower = self.value - self.epsilon;
        let upper = self.value + self.epsilon;
        if lower <= -1.0 || upper >= 1.0 {
            if self.epsilon == 0.0 && self.value.abs() == 1.0 {
                return Err(Problem::Infinity);
            }
            return if upper < -1.0 || lower > 1.0 {
                Err(Problem::NotANumber)
            } else {
                Err(Problem::UnknownZero)
            };
        }
        Self::from_unary(
            self.value.atanh(),
            self.epsilon / (1.0 - self.value * self.value).abs(),
        )
    }

    fn div(self, rhs: Self) -> BlasResult<Self> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "op", "div-owned-owned");
        match rhs.zero_status() {
            ZeroStatus::Zero => return Err(Problem::DivideByZero),
            ZeroStatus::Unknown => return Err(Problem::UnknownZero),
            ZeroStatus::NonZero => {}
        }

        let center = self.value / rhs.value;
        let denom = rhs.value.abs() - rhs.epsilon;
        let epsilon = self.epsilon / denom + self.value.abs() * rhs.epsilon / (denom * denom);
        Self::new(center, epsilon + ROUNDING_EPSILON * center.abs())
    }

    #[inline(always)]
    fn definitely_zero(&self) -> bool {
        crate::trace_dispatch!("hyperlattice_approx_backend", "query", "definitely-zero");
        self.value == 0.0 && self.epsilon == 0.0
    }

    #[inline(always)]
    fn definitely_one(&self) -> bool {
        crate::trace_dispatch!("hyperlattice_approx_backend", "query", "definitely-one");
        self.value == 1.0 && self.epsilon == 0.0
    }

    #[inline(always)]
    fn zero_or_one(&self) -> Option<bool> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "query", "zero-or-one");
        if self.value == 0.0 && self.epsilon == 0.0 {
            Some(false)
        } else if self.value == 1.0 && self.epsilon == 0.0 {
            Some(true)
        } else {
            None
        }
    }

    #[inline]
    fn zero_status(&self) -> ZeroStatus {
        crate::trace_dispatch!("hyperlattice_approx_backend", "query", "zero-status");
        if self.definitely_zero() {
            ZeroStatus::Zero
        } else if self.value.abs() > self.epsilon {
            ZeroStatus::NonZero
        } else {
            ZeroStatus::Unknown
        }
    }

    #[inline]
    fn structural_facts(&self) -> ScalarFacts {
        crate::trace_dispatch!("hyperlattice_approx_backend", "query", "structural-facts");
        let zero = self.zero_status();
        let sign = if self.definitely_zero() {
            Some(ScalarSign::Zero)
        } else if self.value - self.epsilon > 0.0 {
            Some(ScalarSign::Positive)
        } else if self.value + self.epsilon < 0.0 {
            Some(ScalarSign::Negative)
        } else {
            None
        };
        let magnitude = match zero {
            ZeroStatus::NonZero => {
                let lower = (self.value.abs() - self.epsilon).max(0.0);
                if lower > 0.0 && lower.is_finite() {
                    Some(ScalarMagnitudeBits {
                        msd: lower.log2().floor() as i32,
                        exact_msd: self.epsilon == 0.0,
                    })
                } else {
                    None
                }
            }
            ZeroStatus::Zero | ZeroStatus::Unknown => None,
        };

        ScalarFacts {
            sign,
            zero,
            exact_rational: false,
            magnitude,
        }
    }

    fn abort(&mut self, _signal: AbortSignal) {
        crate::trace_dispatch!("hyperlattice_approx_backend", "query", "attach-abort-noop");
    }

    #[inline(always)]
    fn into_f64(self) -> f64 {
        crate::trace_dispatch!("hyperlattice_approx_backend", "conversion", "into-f64");
        self.value
    }

    #[inline]
    fn to_f64_approx(&self) -> Option<f64> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "conversion", "to-f64-approx");
        self.value.is_finite().then_some(self.value)
    }
}

impl PartialEq for BackendScalar {
    fn eq(&self, rhs: &Self) -> bool {
        // Avoid `inf - inf` in the tolerance check; it produces NaN and would
        // make equal overflowed centers compare false.
        if self.value == rhs.value {
            return true;
        }
        if !self.value.is_finite() || !rhs.value.is_finite() {
            return false;
        }
        (self.value - rhs.value).abs() <= self.epsilon + rhs.epsilon
    }
}

impl fmt::Display for BackendScalar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

macro_rules! impl_integer_conversion {
    ($($ty:ty),* $(,)?) => {
        $(
            impl From<$ty> for BackendScalar {
                fn from(value: $ty) -> Self {
                    Self {
                        value: value as f64,
                        epsilon: 0.0,
                    }
                }
            }
        )*
    };
}

impl_integer_conversion!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);

impl TryFrom<f32> for BackendScalar {
    type Error = Problem;

    fn try_from(value: f32) -> Result<Self, Self::Error> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "try-from-f32");
        Self::new(value.into(), 0.0)
    }
}

impl TryFrom<f64> for BackendScalar {
    type Error = Problem;

    fn try_from(value: f64) -> Result<Self, Self::Error> {
        crate::trace_dispatch!("hyperlattice_approx_backend", "constructor", "try-from-f64");
        Self::new(value, 0.0)
    }
}

impl Add for BackendScalar {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        crate::trace_dispatch!("hyperlattice_approx_backend", "trait_op", "add-owned-owned");
        let value = self.value + rhs.value;
        Self {
            value,
            epsilon: self.epsilon + rhs.epsilon + ROUNDING_EPSILON * value.abs(),
        }
    }
}

impl Sub for BackendScalar {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        crate::trace_dispatch!("hyperlattice_approx_backend", "trait_op", "sub-owned-owned");
        let value = self.value - rhs.value;
        Self {
            value,
            epsilon: self.epsilon + rhs.epsilon + ROUNDING_EPSILON * value.abs(),
        }
    }
}

impl Neg for BackendScalar {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self::Output {
        crate::trace_dispatch!("hyperlattice_approx_backend", "trait_op", "neg-owned");
        Self {
            value: -self.value,
            epsilon: self.epsilon,
        }
    }
}

impl Mul for BackendScalar {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: Self) -> Self::Output {
        crate::trace_dispatch!("hyperlattice_approx_backend", "trait_op", "mul-owned-owned");
        let value = self.value * rhs.value;
        let epsilon = product_epsilon(&self, &rhs, value);
        Self { value, epsilon }
    }
}