Skip to main content

score_set/
float.rs

1use core::fmt::Debug;
2use core::ops::{Add, Div, Mul, Neg, Sub};
3
4/// Sealed trait for floating-point types supported by this crate.
5///
6/// Implemented for `f32` and `f64` only — downstream crates cannot add new impls.
7pub(crate) mod sealed {
8    use super::*;
9
10    pub trait SealedFloat:
11        Copy
12        + PartialOrd
13        + PartialEq
14        + Debug
15        + Add<Output = Self>
16        + Sub<Output = Self>
17        + Mul<Output = Self>
18        + Div<Output = Self>
19        + Neg<Output = Self>
20        + 'static
21    {
22        fn zero() -> Self;
23        fn one() -> Self;
24        fn is_finite(self) -> bool;
25        fn from_f64(v: f64) -> Self;
26        fn abs(self) -> Self;
27        fn min(self, other: Self) -> Self;
28        fn max(self, other: Self) -> Self;
29        fn exp(self) -> Self;
30        fn ln(self) -> Self;
31        fn epsilon() -> Self;
32    }
33
34    impl SealedFloat for f32 {
35        #[inline]
36        fn zero() -> Self {
37            0.0
38        }
39        #[inline]
40        fn one() -> Self {
41            1.0
42        }
43        #[inline]
44        fn is_finite(self) -> bool {
45            f32::is_finite(self)
46        }
47        #[inline]
48        fn from_f64(v: f64) -> Self {
49            v as f32
50        }
51        #[inline]
52        fn abs(self) -> Self {
53            f32::abs(self)
54        }
55        #[inline]
56        fn min(self, other: Self) -> Self {
57            f32::min(self, other)
58        }
59        #[inline]
60        fn max(self, other: Self) -> Self {
61            f32::max(self, other)
62        }
63        #[inline]
64        fn exp(self) -> Self {
65            f32::exp(self)
66        }
67        #[inline]
68        fn ln(self) -> Self {
69            f32::ln(self)
70        }
71        #[inline]
72        fn epsilon() -> Self {
73            f32::EPSILON
74        }
75    }
76
77    impl SealedFloat for f64 {
78        #[inline]
79        fn zero() -> Self {
80            0.0
81        }
82        #[inline]
83        fn one() -> Self {
84            1.0
85        }
86        #[inline]
87        fn is_finite(self) -> bool {
88            f64::is_finite(self)
89        }
90        #[inline]
91        fn from_f64(v: f64) -> Self {
92            v
93        }
94        #[inline]
95        fn abs(self) -> Self {
96            f64::abs(self)
97        }
98        #[inline]
99        fn min(self, other: Self) -> Self {
100            f64::min(self, other)
101        }
102        #[inline]
103        fn max(self, other: Self) -> Self {
104            f64::max(self, other)
105        }
106        #[inline]
107        fn exp(self) -> Self {
108            f64::exp(self)
109        }
110        #[inline]
111        fn ln(self) -> Self {
112            f64::ln(self)
113        }
114        #[inline]
115        fn epsilon() -> Self {
116            f64::EPSILON
117        }
118    }
119}
120
121/// Public trait bound for floating-point types used throughout `score-set`.
122///
123/// Blanket-implemented for `f32` and `f64`. Sealed so downstream cannot add new impls.
124pub trait Float: sealed::SealedFloat {}
125
126impl<T: sealed::SealedFloat> Float for T {}
127
128#[cfg(test)]
129mod tests_for_float;