最好的求平方根的方法(精确度VS速度)

本文对比了12种求平方根的方法,包括Babylonian Method和Newton's Method等,通过速度与精确度评估,帮助游戏开发者选择最适合其需求的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章来源链接:http://www.codeproject.com/KB/cpp/Sqrt_Prec_VS_Speed.aspx

Technorati 标签: 平方根, 算法, 速度, 精确度, Quake3, c++, c

简介:
我喜欢用DirectX进行游戏编程。我注意到在我的游戏中大多数被调用函数都是在Math.h中的标准sprt方法。所以,我被迫寻找比标准sqrt函数更加快速的函数。经过一段时间的搜索,我发现很多函数都比sqrt函数快的多,但是,这些函数总是在精确度和速度之间取折中的解决方法。 这篇文章的目的是为了帮助程序员选择适合他们程序的最好的求平方根方法

背景知识:
在这篇文章里,我以标准sqrt函数为参考,将12中不同的求平方根方法和它进行比较。对于每一种方法,我将它的精确度和速度和
sqrt 函数进行比较。

文章不包括:
1、解释每种方法的工作原理
2、计算平方根的新方法。

应用代码:
代码很简单,基本上包括:
1、main.cpp
调用所有的方法,每一个计算出和sqrt函数比较的精确度和速度。

2、SquareRootmethods.h
这个头文件包括函数的实现,和我查找这些方法的参考。

----------------------------------------------------

首先,我计算了作为参考的标准sqrt函数的精确度和速度。
为了计算速度,我记录了调用sqrt函数(M-1)次花费的时间。再将这个值赋值给 RefSpeed 作为我的参考值。

为了计算精确度,我每次调用函数时,把每次的计算结果累加,记录在变量 RefTotalPrecision 中。RefTotalPrecision 变量就是我的参考值。

为了记录函数的运行时持续速度,我应用了CDuration 类(在这个链接里找到的),CDuration 类的对象是 dur

	for(int j=0;j<AVG;j++)	
	{	
	  dur.Start();
		
		for(int i=1;i<M;i++)
		   RefTotalPrecision+=sqrt((float) i);
	
      	 dur.Stop();
	
	  Temp+=dur.GetDuration();
	
	}
	
	RefTotalPrecision/=AVG;
	Temp/=AVG;

	
	RefSpeed=(float)(Temp)/CLOCKS_PER_SEC;

对于其它算法,我做了相同的计算,但是在最后将它们和sqrt做了比较。

	for(int j=0;j<AVG;j++)	
	{	
	  dur.Start();
		
		for(int i=1;i<M;i++)
		    TotalPrecision+=sqrt1((float) i);
	
          dur.Stop();
	
	  Temp+=dur.GetDuration();
	
	}
	
	TotalPrecision/=AVG;
	Temp/=AVG;
		
	Speed=(float)(Temp)/CLOCKS_PER_SEC;

cout<<"Precision = "
<<(double)(1-abs((TotalPrecision-RefTotalPrecision)/(RefTotalPrecision)))*100<<endl;    

         

注意:
1 我认为计算精度时 区分谁大谁小没有意义,因此用abs取其绝对值。
2 (针对下面两个图表)“精度表”中的数字表示计算精度(相对于math.sqrt之精度)下降的百分比,“速度表”中的数值是(相对于math.sqrt之速度)的百分比。

(这段不太理解,一下是原文:

1、I Assume that the error in Precision whether larger or smaller than the reference is equal that's why i use "abs". 
2、The Speed is refrenced as the actual percentage, while the Precision is referenced a decrease percentage. )

你可以根据自己的喜欢修改M值,我将M值初始化为10000。
你也可以修改AVG值,这个值越高,精确度就越高。

#define M 10000   
#define AVG 10 

有趣的要点:
标准sqrt方法的精确度是最好的,但是,其它函数能快5倍以上。我个人会选择方法N#2。因为,它精确度高,速度也快。但是,我还有
把选择权教给你吧。

我抽取了5个样本进行平均。下面是输出的结果图:

 

注意:这些函数的运行很多程度依赖于你计算机的中央处理器,所以,一台计算机的结果和另一台的会有所不同。

方法:
Sqrt1:

参考链接:http://ilab.usc.edu/wiki/index.php/Fast_Square_Root
运算法则:Babylonian Method + 一些 IEEE 32bit浮点数表示的控制 。

float sqrt1(const float x)  
{
  union
  {
    int i;
    float x;
  } u;
  u.x = x;
  u.i = (1<<29) + (u.i >> 1) - (1<<22); 
  
  // Two Babylonian Steps (simplified from:)
  // u.x = 0.5f * (u.x + x/u.x);
  // u.x = 0.5f * (u.x + x/u.x);
  u.x =       u.x + x/u.x;
  u.x = 0.25f*u.x + x/u.x;

  return u.x;
}  
Sqrt2

Sqrt2:
参考链接:http://ilab.usc.edu/wiki/index.php/Fast_Square_Root
运算法则:The Magic Number (Quake 3)

#define SQRT_MAGIC_F 0x5f3759df 
 float  sqrt2(const float x)
{
  const float xhalf = 0.5f*x;
 
  union // get bits for floating value
  {
    float x;
    int i;
  } u;
  u.x = x;
  u.i = SQRT_MAGIC_F - (u.i >> 1);  // gives initial guess y0
  return x*u.x*(1.5f - xhalf*u.x*u.x);// Newton step, repeating increases accuracy 
}   

Sqrt3:
参考链接:http://www.dreamincode.net/code/snippet244.htm
运算法则:Log base 2 approximation and Newton's Method  

float sqrt3(const float x)  
{
  union
  {
    int i;
    float x;
  } u;

  u.x = x;
  u.i = (1<<29) + (u.i >> 1) - (1<<22); 
  return u.x;
} 

Sqrt4:
参考链接:很久以前在论坛上看到的,现在忘记了,如果你有参考链接请联系我。
运算法则:Bakhsali Approximation

float sqrt4(const float m)
{
   int i=0; 
   while( (i*i) <= m )
          i++;
    i--; 
   float d = m - i*i; 
 float p=d/(2*i); 
 float a=i+p; 
   return a-(p*p)/(2*a);
}  

Sqrt5:
参考链接:http://www.dreamincode.net/code/snippet244.htm
运算法则:Babylonian Method

   float sqrt5(const float m)
{
   float i=0;
   float x1,x2;
   while( (i*i) <= m )
          i+=0.1f;
   x1=i;
   for(int j=0;j<10;j++)
   {
       x2=m;
      x2/=x1;
      x2+=x1;
      x2/=2;
      x1=x2;
   }
   return x2;
}   

Sqrt6:
参考链接:http://www.azillionmonkeys.com/qed/sqroot.html#calcmeth
运算法则:依赖IEEE,只在32bit上工作。

(原文如下:Dependant on IEEE representation and only works for 32 bits )

double sqrt6 (double y) 
{
    double x, z, tempf;
    unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
    tempf = y;
   *tfptr = (0xbfcdd90a - *tfptr)>>1; 
 x =  tempf;
 z =  y*0.5;                       
 x = (1.5*x) - (x*x)*(x*z);    //The more you make replicates of this statment the higher the 					//accuracy, here only 2 replicates are used  
  x = (1.5*x) - (x*x)*(x*z);       
  return x*y; 
  }  

Sqrt7:
参考链接: http://bits.stephan-brumme.com/squareRoot.html
运算法则:依赖IEEE,只在32bit上工作。

float sqrt7(float x)
 {
   unsigned int i = *(unsigned int*) &x; 
   // adjust bias
   i  += 127 << 23;
   // approximation of square root
   i >>= 1; 
   return *(float*) &i;
 }   


Sqrt8:
参考链接: http://forums.techarena.in/software-development/1290144.htm
运算法则: Babylonian Method

double sqrt9( const double fg)
{ 
 double n = fg / 2.0;
 double lstX = 0.0; 
 while(n != lstX)  
 { 
 lstX = n;
 n = (n + fg/n) / 2.0; 
 }
 return n;
 }  

Sqrt9:
参考链接: http://www.functionx.com/cpp/examples/squareroot.htm
运算法则: Babylonian Method 

 double Abs(double Nbr)
{ 
 if( Nbr >= 0 ) 
  return Nbr; 
 else
  return -Nbr;
}

double sqrt10(double Nbr)
{
 double Number = Nbr / 2; 
 const double Tolerance = 1.0e-7; 
 do
 {
  Number = (Number + Nbr / Number) / 2;
 }while( Abs(Number * Number - Nbr) > Tolerance);
	
 return Number;
}   

Sqrt10:
参考链接: http://www.cs.uni.edu/~jacobson/C++/newton.html
运算法则: Newton's Approximation Method
double sqrt11(const double number)e
{
const double ACCURACY=0.001;
double lower, upper, guess;

 if (number < 1)
 {
  lower = number;
  upper = 1;
 }
 else
 {
  lower = 1;
  upper = number;
 }

 while ((upper-lower) > ACCURACY)
 {
  guess = (lower + upper)/2;
  if(guess*guess > number)
   upper =guess;
  else
   lower = guess; 
 }
 return (lower + upper)/2;

}  

Sqrt11:
参考链接: http://www.drdobbs.com/184409869;jsessionid=AIDFL0EBECDYLQE1GHOSKH4ATMY32JVN
运算法则: Newton's Approximation Method

 double sqrt12( unsigned long N )
{
    double n, p, low, high;
    if( 2 > N )
        return( N );
    low  = 0;
    high = N;
    while( high > low + 1 )
    {
        n = (high + low) / 2;
        p = n * n;
        if( N < p )
            high = n;
        else if( N > p )
            low = n;
        else
            break;
    }
    return( N == p ? n : low );
}  

Sqrt12:
参考链接:http://cjjscript.q8ieng.com/?p=32
运算法则: Babylonian Method 

	double sqrt13( int n )
	{
		// double a = (eventually the main method will plug values into a)
		double a = (double) n;
		double x = 1;
 
		// For loop to get the square root value of the entered number.
		for( int i = 0; i < n; i++)
		{
			x = 0.5 * ( x+a / x );
		}
 
		return x;
	}  

最后,我希望这篇贫乏的文章可以对感兴趣的人有一点帮助。

05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result {"code":200,"msg":"操作成功","data":[{"id":1,"name":"华为Mate 60 Pro","model":"ALN-AL80","imageUrl":"https://img.alicdn.com/imgextra/i3/2213047591560/O1CN01QNx6eB1NOWu3oeUfn_!!2213047591560.jpg_q50.jpg_.webp","shortDescription":"麒麟9000S 卫星通信","price":6999.00,"subsidizedPrice":6499.00,"sales":15000,"rating":4.90,"stock":500,"isAvailable":0,"brand":"华为"},{"id":2,"name":"华为P60 Art","model":"NOH-AN80","imageUrl":"https://img.alicdn.com/imgextra/i4/2609173245/O1CN011h2L1I1ZqGCdlsYlJ_!!2609173245.jpg_q50.jpg_.webp","shortDescription":"超聚光XMAGE影像","price":7988.00,"subsidizedPrice":7588.00,"sales":12000,"rating":4.80,"stock":300,"isAvailable":0,"brand":"华为"},{"id":3,"name":"华为nova 12","model":"BAC-AL00","imageUrl":"https://picasso.alicdn.com/imgextra/O1CNA1bnbda21Vuba4TjLYB_!!2838892713-0-psf.jpg_.webp","shortDescription":"前置双摄人像","price":2999.00,"subsidizedPrice":2699.00,"sales":50000,"rating":4.70,"stock":1000,"isAvailable":0,"brand":"华为"},{"id":4,"name":"荣耀Magic6 Pro","model":"PGT-AN30","imageUrl":"https://img.alicdn.com/imgextra/i2/263726286/O1CN01ko93gt1wJ2hQAORAB_!!4611686018427380942-0-item_pic.jpg_.webp","shortDescription":"骁龙8 Gen3 青海湖电池","price":5699.00,"subsidizedPrice":5399.00,"sales":25000,"rating":4.80,"stock":600,"isAvailable":0,"brand":"荣耀"},{"id":5,"name":"荣耀X50 GT","model":"ANY-AN00","imageUrl":"https://img.alicdn.com/imgextra/i3/3851598352/O1CN01UHPfyv2BZGwPnHUlx_!!0-item_pic.jpg_q50.jpg_.webp","shortDescription":"1.5K护眼曲屏","price":1999.00,"subsidizedPrice":1799.00,"sales":80000,"rating":4.60,"stock":1500,"isAvailable":0,"brand":"荣耀"},{"id":6,"name":"荣耀90 Pro","model":"REA-AN20","imageUrl":"https://img.alicdn.com/imgextra/i3/1114511827/O1CN01r3ngr41PMof7MhFhY_!!4611686018427386323-0-item_pic.jpg_q50.jpg_.webp","shortDescription":"2亿像素写真相机","price":3299.00,"subsidizedPrice":2999.00,"sales":40000,"rating":4.70,"stock":800,"isAvailable":0,"brand":"荣耀"},{"id":7,"name":"荣耀Play7T Pro","model":"CMA-AN40","imageUrl":"https://img.alicdn.com/imgextra/i1/1907069314/O1CN019jd3kt2IfrnZ6J0AH_!!1907069314.jpg_q50.jpg_.webp","shortDescription":"40W快充 OLED屏","price":1799.00,"subsidizedPrice":1599.00,"sales":120000,"rating":4.50,"stock":2000,"isAvailable":0,"brand":"荣耀"},{"id":8,"name":"华为畅享70 Pro","model":"CMA-AN70","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"7000mAh长续航","price":1599.00,"subsidizedPrice":1399.00,"sales":150000,"rating":4.40,"stock":3000,"isAvailable":0,"brand":"华为"},{"id":9,"name":"华为Mate X5","model":"TET-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"折叠屏 玄武钢化","price":12999.00,"subsidizedPrice":12499.00,"sales":5000,"rating":4.90,"stock":100,"isAvailable":0,"brand":"华为"},{"id":10,"name":"荣耀Magic Vs2","model":"CMA-AN80","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"轻薄折叠屏","price":8999.00,"subsidizedPrice":8499.00,"sales":10000,"rating":4.70,"stock":300,"isAvailable":0,"brand":"荣耀"},{"id":11,"name":"iPhone 15 Pro Max","model":"A3108","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A17 Pro 钛金属","price":9999.00,"subsidizedPrice":null,"sales":80000,"rating":4.80,"stock":200,"isAvailable":0,"brand":"苹果"},{"id":12,"name":"iPhone 15","model":"A3092","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"A16芯片 灵动岛","price":5999.00,"subsidizedPrice":null,"sales":150000,"rating":4.80,"stock":5000,"isAvailable":0,"brand":"苹果"},{"id":13,"name":"iPhone 15 Plus","model":"A3096","imageUrl":"https://android-api.oss-cn-beijing.aliyuncs.com/logo.jpg","shortDescription":"6.7英寸大屏","price":6999.00,"subsidizedPrice":null,"sales":80000,"rating":4.70,"stock":3000,"isAvailable":0,"brand":"苹果"},{"id":14,"name":"iPhone SE 4","model":"A2785","imageUrl":"https://android-api.oss-cn-beijing.aliyun 05-23 16:00:49.311 25132-25512 A0c0d0/JSAPP I result [object Object] 05-23 16:00:49.359 25132-25512 C03f00/ArkCompiler E [ArkRuntime Log] TypeError: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Lifetime: 0.000000s 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Js-Engine: ark 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]page: pages/Index.js 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Error message: is not callable 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log]Stacktrace: 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at forEachUpdateFunction (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4352:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:144:13) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateFunc (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6812:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at UpdateElement (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6514:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at anonymous (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6741:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at updateDirtyElements (/devcloud/slavespace/usr1/081f8aba80800f0f0fcec015bd66c7e0/harmony_code/codearts_workspace/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6736:1) 05-23 16:00:49.361 25132-25512 C03900/Ace E [Engine Log] at rerender (entry|entry|1.0.0|src/main/ets/pages/Index.ts:152:9)
最新发布
05-24
### 关于 HarmonyOS 或 ArkCompiler 中 'is not callable' 的 TypeError 问题 在 HarmonyOS 和 ArkCompiler 开发环境中遇到 `TypeError: is not callable` 错误通常表明尝试调用的对象并非函数或可调用对象。这种错误可能由多种原因引起,例如变量名冲突、未正确定义的回调函数或者对 `null` 值进行了不当操作。 #### 可能的原因分析 1. **变量覆盖了内置方法或函数** 如果开发者无意间将某些全局名称重新赋值为其他类型的值(如 `null`),可能会导致该错误。例如,在 JavaScript 环境下,如果重写了 `Array.prototype.map` 方法并将其设置为 `null`,那么后续对该方法的调用会失败[^4]。 2. **异步编程中的上下文丢失** 在使用箭头函数或其他形式绑定事件处理器时,如果没有正确处理 this 上下文,则可能导致原本期望作为函数执行的内容变成了 undefined 或 null。这种情况常见于 React 组件生命周期方法以及 Vue.js 自定义指令场景中[^5]。 3. **API 返回值异常** 当从外部服务获取数据时,若返回的结果不符合预期结构——比如应该是一个函数却实际接收到的是 null ——也会引发此类错误消息提示[^6]。 4. **Null 安全性问题** 类似于 MySQL 数据库设计里提到允许字段为空 (NULL)[^2], 在程序逻辑层面也需要考虑如何优雅地应对可能出现的 Null 情况。如果不小心试图直接调用了被赋予 Null 的属性或方法就会抛出类似的运行期错误。 #### 解决方案建议 针对以上几种可能性提供相应的解决方案如下: - #### 验证目标是否确实具备可调用特性 在每次准备调用前先验证其类型是否属于 Function 。可以利用 typeof 运算符来进行初步判断。 ```javascript const maybeFunction = someCondition ? () => {} : null; if(typeof maybeFunction === 'function') { maybeFunction(); } ``` - #### 使用 Optional Chaining 提高健壮性 ES2020 引入了一个新功能叫做 optional chaining (`?.`) ,它可以帮助我们更简洁安全地访问深层嵌套对象而不用担心中间环节存在 null/undefined 导致崩溃风险。 ```javascript let result = possiblyUndefinedObject ?. methodToCall(); // 不会报错即使object不存在 ``` - #### 明确区分命名空间内的同名实体 尽量避免自定义标识符与框架内部保留字发生碰撞;另外通过模块化组织代码也能有效减少这类隐患的发生几率。 - #### 调试工具辅助定位具体位置 利用断点调试技术逐步跟踪哪一部分代码最终触发了这个特定异常,并针对性修复源头问题所在之处。 ```javascript try{ potentiallyProblematicCode(); }catch(e){ console.error('Error occurred:', e); } ``` ### 总结 通过对 HarmonyOS / ArkCompiler 平台开发过程中常见的 “not callable” 类型错误现象剖析可知,这往往涉及到基本概念理解偏差或者是编码习惯上的疏漏所致。采取适当预防措施加上良好实践能够显著降低遭遇这些问题的概率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值