CSDN博客

img Nhsoft

各种快速SQRT

发表于2004/10/21 18:30:00  2247人阅读

float mysqrt(float x)
{
        union
       {
                    int intPart;
                          float floatPart;
         } convertor;
          union
         {
                        int intPart;
                             float floatPart;
          } convertor2;

          convertor.floatPart = x;
          convertor2.floatPart = x;
          convertor.intPart = 0x1FBCF800 + (convertor.intPart >> 1);
          convertor2.intPart = 0x5f3759df - (convertor2.intPart >> 1);
          return 0.5f*(convertor.floatPart + (x * convertor2.floatPart));
}

float InvSqrt_Lomont(float x)

         float xhalf = 0.5f*x;
         int i = *(int*)&x;
         i = 0x5f375a86 - (i>>1);
         x = *(float*)&i;
         x = x*(1.5f-xhalf*x*x);
         // x = x*(1.5f-xhalf*x*x); // added precision
         return x;
} // InvSqrt_Lomont

float InvSqrt (float x)
{
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i >> 1);
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}

阅读全文
0 0

相关文章推荐

img
取 消
img