Skip to content

Instantly share code, notes, and snippets.

@victorfeitosa
Last active May 9, 2024 21:56
Show Gist options
  • Save victorfeitosa/61ed113fdd0cd24f79b596f9def100de to your computer and use it in GitHub Desktop.
Save victorfeitosa/61ed113fdd0cd24f79b596f9def100de to your computer and use it in GitHub Desktop.
Float point numbers to Fixed number representation
#include <stdio.h>
typedef int Fixed;
#define FRACT_BITS 16
#define FRACT_BITS_D2 8
#define FIXED_ONE (1 << FRACT_BITS)
#define INT2FIXED(x) ((x) << FRACT_BITS)
#define FLOAT2FIXED(x) ((int)((x) * (1 << FRACT_BITS)))
#define FIXED2INT(x) ((x) >> FRACT_BITS)
#define FIXED2DOUBLE(x) (((double)(x)) / (1 << FRACT_BITS))
#define MULT(x, y) ( ((x) >> FRACT_BITS_D2) * ((y)>> FRACT_BITS_D2) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment