Expand description
§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):
32767represents+1.00represents0.0-32768represents approximately-1.0
To convert to a human-readable float:
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 radians16384≈ π/232768≈ π65535≈ 2π
§Quick Start
Add to your Cargo.toml:
[dependencies]
fixed-math-taylor = { version = "0.3", features = ["lut"] }Basic usage:
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.
Functions§
- from_
fixed - Converts a Q15 fixed-point value to a float.
- radians_
to_ angle - Converts an angle in radians to the
Anglerepresentation used by this library. - to_
fixed - Converts a float value to Q15 fixed-point format.