Created
February 21, 2013 17:31
-
-
Save devgru/5006529 to your computer and use it in GitHub Desktop.
Best, fast & exact floor(sqrt()) for Arduino etc.
This file contains hidden or 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
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