Skip to content

Instantly share code, notes, and snippets.

@devgru
Created February 21, 2013 17:31
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 devgru/5006529 to your computer and use it in GitHub Desktop.
Save devgru/5006529 to your computer and use it in GitHub Desktop.
Best, fast & exact floor(sqrt()) for Arduino etc.
uint8_t my_sqrt(uint16_t input) {
uint16_t res = 0;
uint16_t one = 1u << 14;
while (one > input) one /= 4;
while (one != 0) {
if (input >= res + one) {
res += one;
input -= res;
res += one;
}
res /= 2;
one /= 4;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment