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
/// An error that can occur when converting from a string into a `TypedFloat`
#[derive(Debug)]
pub enum FromStrError {
    /// The string did not contain a valid float number
    ParseFloatError(core::num::ParseFloatError),
    /// The string contained a valid float number but it didn't fit in the target type
    InvalidNumber(InvalidNumber),
}

impl core::fmt::Display for FromStrError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::ParseFloatError(e) => write!(f, "{e}"),
            Self::InvalidNumber(e) => write!(f, "{e}"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for FromStrError {}

#[cfg(feature = "serde")]
use serde::Serialize;

/// An error that can occur when converting into a typed float
#[derive(Debug, Eq, PartialEq)]
pub enum InvalidNumber {
    /// Any variant of `Nan`
    NaN,
    /// `+0.0` or `-0.0`
    Zero,
    /// Any negative number, including `-0.0` and `-inf`
    Negative,
    /// Any positive number, including `+0.0` and `+inf`
    Positive,
    /// `+inf` or `-inf`
    Infinite,
}

impl core::fmt::Display for InvalidNumber {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::NaN => write!(f, "Number is NaN"),
            Self::Zero => write!(f, "Number is zero"),
            Self::Negative => write!(f, "Number is negative"),
            Self::Positive => write!(f, "Number is positive"),
            Self::Infinite => write!(f, "Number is infinite"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidNumber {}

/// A non-NaN floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct NonNaN<T = f64>(T);

/// A non-NaN floating point number different from zero
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not zero.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct NonZeroNonNaN<T = f64>(T);

/// A non-NaN finite floating point number different from zero
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not infinite.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct NonNaNFinite<T = f64>(T);

/// A non-NaN finite floating point number different from zero
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not infinite.
/// - It is not zero.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct NonZeroNonNaNFinite<T = f64>(T);

/// A non-NaN positive floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not negative.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct Positive<T = f64>(T);

/// A non-NaN negative floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not positive.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct Negative<T = f64>(T);

/// A non-NaN positive finite floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not infinite.
/// - It is not negative.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct PositiveFinite<T = f64>(T);

/// A non-NaN negative finite floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not infinite.
/// - It is not positive.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct NegativeFinite<T = f64>(T);

/// A non-NaN strictly positive floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not zero.
/// - It is not negative.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct StrictlyPositive<T = f64>(T);

/// A non-NaN strictly negative floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not zero.
/// - It is not positive.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct StrictlyNegative<T = f64>(T);

/// A non-NaN strictly positive finite floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not negative.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct StrictlyPositiveFinite<T = f64>(T);

/// A non-NaN strictly negative finite floating point number
///
/// It satisfies the following constraints:
/// - It is not NaN.
/// - It is not positive.
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct StrictlyNegativeFinite<T = f64>(T);

use crate::traits::{Max, Min};

#[cfg(any(feature = "std", feature = "libm"))]
use crate::traits::{Atan2, Copysign, DivEuclid, Hypot, Powf};

#[cfg(all(feature = "libm", not(feature = "std")))]
use num_traits::Float;

mod accept;
mod f32;
mod f64;
mod impls;

typed_floats_macros::generate_floats!();