Skip to content

Instantly share code, notes, and snippets.

@spiridenok
Created July 29, 2013 10:53
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 spiridenok/6103570 to your computer and use it in GitHub Desktop.
Save spiridenok/6103570 to your computer and use it in GitHub Desktop.
Fast SQRT
static double fast_sqrt( const double number )
{
const double f = 1.5;
const double x = number * 0.5;
double y = number;
union
{
double *double_repr;
long long *long_long_repr;
} sqrt_union;
sqrt_union.double_repr = &y;
long long i = *(sqrt_union.long_long_repr);
// for floats use:
// i = 0x5f3759df - (i >> 1);
i = 0x5fe6eb50c7b537a9LL - (i >> 1);
sqrt_union.long_long_repr = &i;
y = *(sqrt_union.double_repr);
y = y * (f - (x * y * y));
y = y * (f - (x * y * y));
return (number * y);
//0x5fe6eb50c7b537a9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment