惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
U
Unit 42
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
Recent Announcements
Recent Announcements
B
Blog RSS Feed
罗磊的独立博客
博客园 - Franky
J
Java Code Geeks
S
SegmentFault 最新的问题
D
DataBreaches.Net
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
腾讯CDC
博客园_首页
美团技术团队
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏

博客园 - gxc

C#2.0中的泛型约束(转载) 解决‘“System.Configuration.ConfigurationSettings.AppSettings”已过时’的警告 回溯法(vc)百鸡百钱问题 回溯法(vc)八皇后问题 六十六条经典禅语 prototype.js和Ajax 悖论 标签的使用(2) 标签的使用(1) 自底向上的归并排序 自顶向下的归并排序 归并排序之归并算法 Josephus问题(循环链表) 找质数算法(Sieve of Eratosthenes筛法) 堆排序 直接选择排序 快速排序算法 Some of the new features from ASP.NET 2.0 在ASP.NET中使用AJAX
《雷神之锤III》里求平方根倒数的函数
gxc · 2007-02-06 · via 博客园 - gxc

float Q_rsqrt( float number )
{
  
long i;
  
float x2, y;
  
const float threehalfs = 1.5F;

  x2 
= number * 0.5F;
  y  
= number;
  i  
= * ( long * ) &y;  // evil floating point bit level hacking
  i  = 0x5f3759df - ( i >> 1 ); // what the fuck?
  y  = * ( float * ) &i;
  y  
= y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  
// y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

  #ifndef Q3_VM
  #ifdef __linux__
    assert( 
!isnan(y) ); // bk010122 - FPE?
  #endif
  
#endif
  
return y;
}

对于 double ,如果在单片机环境可以试试可以使用如下代码

double InvSqrt(double number)
{
    __int64 i;
    
double x2, y;
    
const double threehalfs = 1.5F;

    x2 
= number * 0.5F;
    y  
= number;
    i  
= * ( __int64 * ) &y; 
    i  
= 0x5fe6ec85e7de30da - ( i >> 1 ); 
    y  
= * ( double * ) &i;
    y  
= y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
    y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
    return y;
}