Skip to content

Schwungus/S_fixed

Repository files navigation

S_fixed

A header-only 16.16 fixed-point math library for C.

Usage

In your main.c file, compile this library's function definitions by writing:

#define FIX_IMPLEMENTATION
#include <S_fixed.h>

int main(int argc, char* argv[]) {
    // your code goes here...
}

Everywhere else, include the header normally to add the library's functions and constants to scope:

#pragma once

#include <S_fixed.h>

int my_epic_function();
// and so on...

This is pretty much how every "header-only" library works.

Notes

The type you should primarily use with this library is fix16_t. fix32_t is only used internally for multiplication and division.

The trigonometry functions were based on libfixmath's implementations and simplified for this library.

Functions

Function Description
FxFrom(x) Generic number-to-fixed-point conversion
Int2Fx(x) Integer to fixed point
Float2Fx(x) Float to fixed point1
Double2Fx(x) Double to fixed point1
Fx2Int(x) Fixed point to integer
Fx2Float(x) Fixed point to float
Fx2Double(x) Fixed point to double
Fadd(a, b) Add +2
Fsub(a, b) Subtract -2
Fmul(a, b) Multiply *2
Fdiv(a, b) Divide /23
Fmod(a, b) Remainder (modulo) %3
Fhalf(x) Halve x / 22
Fdouble(x) Double x * 22
Ffrac(x) Fractional part
Ffloor(x) Round down
Fceil(x) Round up2
Fabs(x) Absolute value
Fmin(a, b) Minimum of a and b
Fmax(a, b) Maximum of a and b
Fclamp(x, a, b) Clamp x between a and b
Flerp(a, b, x) Linear interpolation2
Fdeg(x) Radians to degrees2
Frad(x) Degrees to radians2
Fsqr(x) Square2
Fsqrt(x) Square root3
Fcube(x) Cube2
Fsin(x) Sine
Fcos(x) Cosine
Ftan(x) Tangent23
Fasin(x) Arcsine3
Facos(x) Arccosine3
Fatan(x) Arctangent23
Fatan2(y, x) Arctangent of y / x with benefits23

Constants

Constant Description
FxOne Fixed point representation of 1
FxHalf Fixed point representation of 0.5
FxZero Fixed point representation of 0
FxPi Fixed point representation of pi (16 decimals)
FxPi4 Fixed point representation of pi / 4 (16 decimals)
FxPi2 Fixed point representation of pi / 2 (16 decimals)
Fx2Pi Fixed point representation of 2 * pi (16 decimals)
Fx3Pi4 Fixed point representation of (3 * pi) / 4 (16 decimals)

Advanced usage

You can define FIX_NOSTD if you're using e.g. SDL3 which includes/defines the integer types from stdint.h for you:

#include <SDL3/SDL_stdinc.h>

#define FIX_NOSTD
#define FIX_IMPLEMENTATION
#include <S_fixed.h>

Footnotes

  1. You should probably use this with common sense. Floats and fixed point don't go well together.
    For paranoid people, it's more reliable to generate constant fixed point numbers from floats by typing x * 65536 in a calculator and storing a truncated version of the result with a fix16_t cast. 2

  2. Overflow is possible. 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  3. Quietly returns 0 on failure. 2 3 4 5 6 7 8

About

Header-only 16.16 fixed point math library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors 2

  •  
  •