Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've always wondered why they did the casting rather than a union like:

    float my_rsqrt( float number )
    { 
        float x2;
        union {
          float y;
          long i;
        } u;
        const float threehalfs = 1.5F;
    
        x2 = number * 0.5F;
        u.y = number;
        u.i  = 0x5f3759df - ( u.i >> 1 );               // what the fuck?
        u.y  = u.y * ( threehalfs - ( x2 * u.y * u.y ) );   // 1st iteration
    
        return u.y;
    }
Were unions not supported by the compilers back then?


I believe that's UB.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: