Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active December 28, 2019 08:30
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 Williammer/6b123e727b3996da22a0a59155d11c3a to your computer and use it in GitHub Desktop.
Save Williammer/6b123e727b3996da22a0a59155d11c3a to your computer and use it in GitHub Desktop.
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)
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