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

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
IT之家
IT之家
量子位
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
罗磊的独立博客
S
SegmentFault 最新的问题
博客园_首页
N
Netflix TechBlog - Medium
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
A
About on SuperTechFans
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
MyScale Blog
MyScale 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;
}