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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (C) 2026 Jorge Andre Castro
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 or the License, or
// (at your option) any later version.
//! # Fixed-Math-Taylor
//!
//! A high-performance fixed-point trigonometry library optimized for embedded systems and microcontrollers
//! without a floating-point unit (FPU).
//!
//! ## Overview
//!
//! Fixed-Math-Taylor eliminates the overhead of software floating-point emulation on resource-constrained
//! devices by implementing trigonometric functions using only integer arithmetic and bit shifts. Designed
//! specifically for the RP2040 and similar microcontrollers, it provides multiple calculation engines
//! with different precision/speed trade-offs.
//!
//! ## Features & Calculation Engines
//!
//! The library provides three distinct calculation engines, selectable via Cargo features:
//!
//! - **`lut`** (default): Lookup table with linear interpolation. Ultra-fast, highest precision (~0.1% error),
//! 640 bytes Flash. Ideal for control loops, motor control, and audio applications.
//! - **`taylor`**: Taylor series expansion (order 5). Pure algorithmic computation with high precision.
//! Smaller memory footprint, better for algorithms-only use cases.
//! - **`fast-sin`**: Bhaskara I approximation. Medium precision with exceptional speed. Optimal for
//! animations and graphics.
//!
//! ## Number Format: Q15 Fixed-Point
//!
//! All functions return results in **Q15 format** (16-bit signed integer):
//! - `32767` represents `+1.0`
//! - `0` represents `0.0`
//! - `-32768` represents approximately `-1.0`
//!
//! To convert to a human-readable float:
//! ```rust,ignore
//! let fixed_value: i16 = /* ... */;
//! let float_value: f32 = (fixed_value as f32) / 32767.0;
//! ```
//!
//! ## Angle Representation
//!
//! Angles are represented as `u16` values (0..=65535):
//! - `0` = 0 radians
//! - `16384` ≈ π/2
//! - `32768` ≈ π
//! - `65535` ≈ 2π
//!
//! ## Quick Start
//!
//! Add to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! fixed-math-taylor = { version = "0.3", features = ["lut"] }
//! ```
//!
//! Basic usage:
//!
//! ```rust,ignore
//! use fixed_math_taylor::{sin_fixed, cos_fixed, Angle, Fixed};
//!
//! let angle: Angle = 16384; // π/2
//! let sine_value: Fixed = sin_fixed(angle);
//! let cosine_value: Fixed = cos_fixed(angle);
//!
//! // Convert to float for display
//! println!("sin(π/2) ≈ {}", (sine_value as f32) / 32767.0);
//! ```
//!
//! ## Performance Characteristics
//!
//! | Engine | Speed | Precision | Flash Cost | Best For |
//! |-----------|---------------|------------|------------|-----------------------------|
//! | LUT | Fastest | ~0.1% err | 640 bytes | Control loops, motor ctrl |
//! | Taylor | Fast | High | ~500 bytes | Pure algorithms |
//! | Fast-Sin | Fastest | Medium | ~300 bytes | Graphics, animations |
//!
//! ## no_std Environment
//!
//! This crate is `#![no_std]` compatible and suitable for embedded environments with no standard library.
//! No dynamic allocations are performed.
//!
//! ## Dependencies
//!
//! Zero external dependencies. Pure Rust with platform-independent implementation.
// --- TYPES DE BASE ---
/// An angle in the range [0, 65535], representing a full rotation [0, 2π).
///
/// The mapping is:
/// - `0` → 0 radians
/// - `16384` → π/2
/// - `32768` → π
/// - `49152` → 3π/2
/// - `65535` → ~2π
///
/// This representation allows fast angle arithmetic using only bit shifts
/// and avoids floating-point operations entirely.
pub type Angle = u16;
/// A fixed-point number in Q15 format, suitable for storing results from trigonometric functions.
///
/// Q15 maps the range [-32768, 32767] to the mathematical range [-1.0, 1.0):
/// - `32767` represents `+1.0`
/// - `0` represents `0.0`
/// - `-32768` represents approximately `-1.0`
///
/// To convert to a float: `(fixed_value as f32) / 32767.0`
///
/// This format is native to 16-bit integer arithmetic and matches the output
/// of all trigonometric functions in this library.
pub type Fixed = i16;
// ==========================================
// MOTEUR LUT (FEATURE "lut")
// ==========================================
// Ré-exportation et fonctions publiques liées à la LUT
/// Computes the sine of an angle using the lookup table engine.
///
/// This function provides the fastest and most memory-efficient trigonometric
/// computation with high accuracy (~0.1% error).
///
/// # Input
/// - `angle`: An [`Angle`] value in the range [0, 65535] representing [0, 2π).
///
/// # Output
/// A [`Fixed`] value in Q15 format, where 32767 ≈ 1.0 and -32768 ≈ -1.0.
///
/// # Algorithm
/// Uses a 257-entry lookup table with linear interpolation. Exploits trigonometric
/// symmetry to store only the first quadrant while computing all four quadrants.
///
/// # Example
/// ```ignore
/// use fixed_math_taylor::sin_fixed;
///
/// // π/2 in angle representation
/// let angle = 16384u16;
/// let result = sin_fixed(angle);
/// // result ≈ 32767 (representing 1.0)
/// ```
///
/// # Performance
/// ~2.4 µs on RP2040 at 125 MHz
pub use sin_fixed;
/// Computes the cosine of an angle using the lookup table engine.
///
/// Equivalent to `sin(angle + π/2)`. Provides the same performance and accuracy
/// as [`sin_fixed`].
///
/// # Input
/// - `angle`: An [`Angle`] value in the range [0, 65535] representing [0, 2π).
///
/// # Output
/// A [`Fixed`] value in Q15 format, where 32767 ≈ 1.0 and -32768 ≈ -1.0.
///
/// # Example
/// ```ignore
/// use fixed_math_taylor::cos_fixed;
///
/// // π in angle representation
/// let angle = 32768u16;
/// let result = cos_fixed(angle);
/// // result ≈ -32768 (representing -1.0)
/// ```
///
/// # Performance
/// ~2.4 µs on RP2040 at 125 MHz
/// Computes both sine and cosine of an angle simultaneously.
///
/// More efficient than calling [`sin_fixed`] and [`cos_fixed`] separately,
/// as some intermediate calculations are shared.
///
/// # Input
/// - `angle`: An [`Angle`] value in the range [0, 65535] representing [0, 2π).
///
/// # Output
/// A tuple `(sin_value, cos_value)`, both in Q15 [`Fixed`] format.
///
/// # Example
/// ```ignore
/// use fixed_math_taylor::sin_cos;
///
/// let angle = 16384u16;
/// let (sin_val, cos_val) = sin_cos(angle);
/// ```
///
/// # Performance
/// ~3.5 µs on RP2040 at 125 MHz (faster than two separate calls)
// ==========================================
// MOTEUR TAYLOR (Q15 - 100% Entiers)
// ==========================================
/// Pure algorithmic sine and cosine using 5th-order Taylor series expansion.
///
/// This module provides trigonometric functions without requiring a lookup table,
/// making it ideal for scenarios where Flash memory is constrained or where
/// purely algorithmic computation is preferred.
///
/// Both functions use 100% integer arithmetic (Q15 fixed-point), with intermediate
/// 64-bit calculations to prevent overflow.
// ==========================================
// MOTEUR FAST (Bhaskara I Q15)
// ==========================================
/// Ultra-fast approximation using Bhaskara I formula.
///
/// This module provides the speediest trigonometric computation at the cost
/// of slightly lower precision. Ideal for graphics, animations, and scenarios
/// where speed is critical.
///
/// Uses the Bhaskara I approximation formula in pure Q15 integer arithmetic.
// ==========================================
// UTILITAIRES COMMUNS
// ==========================================
/// Converts a float value to Q15 fixed-point format.
///
/// # Input
/// - `x`: A floating-point value in the range [-1.0, 1.0]
///
/// # Output
/// A [`Fixed`] value representing the input in Q15 format.
///
/// # Example
/// ```ignore
/// use fixed_math_taylor::to_fixed;
///
/// let f = to_fixed(0.5);
/// assert_eq!(f, 16384); // 0.5 * 32767 ≈ 16384
/// ```
/// Converts a Q15 fixed-point value to a float.
///
/// # Input
/// - `x`: A [`Fixed`] value in Q15 format
///
/// # Output
/// A floating-point value representing the input (approximately in [-1.0, 1.0])
///
/// # Example
/// ```ignore
/// use fixed_math_taylor::from_fixed;
///
/// let f = from_fixed(32767);
/// assert!((f - 1.0).abs() < 0.001); // Close to 1.0
/// ```
/// Converts an angle in radians to the [`Angle`] representation used by this library.
///
/// # Input
/// - `rads`: An angle in radians. Values outside [0, 2π) wrap around automatically.
///
/// # Output
/// An [`Angle`] value suitable for the trigonometric functions.
///
/// # Example
/// ```ignore
/// use fixed_math_taylor::radians_to_angle;
/// use core::f32::consts::PI;
///
/// let angle = radians_to_angle(PI / 2.0);
/// assert_eq!(angle, 16384); // π/2
/// ```
// ==========================================
// TESTS UNITAIRES
// ==========================================