博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
开平方与魔数0x5F3759DF:Quake III 开源代码
阅读量:4704 次
发布时间:2019-06-10

本文共 921 字,大约阅读时间需要 3 分钟。

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;}

 

float InvSqrt(float x){    float xhalf = 0.5f*x;    int i = *(int*)&x; // get bits for floating VALUE    i = 0x5f375a86- (i>>1); // gives initial guess y0    x = *(float*)&i; // convert bits BACK to float    x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy    return x;}

参考:

http://www.matrix67.com/data/InvSqrt.pdf

http://www.guokr.com/post/90718/

转载于:https://www.cnblogs.com/georgethrax/p/4419133.html

你可能感兴趣的文章
POJ2309BST(树状数组)
查看>>
洛谷P2114 起床困难综合症【位运算】【贪心】
查看>>
Ubuntu+caffe训练cifar-10数据集
查看>>
net 把指定 URI 的资源下载到本地
查看>>
js中 $ 未定义 或者 “xxx”未定义
查看>>
Sublime3插件安装
查看>>
[转]大型网站系统架构的演化
查看>>
非常好的JSUI
查看>>
基于EasyNVR摄像机无插件直播流媒体服务器实现类似于单点登录功能的免登录直播功能...
查看>>
python学习0day
查看>>
课堂练习之检测水军
查看>>
函数指针的使用
查看>>
位图数据结构的操作
查看>>
azkaban用户管理及权限配置
查看>>
GCD学习笔记
查看>>
PHP......会话控制SESSION与COOKIE
查看>>
[转]AchartEngineActivity引擎绘制柱状图、曲线图
查看>>
[转]javascript实现限制上传文件的大小
查看>>
我的Java设计模式-策略模式
查看>>
C# 报表接口样例,简单实用
查看>>