PitchHut logo
fr_math (fixed point embedded math lib)
Simple embedded math lib for motors, audio, dashboards and more. NO FPU or deps except stdint.
Pitch

Need math support on your embedded target but don't have an FPU? Use FR_math - a fixed point library with trig, log, pow, wave-gen, and variable radix support for projects running on low end microcontrollers.

Description

FR_Math: A C Language Fixed-Point Math Library for Embedded Systems

See: Documentation & Guide — for API reference, examples, fixed-point primer, build instructions.

FR_Math is a compact, integer-only fixed-point math library built for systems where floating point is too slow, too big, or unavailable. Designed for embedded targets ranging from legacy 16 MHz 68k processors to modern Cortex-M and RISC-V cores, it provides a full suite of math primitives — trigonometry, logarithms, roots, transforms, and signal generators — while remaining deterministic, portable, and small.

Unlike most fixed-point libraries, FR_Math lets the caller choose the binary point (radix) per operation, trading precision and range explicitly instead of locking into a single format. Pure C with C++ wrappers — compiles cleanly under C99, C11, C17, C++11 and later. Compiles under Arduino, PlatformIO, Espressif, many older embedded targets.
Zero dependencies beyond <stdint.h>.

Note qf_math is the float32 version of this library with the same functions. If you have floating point support on your platform and want a faster, lighter version of libm consider that library.

Measured accuracy

Errors below are measured at Q16.16 (s15.16). All functions accept any radix — Q16.16 is just the reference point for the table.

At other radixes (3-bit, 24-bit, etc.) accuracy will differ due to the number of fractional bits available.

FunctionMax err (%)*Avg err (%)Note
sin/cos (BAM)0.15260.0030very fast binary angle trig
sin/cos (deg)0.15260.0029degree input trig fns
sin/cos (rad)0.18280.0033radian (traditional) trig
tan (BAM)0.58230.0008binary angle tangent; ±maxint at poles
tan (deg)0.53110.0008degree input tangent; saturated at poles
tan (rad)0.03860.0001radian (traditional) tangent
asin / acos0.77710.0280reverse trig, radian output
atan20.25640.0237reverse tangent, always safe
atan0.24250.0155reverse tangent, accepts up to maxint
sqrt0.00000.0000Round-to-nearest
log20.01160.0016shift/add only for speed
pow20.00180.0004shift/add only for speed
ln, log100.00040.0000shift/add only for speed
exp0.00030.0000shift/add only for speed
exp_fast0.00090.0001Shift-only scaling
pow100.00050.0000shift/add only for speed
pow10_fast0.00220.0002Shift-only scaling
hypot (exact)0.00000.0000Uses 64-bit intermediate
hypot_fast8 (8-seg)0.09150.0320Shift-only, no multiply

*Relative error; reference clamped to 1% of full-scale output.

What's in the box

AreaFunctions
ArithmeticFR_ADD, FR_SUB, FR_DIV, FR_DIV32, FR_MOD, FR_FixMuls, FR_FixMulSat, FR_CHRDX
UtilityFR_MIN, FR_MAX, FR_CLAMP, FR_ABS, FR_SGN
Trig (degree)fr_sin_deg, fr_cos_deg, fr_tan_deg, FR_SinI, FR_CosI, FR_TanI
Trig (radian/BAM)fr_sin, fr_cos, fr_tan, fr_sin_bam, fr_cos_bam, fr_tan_bam
Inverse trigFR_atan, FR_atan2, FR_asin, FR_acos
Log / expFR_log2, FR_ln, FR_log10, FR_pow2, FR_EXP, FR_POW10, FR_EXP_FAST, FR_POW10_FAST, FR_MULK28
RootsFR_sqrt, FR_hypot, FR_hypot_fast8
Wave generatorsfr_wave_sqr, fr_wave_pwm, fr_wave_tri, fr_wave_saw, fr_wave_tri_morph, fr_wave_noise
Envelopefr_adsr_init, fr_adsr_trigger, fr_adsr_release, fr_adsr_step
2D transformsFR_Matrix2D_CPT (mul, add, sub, det, inv, setrotate, XFormPtI, XFormPtI16)
Formatted outputFR_printNumD, FR_printNumF, FR_printNumH, FR_numstr

Compiled library size (FR_math.c only, -Os)

.text section sizes, all code + internal tables, ROMable. Sorted 8-bit → 64-bit.

TargetLeanCoreFull
AVR ATmega328P (8-bit)9.2 KB12.8 KB15.4 KB
68HC11 (8-bit)13.3 KB18.4 KB22.6 KB
MSP430 (16-bit)7.8 KB10.7 KB12.8 KB
Xtensa LX7 (ESP32-S3)2.9 KB4.2 KB5.3 KB
Cortex-M4 (32-bit)3.3 KB4.4 KB5.5 KB
Cortex-M0 (32-bit)3.4 KB4.5 KB5.7 KB
RISC-V rv324.1 KB5.5 KB6.8 KB
Xtensa LX106 (ESP8266)4.2 KB5.8 KB7.3 KB
m68k (32-bit)4.4 KB6.2 KB7.8 KB
MIPS324.7 KB6.6 KB8.7 KB
x86-325.3 KB7.2 KB9.2 KB
RISC-V rv644.0 KB5.5 KB6.8 KB
x86-64 (GCC)4.6 KB6.1 KB8.0 KB
AArch64 (ARM64)4.8 KB6.6 KB8.7 KB
0 comments

No comments yet.

Sign in to be the first to comment.