Skip to content

Instantly share code, notes, and snippets.

@cousteaulecommandant
Last active June 5, 2017 10:54
Show Gist options
  • Save cousteaulecommandant/5cffbe9fdd66beca244bf9b693f7e418 to your computer and use it in GitHub Desktop.
Save cousteaulecommandant/5cffbe9fdd66beca244bf9b693f7e418 to your computer and use it in GitHub Desktop.
#define OPTION 1 // 1, 2, 3, 4, 5
typedef long res_t; // long, float
#if OPTION == 1
// use Arduino.h's `round`
#elif OPTION == 2
// use template function returning long
template<typename T> long Round(T x) { return x>=0 ? x+0.5 : x-0.5; }
#undef round
#define round(x) Round(x)
#elif OPTION == 3
// use template function returning T
template<typename T> T Round(T x) { return x>=0 ? (long)(x+0.5) : (long)(x-0.5); }
#undef round
#define round(x) Round(x)
#elif OPTION == 4
// use math.h round
#undef round
#elif OPTION == 5
// use math.h lround
#undef round
#define round(x) lround(x)
#endif
void setup() {
Serial.begin(9600);
float x = Serial.read();
res_t y = round(x/2);
Serial.println(y);
}
void loop() { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment