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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Numeric type conversion utilities with safety guarantees.
//!
//! This module provides a set of safe numeric conversion functions that handle
//! edge cases and prevent undefined behavior. The functions are designed for:
//!
//! - Safe conversion between floating-point and integer types
//! - Handling of NaN, infinity, and out-of-range values
//! - Clamping values to valid ranges
//! - Proper rounding of floating-point numbers
//!
//! # Safety
//!
//! All functions in this module guarantee:
//! - No undefined behavior
//! - No panics
//! - Deterministic results for all inputs
//! - Proper handling of edge cases (NaN, infinity, etc.)
//!
//! # Examples
//!
//! ```rust
//! use imx::numeric::{f32_to_i32, i32_to_u32, f32_to_u8};
//!
//! // Safe float to int conversion
//! assert_eq!(f32_to_i32(3.7), 4); // Rounds to nearest
//! assert_eq!(f32_to_i32(f32::NAN), 0); // NaN becomes 0
//!
//! // Safe signed to unsigned conversion
//! assert_eq!(i32_to_u32(-5), 0); // Negative becomes 0
//! assert_eq!(i32_to_u32(42), 42); // Positive passes through
//!
//! // Safe float to byte conversion
//! assert_eq!(f32_to_u8(127.6), 128); // Rounds to nearest
//! assert_eq!(f32_to_u8(300.0), 255); // Clamps to max
//! ```
/// Constants for f32 range that can safely represent integers without precision loss.
/// These values are derived from the fact that f32 has 24 bits of precision,
/// meaning it can exactly represent integers up to 2^24 (16,777,216).
pub const F32_MAX_SAFE_INT: f32 = 16_777_216.0; // 2^24
pub const F32_MIN_SAFE_INT: f32 = -16_777_216.0;
/// Safely converts an f32 to i32 with proper rounding and range clamping.
///
/// This function provides several safety guarantees:
/// - NaN values are converted to 0
/// - Values outside i32's range are clamped to `i32::MIN` or `i32::MAX`
/// - Values are rounded to the nearest integer using banker's rounding
///
/// # Arguments
///
/// * `x` - The f32 value to convert
///
/// # Returns
///
/// Returns the converted i32 value, properly rounded and clamped
///
/// # Examples
///
/// ```rust
/// use imx::numeric::f32_to_i32;
///
/// assert_eq!(f32_to_i32(3.7), 4); // Rounds up
/// assert_eq!(f32_to_i32(3.2), 3); // Rounds down
/// assert_eq!(f32_to_i32(f32::NAN), 0); // NaN becomes 0
/// assert_eq!(f32_to_i32(f32::INFINITY), i32::MAX); // Clamps to max
/// assert_eq!(f32_to_i32(f32::NEG_INFINITY), i32::MIN); // Clamps to min
/// ```
/// Safely converts an i32 to u32, clamping negative values to 0.
///
/// This function guarantees that negative values become 0 while
/// positive values are preserved. This is useful when working with
/// unsigned quantities like array indices or dimensions.
///
/// # Arguments
///
/// * `x` - The i32 value to convert
///
/// # Returns
///
/// Returns the converted u32 value, with negative inputs clamped to 0
///
/// # Examples
///
/// ```rust
/// use imx::numeric::i32_to_u32;
///
/// assert_eq!(i32_to_u32(42), 42); // Positive passes through
/// assert_eq!(i32_to_u32(0), 0); // Zero remains zero
/// assert_eq!(i32_to_u32(-5), 0); // Negative becomes zero
/// ```
/// Safely converts a u32 to i32, clamping values above `i32::MAX`.
///
/// This function handles the case where a u32 value is too large
/// to fit in an i32. Such values are clamped to `i32::MAX`.
///
/// # Arguments
///
/// * `x` - The u32 value to convert
///
/// # Returns
///
/// Returns the converted i32 value, clamped to `i32::MAX` if necessary
///
/// # Examples
///
/// ```rust
/// use imx::numeric::u32_to_i32;
///
/// assert_eq!(u32_to_i32(42), 42); // Small values pass through
/// assert_eq!(u32_to_i32(0), 0); // Zero remains zero
/// assert_eq!(u32_to_i32(3_000_000_000), i32::MAX); // Large values clamp to max
/// ```
/// Safely converts an f32 to u8, with rounding and range clamping.
///
/// This function is particularly useful for color channel conversions
/// where values need to be constrained to the 0-255 range. It provides
/// proper handling of floating point edge cases.
///
/// # Arguments
///
/// * `x` - The f32 value to convert
///
/// # Returns
///
/// Returns the converted u8 value, rounded and clamped to 0..=255
///
/// # Examples
///
/// ```rust
/// use imx::numeric::f32_to_u8;
///
/// assert_eq!(f32_to_u8(127.6), 128); // Rounds to nearest
/// assert_eq!(f32_to_u8(300.0), 255); // Clamps to max
/// assert_eq!(f32_to_u8(-5.0), 0); // Clamps to min
/// assert_eq!(f32_to_u8(f32::NAN), 0); // NaN becomes 0
/// ```
/// Converts an i32 to f32 for text positioning purposes.
///
/// This function is specifically designed for converting screen coordinates
/// to floating point values for text rendering. While it may lose precision
/// for very large values, this is acceptable for text positioning where:
/// - Values are typically small (screen coordinates)
/// - Sub-pixel precision isn't critical
/// - Exact representation isn't required
///
/// # Arguments
///
/// * `x` - The i32 screen coordinate to convert
///
/// # Returns
///
/// Returns the coordinate as an f32 value
///
/// # Examples
///
/// ```rust
/// use imx::numeric::i32_to_f32_for_pos;
///
/// assert_eq!(i32_to_f32_for_pos(42), 42.0); // Exact for small values
/// assert_eq!(i32_to_f32_for_pos(0), 0.0); // Zero remains exact
/// ```
///
/// # Note
///
/// This function is marked as safe for text positioning specifically because:
/// 1. Screen coordinates are typically well within f32's precise range
/// 2. Sub-pixel precision isn't critical for text rendering
/// 3. Any loss of precision won't affect visual quality
/// Converts an f32 to a u32, handling NaN, infinity, and out-of-range values.
///
/// This function provides safe conversion from f32 to u32 with consistent handling
/// of edge cases and proper rounding behavior. Note that due to f32's precision
/// limitations, values very close to `u32::MAX` may be rounded to `u32::MAX`.
///
/// # Examples
///
/// ```
/// use imx::numeric::f32_to_u32;
///
/// assert_eq!(f32_to_u32(0.0), 0);
/// assert_eq!(f32_to_u32(1.4), 1);
/// assert_eq!(f32_to_u32(1.6), 2);
/// assert_eq!(f32_to_u32(-1.0), 0); // Negative values clamp to 0
/// assert_eq!(f32_to_u32(f32::NAN), 0);
/// assert_eq!(f32_to_u32(f32::INFINITY), u32::MAX);
/// ```