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
//! Real number-specific operations and traits.
//!
//! This module provides traits for operations specific to real numbers, including
//! clamping, classification, rounding, sign manipulation, and total ordering.
use ;
/// Trait for clamping a value between minimum and maximum bounds.
///
/// This trait provides functionality to restrict a value to lie within a specified range.
/// If the value is less than the minimum, it returns the minimum. If greater than the
/// maximum, it returns the maximum. Otherwise, it returns the value unchanged.
///
/// # Examples
///
/// ```
/// use num_valid::functions::Clamp;
///
/// let result = Clamp::clamp_ref(5.0f64, &0.0, &10.0);
/// assert_eq!(result, 5.0);
///
/// let result = Clamp::clamp_ref(-5.0f64, &0.0, &10.0);
/// assert_eq!(result, 0.0);
///
/// let result = Clamp::clamp_ref(15.0f64, &0.0, &10.0);
/// assert_eq!(result, 10.0);
/// ```
/// Trait for classifying floating-point values into categories.
///
/// This trait provides functionality to determine the category of a floating-point number
/// according to the IEEE 754 standard (normal, subnormal, zero, infinite, or NaN).
///
/// # Examples
///
/// ```
/// use num_valid::functions::Classify;
/// use std::num::FpCategory;
///
/// let normal = 42.0f64;
/// assert_eq!(Classify::classify(&normal), FpCategory::Normal);
///
/// let infinity = f64::INFINITY;
/// assert_eq!(Classify::classify(&infinity), FpCategory::Infinite);
///
/// let zero = 0.0f64;
/// assert_eq!(Classify::classify(&zero), FpCategory::Zero);
/// ```
/// Trait for computing `exp(x) - 1` with high precision for small values.
///
/// This trait provides the `exp_m1` function, which computes `e^x - 1` more accurately
/// than `exp(x) - 1` when `x` is close to zero. This avoids catastrophic cancellation
/// that occurs when subtracting two nearly equal numbers.
///
/// # Mathematical Background
///
/// For small values of `x`, computing `exp(x) - 1` directly can lose precision because
/// `exp(x)` is close to 1. The `exp_m1` function uses alternative algorithms (such as
/// Taylor series) that maintain accuracy in this regime.
///
/// # Examples
///
/// ```
/// use num_valid::functions::ExpM1;
///
/// let x = 1e-10f64;
/// let result = ExpM1::exp_m1(x);
/// // More accurate than: x.exp() - 1.0
/// assert!((result - x).abs() < 1e-15);
/// ```
/// Trait for computing the Euclidean distance (hypotenuse) between two values.
///
/// This trait provides the `hypot` function, which computes `sqrt(x² + y²)` in a way
/// that avoids overflow and underflow for intermediate calculations. This is the length
/// of the hypotenuse of a right triangle with sides of length `|x|` and `|y|`.
///
/// # Mathematical Background
///
/// Computing `sqrt(x² + y²)` directly can overflow if `x` or `y` are very large,
/// or underflow if they are very small. The `hypot` function uses scaling techniques
/// to avoid these issues while maintaining numerical accuracy.
///
/// # Examples
///
/// ```
/// use num_valid::functions::Hypot;
///
/// let x = 3.0f64;
/// let y = 4.0f64;
/// let distance = Hypot::hypot(x, &y);
/// assert_eq!(distance, 5.0);
/// ```
/// Trait for computing `ln(1 + x)` with high precision for small values.
///
/// This trait provides the `ln_1p` function, which computes `ln(1 + x)` more accurately
/// than `ln(1.0 + x)` when `x` is close to zero. This avoids precision loss from adding
/// a small number to 1 and then taking the logarithm.
///
/// # Mathematical Background
///
/// For small values of `x`, computing `ln(1 + x)` directly loses precision because
/// `1 + x` rounds to 1 in floating-point arithmetic. The `ln_1p` function uses
/// alternative algorithms (such as Taylor series) that maintain accuracy for small `x`.
///
/// # Examples
///
/// ```
/// use num_valid::functions::Ln1p;
///
/// let x = 1e-10f64;
/// let result = Ln1p::ln_1p(x);
/// // More accurate than: (1.0 + x).ln()
/// assert!((result - x).abs() < 1e-15);
/// ```
/// Trait for total ordering comparison of floating-point values.
///
/// This trait provides a total ordering for floating-point numbers, including special
/// values like NaN and signed zeros. Unlike the standard `PartialOrd` trait, this
/// comparison always returns a definite ordering.
///
/// # Ordering Rules
///
/// The total ordering is defined as:
/// - Negative NaN < negative infinity < negative numbers < -0.0 < +0.0 < positive numbers < positive infinity < positive NaN
/// - NaN values are ordered by their bit representation
/// - -0.0 is ordered as less than +0.0
///
/// This matches the IEEE 754-2008 `totalOrder` predicate.
///
/// # Examples
///
/// ```
/// use num_valid::functions::TotalCmp;
/// use std::cmp::Ordering;
///
/// let a = -0.0f64;
/// let b = 0.0f64;
/// assert_eq!(TotalCmp::total_cmp(&a, &b), Ordering::Less);
///
/// let nan = f64::NAN;
/// let inf = f64::INFINITY;
/// assert_eq!(TotalCmp::total_cmp(&inf, &nan), Ordering::Less);
/// ```