typed_floats/types/
mod.rs

1use const_fn::const_fn;
2
3/// An error that can occur when converting from a string into a typed float
4#[derive(Debug)]
5pub enum FromStrError {
6    /// The string did not contain a valid float number
7    ParseFloatError(core::num::ParseFloatError),
8    /// The string contained a valid float number but it didn't fit in the target type
9    InvalidNumber(InvalidNumber),
10}
11
12impl core::fmt::Display for FromStrError {
13    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14        match self {
15            Self::ParseFloatError(e) => write!(f, "{e}"),
16            Self::InvalidNumber(e) => write!(f, "{e}"),
17        }
18    }
19}
20
21#[cfg(feature = "std")]
22impl std::error::Error for FromStrError {}
23
24#[cfg(feature = "serde")]
25use serde::Serialize;
26
27/// An error that can occur when converting into a typed float
28#[derive(Debug, Eq, PartialEq)]
29pub enum InvalidNumber {
30    /// Any variant of `Nan`
31    NaN,
32    /// `+0.0` or `-0.0`
33    Zero,
34    /// Any negative number, including `-0.0` and `-inf`
35    Negative,
36    /// Any positive number, including `+0.0` and `+inf`
37    Positive,
38    /// `+inf` or `-inf`
39    Infinite,
40}
41
42impl core::fmt::Display for InvalidNumber {
43    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44        match self {
45            Self::NaN => write!(f, "Number is NaN"),
46            Self::Zero => write!(f, "Number is zero"),
47            Self::Negative => write!(f, "Number is negative"),
48            Self::Positive => write!(f, "Number is positive"),
49            Self::Infinite => write!(f, "Number is infinite"),
50        }
51    }
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for InvalidNumber {}
56
57/// A non-NaN floating point number
58///
59/// It satisfies the following constraints:
60/// - It is not NaN.
61#[cfg_attr(feature = "serde", derive(Serialize))]
62#[derive(Debug, Copy, Clone)]
63#[repr(transparent)]
64pub struct NonNaN<T = f64>(T);
65
66/// A non-NaN floating point number different from zero
67///
68/// It satisfies the following constraints:
69/// - It is not NaN.
70/// - It is not zero.
71#[cfg_attr(feature = "serde", derive(Serialize))]
72#[derive(Debug, Copy, Clone)]
73#[repr(transparent)]
74pub struct NonZeroNonNaN<T = f64>(T);
75
76/// A non-NaN finite floating point number different from zero
77///
78/// It satisfies the following constraints:
79/// - It is not NaN.
80/// - It is not infinite.
81#[cfg_attr(feature = "serde", derive(Serialize))]
82#[derive(Debug, Copy, Clone)]
83#[repr(transparent)]
84pub struct NonNaNFinite<T = f64>(T);
85
86/// A non-NaN finite floating point number different from zero
87///
88/// It satisfies the following constraints:
89/// - It is not NaN.
90/// - It is not infinite.
91/// - It is not zero.
92#[cfg_attr(feature = "serde", derive(Serialize))]
93#[derive(Debug, Copy, Clone)]
94#[repr(transparent)]
95pub struct NonZeroNonNaNFinite<T = f64>(T);
96
97/// A non-NaN positive floating point number
98///
99/// It satisfies the following constraints:
100/// - It is not NaN.
101/// - It is not negative.
102#[cfg_attr(feature = "serde", derive(Serialize))]
103#[derive(Debug, Copy, Clone)]
104#[repr(transparent)]
105pub struct Positive<T = f64>(T);
106
107/// A non-NaN negative floating point number
108///
109/// It satisfies the following constraints:
110/// - It is not NaN.
111/// - It is not positive.
112#[cfg_attr(feature = "serde", derive(Serialize))]
113#[derive(Debug, Copy, Clone)]
114#[repr(transparent)]
115pub struct Negative<T = f64>(T);
116
117/// A non-NaN positive finite floating point number
118///
119/// It satisfies the following constraints:
120/// - It is not NaN.
121/// - It is not infinite.
122/// - It is not negative.
123#[cfg_attr(feature = "serde", derive(Serialize))]
124#[derive(Debug, Copy, Clone)]
125#[repr(transparent)]
126pub struct PositiveFinite<T = f64>(T);
127
128/// A non-NaN negative finite floating point number
129///
130/// It satisfies the following constraints:
131/// - It is not NaN.
132/// - It is not infinite.
133/// - It is not positive.
134#[cfg_attr(feature = "serde", derive(Serialize))]
135#[derive(Debug, Copy, Clone)]
136#[repr(transparent)]
137pub struct NegativeFinite<T = f64>(T);
138
139/// A non-NaN strictly positive floating point number
140///
141/// It satisfies the following constraints:
142/// - It is not NaN.
143/// - It is not zero.
144/// - It is not negative.
145#[cfg_attr(feature = "serde", derive(Serialize))]
146#[derive(Debug, Copy, Clone)]
147#[repr(transparent)]
148pub struct StrictlyPositive<T = f64>(T);
149
150/// A non-NaN strictly negative floating point number
151///
152/// It satisfies the following constraints:
153/// - It is not NaN.
154/// - It is not zero.
155/// - It is not positive.
156#[cfg_attr(feature = "serde", derive(Serialize))]
157#[derive(Debug, Copy, Clone)]
158#[repr(transparent)]
159pub struct StrictlyNegative<T = f64>(T);
160
161/// A non-NaN strictly positive finite floating point number
162///
163/// It satisfies the following constraints:
164/// - It is not NaN.
165/// - It is not negative.
166#[cfg_attr(feature = "serde", derive(Serialize))]
167#[derive(Debug, Copy, Clone)]
168#[repr(transparent)]
169pub struct StrictlyPositiveFinite<T = f64>(T);
170
171/// A non-NaN strictly negative finite floating point number
172///
173/// It satisfies the following constraints:
174/// - It is not NaN.
175/// - It is not positive.
176#[cfg_attr(feature = "serde", derive(Serialize))]
177#[derive(Debug, Copy, Clone)]
178#[repr(transparent)]
179pub struct StrictlyNegativeFinite<T = f64>(T);
180
181use crate::traits::{Max, Min};
182
183#[cfg(any(feature = "std", feature = "libm"))]
184use crate::traits::{Atan2, Copysign, DivEuclid, Hypot, Powf};
185
186#[rustversion::since(1.85)]
187use crate::traits::Midpoint;
188
189#[cfg(all(feature = "libm", not(feature = "std")))]
190#[allow(unused_imports)]
191use num_traits::Float;
192
193mod accept;
194mod f32;
195mod f64;
196mod impls;
197
198typed_floats_macros::generate_floats!();