Fixed point arithmetic for AVR
This library provides fixed-point arithmetic types for AVR microcontrollers.
The fixed point formats supported are
- Q7.8 format: A 16-bit fixed-point number with 7 integer bits and 8 fractional bits (
Q7p8). - Q15.8 format: A 24-bit fixed-point number with 15 integer bits and 8 fractional bits (
Q15p8).
The supported operations are
- Basic arithmetic operations: addition, subtraction, multiplication, and division.
- Macros for easy construction of fixed-point numbers from integers or fractions.
- Conversions between fixed-point types and integer types.
- Optional
curveipofeature for curve interpolation.
Usage
Add this to your Cargo.toml:
[]
= "1"
Examples
Creating fixed-point numbers
use ;
// From an integer
let a = q7p8!;
assert_eq!;
// From a fraction
let b = q7p8!; // 0.5
let c = q7p8!; // 3.33...
// From variables
let numerator = 10_i16;
let denominator = 3_i16;
let d = q7p8!;
Arithmetic operations
use ;
let a = q7p8!; // 0.5
let b = q7p8!; // 0.25
let sum = a + b;
assert_eq!; // 0.75
let diff = a - b;
assert_eq!; // 0.25
let prod = a * b;
assert_eq!; // 0.125
let quot = a / b;
assert_eq!; // 2.0
let neg = -a;
assert_eq!;
let abs = neg.abs;
assert_eq!;
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.