Skip to content

Instantly share code, notes, and snippets.

@oblaser
Last active February 28, 2024 11:21
Show Gist options
  • Save oblaser/9ec852abcd598291a57e7614194c6d59 to your computer and use it in GitHub Desktop.
Save oblaser/9ec852abcd598291a57e7614194c6d59 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// rounds a float or double away from zero, casting to an integer type has to be applied
#define UTIL_ROUND(v) ((v) < 0 ? ((v) - 0.5f) : ((v) + 0.5f))
int main()
{
for (float f = -3.1f; f < 3.1f; f += 0.1f)
{
cout << f << " => " << (int)UTIL_ROUND(f) << endl;
}
}
/* output:
-3.1 => -3
-3 => -3
-2.9 => -3
-2.8 => -3
-2.7 => -3
-2.6 => -3
-2.5 => -3
-2.4 => -2
-2.3 => -2
-2.2 => -2
-2.1 => -2
-2 => -2
-1.9 => -2
-1.8 => -2
-1.7 => -2
-1.6 => -2
-1.5 => -2
-1.4 => -1
-1.3 => -1
-1.2 => -1
-1.1 => -1
-1 => -1
-0.900001 => -1
-0.800001 => -1
-0.700001 => -1
-0.600001 => -1
-0.500001 => -1
-0.400001 => 0
-0.300001 => 0
-0.200001 => 0
-0.100001 => 0
-6.10948e-07 => 0
0.0999994 => 0
0.199999 => 0
0.299999 => 0
0.399999 => 0
0.499999 => 0
0.599999 => 1
0.699999 => 1
0.799999 => 1
0.899999 => 1
0.999999 => 1
1.1 => 1
1.2 => 1
1.3 => 1
1.4 => 1
1.5 => 1
1.6 => 2
1.7 => 2
1.8 => 2
1.9 => 2
2 => 2
2.1 => 2
2.2 => 2
2.3 => 2
2.4 => 2
2.5 => 2
2.6 => 3
2.7 => 3
2.8 => 3
2.9 => 3
3 => 3
3.1 => 3
*/
@LosXLucifer
Copy link

useful

@LosXLucifer
Copy link

Thanks Bro

@LosXLucifer
Copy link

ff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment