Implement Fast Inverse Square Root in Javascript?

Posted by BBz on Game Development See other posts from Game Development or by BBz
Published on 2012-06-17T04:29:56Z Indexed on 2012/06/17 9:23 UTC
Read the original article Hit count: 245

Filed under:

The Fast Inverse Square Root from Quake III seems to use a floating-point trick. As I understand, floating-point representation can have some different implementations.

So is it possible to implement the Fast Inverse Square Root in Javascript?

Would it return the same result?

float Q_rsqrt(float number) {

  long i;
  float x2, y;
  const float threehalfs = 1.5F;

  x2 = number * 0.5F;
  y = number;
  i = * ( long * ) &y;
  i = 0x5f3759df - ( i >> 1 );
  y = * ( float * ) &i;
  y = y * ( threehalfs - ( x2 * y * y ) );

  return y;

}

© Game Development or respective owner

Related posts about JavaScript