Last active
December 28, 2019 08:30
-
-
Save Williammer/6b123e727b3996da22a0a59155d11c3a to your computer and use it in GitHub Desktop.
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
def sqrt_binary(num): | |
if num > 1: | |
low = 1 | |
high = num | |
else : | |
low = num | |
high = 1 | |
mid = float(low) + float((high - low) / 2) | |
while abs(mid ** 2 - num) > 0.000001: | |
if mid ** 2 < num: | |
low = mid | |
else : | |
high = mid | |
mid = (low + high) / 2 | |
return round(mid, 6) |
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 q_rsqrt(float number) { | |
int i; | |
float x2, y; | |
const float threehalfs = 1.5; | |
x2 = number * 0.5; | |
y = number; | |
i = * (int * ) & y; | |
i = 0x5f3759df - (i >> 1); | |
y = * (float * ) & i; | |
y = y * (threehalfs - (x2 * y * y)); | |
y = y * (threehalfs - (x2 * y * y)); | |
y = y * (threehalfs - (x2 * y * y)); | |
return 1.0 / y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment