Last active
October 22, 2016 14:42
-
-
Save Cloudef/6fb2fd5d950df8be9e30 to your computer and use it in GitHub Desktop.
Find shortest rotation without branches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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