免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11407 | 回复: 13
打印 上一主题 下一主题

卡马克算法 开方函数 哪位解释一下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-13 17:03 |只看该作者 |倒序浏览
30可用积分
看到一个快速开方算法,称为卡马克算法,是在精度不高的sqrt()函数之上进行了封装,得到一个精度更高的开方函数。如下所示,rsqrt()为sqrt()函数的倒数,输入输出都为float类型。最终的结果为double的精度。我测试了一下,确实精度很高,但是不太懂原理,哪位给讲讲,谢谢。


  1. double sqrt2(double y)
  2. {
  3.         double x = (double)(rsqrt((float)y));
  4.         double z = y * 0.5;
  5.         x = 1.5 * x - (x * x * x * z);
  6.         x = 1.5 * x - (x * x * x * z);
  7.         x = 1.5 * x - (x * x * x * z);
  8.         return x * y;
  9. }
复制代码

最佳答案

查看完整内容

ohn Carmack的密码:0x5f3759df有人在Quake III的源代码里面发现这么一段用来求平方根的代码:/*================SquareRootFloat================*/float SquareRootFloat(float number){ long i; float x, y; const float f = 1.5F; x = number * 0.5F; y = number; i = * ( long * ) &y; i = 0x5f3759df - ( i >> 1 ); //注意这一行 y = * ( float * ) &i; y = y * ( f - ( x * ...

论坛徽章:
0
2 [报告]
发表于 2009-07-13 17:03 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2009-07-13 17:10 |只看该作者
哪里摘来的?quake3里面得么?~~

论坛徽章:
0
4 [报告]
发表于 2009-07-13 17:16 |只看该作者
tjj bdq
ding!

论坛徽章:
0
5 [报告]
发表于 2009-07-13 17:17 |只看该作者
http://www.cnblogs.com/vagerent/archive/2007/06/25/794695.html
这篇里的不知道是不是你想要的。

论坛徽章:
0
6 [报告]
发表于 2009-07-13 17:24 |只看该作者
原帖由 cugb_cat 于 2009-7-13 17:17 发表
http://www.cnblogs.com/vagerent/archive/2007/06/25/794695.html
这篇里的不知道是不是你想要的。


那篇文章里面说的和lz的算法不大一样~~


  1. //
  2. // Carmack在QUAKE3中使用的计算平方根的函数
  3. //
  4. float CarmSqrt(float x){
  5.     union{
  6.         int intPart;
  7.         float floatPart;
  8.     } convertor;

  9.     union{
  10.         int intPart;
  11.         float floatPart;
  12.     } convertor2;

  13.     convertor.floatPart = x;
  14.     convertor2.floatPart = x;
  15.     convertor.intPart = 0x1FBCF800 + (convertor.intPart >> 1);
  16.     convertor2.intPart = 0x5f3759df - (convertor2.intPart >> 1);
  17.     return 0.5f*(convertor.floatPart + (x * convertor2.floatPart));
  18. }
复制代码

论坛徽章:
0
7 [报告]
发表于 2009-07-13 17:36 |只看该作者
原帖由 xiaomiao 于 2009-7-13 17:31 发表
ohn Carmack的密码:0x5f3759df

有人在Quake III的源代码里面发现这么一段用来求平方根的代码:


/*================SquareRootFloat================*/
float SquareRootFloat(float number)
{
  lo ...



呵呵,赞~~

论坛徽章:
0
8 [报告]
发表于 2009-07-13 17:43 |只看该作者
http://en.wikipedia.org/wiki/Fast_inverse_square_root

Lomont为此写下一篇论文,"Fast Inverse Square Root"。

论坛徽章:
0
9 [报告]
发表于 2009-07-13 18:03 |只看该作者
原帖由 xiaomiao 于 2009-7-13 17:31 发表
ohn Carmack的密码:0x5f3759df

有人在Quake III的源代码里面发现这么一段用来求平方根的代码:


/*================SquareRootFloat================*/
float SquareRootFloat(float number)
{
  lo ...

卡马克不会也是一个数字一个数字的试出来的吧?

论坛徽章:
0
10 [报告]
发表于 2009-07-13 18:03 |只看该作者
原帖由 anders0913 于 2009-7-13 17:10 发表
哪里摘来的?quake3里面得么?~~


嗯。就是里面得到的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP