Skip to main content

Crate fixed_math_taylor

Crate fixed_math_taylor 

Source
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):

  • 32767 represents +1.0
  • 0 represents 0.0
  • -32768 represents 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 radians
  • 16384 ≈ π/2
  • 32768 ≈ π
  • 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

EngineSpeedPrecisionFlash CostBest For
LUTFastest~0.1% err640 bytesControl loops, motor ctrl
TaylorFastHigh~500 bytesPure algorithms
Fast-SinFastestMedium~300 bytesGraphics, 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 Angle representation used by this library.
to_fixed
Converts a float value to Q15 fixed-point format.

Type Aliases§

Angle
An angle in the range [0, 65535], representing a full rotation [0, 2π).
Fixed
A fixed-point number in Q15 format, suitable for storing results from trigonometric functions.