Fast inverse square root

float InvSqrt(float x)
{
    float xhalf = 0.5f*x;
    int i = *(int*)&x;          // get bits for floating value
    i = 0x5f3759df - (i>>1);    // gives initial guess y0
    x = *(float*)&i;            // convert bits back to float
    x = x*(1.5f-xhalf*x*x);     // Newton step, repeating increases accuracy
    return x;
}

Chris Lomont

posted @ 2017-08-30 17:00  fndefbwefsowpvqfx  阅读(482)  评论(0编辑  收藏  举报