Last active
June 12, 2016 15:34
-
-
Save slavaceornea/5a32846f2141c003ee5e3a602251ff54 to your computer and use it in GitHub Desktop.
Java function calculates square root using binary search.
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
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