Skip to content

Instantly share code, notes, and snippets.

@tonekk
Created October 16, 2014 06:36
Show Gist options
  • Save tonekk/6107fdf3cd56edb89b19 to your computer and use it in GitHub Desktop.
Save tonekk/6107fdf3cd56edb89b19 to your computer and use it in GitHub Desktop.
fix_point.h, Beuth
#ifndef FIX_POINT_H
#define FIX_POINT_H
#include <cmath>
/*
* fix_point.h
*
* Finn-Lennart Heemeyer, 791440.Beuth
*/
/*
* define structure
*/
struct fix_point {
float value;
};
/*
* pseudo constructor
* (classes propably lat0r this semester)
*/
fix_point to_fix_point(float fl) {
fix_point fp;
fp.value = fl;
return fp;
}
/*
* conversion
*/
float to_float(fix_point fp) {
return fp.value;
}
int to_int(fix_point fp) {
return (int) fp.value;
}
/*
* logical functions
*/
bool equals(fix_point fp1, fix_point fp2) {
return fp1.value == fp2.value;
}
bool less_than(fix_point fp1, fix_point fp2) {
return fp1.value < fp2.value;
}
bool less_or_equal_than(fix_point fp1, fix_point fp2) {
return fp1.value <= fp2.value;
}
bool more_than(fix_point fp1, fix_point fp2) {
return fp1.value > fp2.value;
}
bool more_or_equal_than(fix_point fp1, fix_point fp2) {
return fp1.value >= fp2.value;
}
/*
* arithmetic functions
*/
fix_point add(fix_point fp1, fix_point fp2) {
fix_point fp;
fp.value = fp1.value + fp2.value;
return fp;
}
fix_point sub(fix_point fp1, fix_point fp2) {
fix_point fp;
fp.value = fp1.value - fp2.value;
return fp;
}
fix_point mul(fix_point fp1, fix_point fp2) {
fix_point fp;
fp.value = fp1.value * fp2.value;
return fp;
}
fix_point div(fix_point fp1, fix_point fp2) {
fix_point fp;
fp.value = fp1.value / fp2.value;
return fp;
}
/*
* floor & frac
*/
float floor(fix_point fp) {
return std::floor(fp.value);
}
float frac(fix_point fp) {
return fp.value - (int) fp.value;
}
/*
* I hope you had some fun reading this code, see you on github:
*
* github.com/tonekk
*/
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment