Skip to content

Instantly share code, notes, and snippets.

@slavaceornea
Last active June 12, 2016 15:34
Show Gist options
  • Save slavaceornea/5a32846f2141c003ee5e3a602251ff54 to your computer and use it in GitHub Desktop.
Save slavaceornea/5a32846f2141c003ee5e3a602251ff54 to your computer and use it in GitHub Desktop.
Java function calculates square root using binary search.
float sqrtBinarySearch(n) {
low = 0.0;
high = (float)n+1;
while ((high-low) > 0.00001) {
mid = (low+high) / 2;
if (mid*mid < n) {
low = mid;
}
else {
high = mid;
}
}
return low;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment