Skip to content

Instantly share code, notes, and snippets.

@Cloudef
Last active October 22, 2016 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cloudef/6fb2fd5d950df8be9e30 to your computer and use it in GitHub Desktop.
Save Cloudef/6fb2fd5d950df8be9e30 to your computer and use it in GitHub Desktop.
Find shortest rotation without branches.
#include <stdint.h>
// you can use any precision you want
typedef uint32_t ubams;
typedef int32_t bams;
static const uint32_t erro = (sizeof(ubams) < sizeof(uint32_t));
static const uint32_t most = (ubams)~0;
static inline ubams
tobams(double x)
{
return (x/360.0) * (most + erro);
}
static inline double
todegs(bams x)
{
return (x/(double)(most + erro)) * 360.0;
}
static inline double
find_rotation(double a, double b)
{
return todegs(tobams(b) - tobams(a));
}
int
main(void)
{
for (uint32_t i = 0; i <= 360; i += 10)
printf("from %u to 350 == %.2f\n", i, find_rotation(i, 350));
return 0;
}
#include <stdint.h>
#include <math.h>
static inline double
modn(double a, double b)
{
return a - b * round(a / b);
}
static inline double
find_rotation(double a, double b)
{
return modn(b - a, 360);
}
int
main(void)
{
for (uint32_t i = 0; i <= 360; i += 10)
printf("from %u to 350 == %.2f\n", i, find_rotation(i, 350));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment