ndarray_glm/
num.rs

1//! numerical trait constraints
2use std::cmp;
3
4use ndarray::ScalarOperand;
5use ndarray_linalg::Lapack;
6
7pub trait Float: Sized + num_traits::Float + Lapack + ScalarOperand {
8    // Return 1/2 = 0.5
9    fn half() -> Self;
10
11    /// A more conventional sign function, because the built-in signum treats signed zeros as
12    /// positive and negative: https://github.com/rust-lang/rust/issues/57543
13    fn sign(self) -> Self {
14        if self == Self::zero() {
15            Self::zero()
16        } else {
17            self.signum()
18        }
19    }
20
21    /// total_cmp is not implemented in num_traits, so implement it here.
22    fn total_cmp(&self, other: &Self) -> cmp::Ordering;
23}
24
25impl Float for f32 {
26    fn half() -> Self {
27        0.5
28    }
29
30    fn total_cmp(&self, other: &Self) -> cmp::Ordering {
31        self.total_cmp(other)
32    }
33}
34impl Float for f64 {
35    fn half() -> Self {
36        0.5
37    }
38
39    fn total_cmp(&self, other: &Self) -> cmp::Ordering {
40        self.total_cmp(other)
41    }
42}