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

推荐订阅源

B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
腾讯CDC
G
Google Developers Blog
宝玉的分享
宝玉的分享
I
InfoQ
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
IT之家
IT之家
D
DataBreaches.Net
Martin Fowler
Martin Fowler
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog

博客园 - 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;
}