sqrt函数实现
感谢杨工,让我更加认识到自己技术薄弱,这道题源自于和杨工的非正式面试,当时根本没思路,甚至没和查找有丝毫的联系,看来做自己想做的还是要付出努力的。sqrt()即开平方运算,y=x*x,已知Y的情况下求解X的值,基本的思路是找个区间,逐步计算逼近,知道需要的精度。
(1)二分查找
并不是严格的二分查找,设定寻找的区间,在这个区间中一直取中点,计算中点的平方和Y的查找,逐步逼近,直到自己需要的精度:
#define ABS_FLOAT 0.000001
bool eqs(double val1 , double val2)
{
double diff = fabs(val1 - val2) ;
if(diff < ABS_FLOAT)
{
return true ;
}
else
{
return false ;
}
} //获取开方值,二分查找的方法
double SqrtBybisection(double _value)
{
if (_value <= 0 )
return 0 ; double low = 0.0;
double high = 0.0 ; if (_value > 0 && _value < 1)
{
low = _value;
high = 1.0 ;
}
else
{
low = 1.0 ;
high = _value ;
} double mid = (low + high)/2.0 ;
double last = 0.0 ; do
{
if (mid * mid > _value)
{
high = mid ;
}
else
{
low = mid ;
} last = mid ;
mid = (high + low )/ 2.0 ; //std::cout << mid << std::endl ; }while(! eqs( last , mid)) ; return mid ;
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
牛顿迭代法通过泰勒公司展开,通过切线逐步逼近,具体推到可以参考:牛顿逼近 , sqrt实现的代码:
//牛顿迭代法求解
/* f(x) = x^2 - v --> x = x0 - f(x0)/2x0 -->x = (x0 + v / x0) / 2 ;
-->
*/
double SqrtByNewton(const double& _val)
{
double nrt = _val ;
double last_nrt = 0 ;
while (! eqs( nrt , last_nrt))
{
last_nrt = nrt ;
nrt = (nrt + _val / nrt) / 2.0 ;
}
return nrt ;
}
3 技巧算法
看到这种解法,我也很惊讶,程序员真是无底线啊~~
先看看浮点数表示,浮点数不论是float还是double在存储方式上都是遵从IEEE的规范的,float遵从的是IEEE R32.24 ,而double 遵从的是R64.53。
数学中浮点用S=M*2^N, 在计算机中 主要由三部分构成:符号位+指数位(N)+尾数(M),符号位:0为正1为负,指数位:2^M ,移位存储,尾数:即有效数字,规定整数部分为1
float 浮点数内存分布:
31 | 30~23 | 22~0 |
1 位 符号位 | 8位 指数位 | 23位 尾数 |
63 | 62~52 | 51~0 |
1 位 符号位 | 11位 指数位 | 52位 尾数 |
符号位 | 指数位 | 尾数 |
0 | 10000010 | 0001 0000 0000 0000 0000 000 |
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
float sqrtinv(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
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy return 1/x;
}
这个算法速度据说比系统函数还要快,确实,迭代的步骤少来很多,具体解释和浮点的表示有关,可以参考下文:一般而言,一个float数据 共32个bit,和int数据一样。其中前23位为有效数字
,后面接着一个8位数据
表示指数,最后一位表示符号,由于这里被开方的数总是大于0,所以我们暂不考虑最后一个符号位。此时
如果我们把计算机内的浮点数 看做一个整数
,那么
现在开始逐步分析函数。这个函数的主体有四个语句,分别的功能是:
int i = *(int*)&x; 这条语句把 转成
。
i = 0x5f3759df - (i>>1); 这条语句从 计算
。
y = *(float*)&i; 这条语句将 转换为
。
y = y*(1.5f - xhalf*y*y); 这时候的y是近似解;此步就是经典的牛顿迭代法。迭代次数越多越准确。关键是第二步 i = 0x5f3759df - (i>>1); 这条语句从 计算
原理:
令
用 和
带入之后两边取对数,再利用近似表示
算一算就得到:
若取 ,
就是程序里所用的常量0x5f3759df。至于为何选择这个
,则应该是曲线拟合实验的结果。
4 测试结果
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
sqrt函数实现的更多相关文章
- 转:一个Sqrt函数引发的血案
转自:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/1844725.html 源码下载地址:http://diducoder.com/sotr ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- Sqrt函数高效实现
转自一个Sqrt函数引发的血案 我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来 ...
- 一个Sqrt函数引发的血案(转)
作者: 码农1946 来源: 博客园 发布时间: 2013-10-09 11:37 阅读: 4556 次 推荐: 41 原文链接 [收藏] 好吧,我承认我标题党了,不过既然你来了, ...
- 【转载】一个Sqrt函数引发的血案
转自:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html 源码下载地址:http://diducoder ...
- 一个Sqrt函数引发的血案
源码下载地址:http://diducoder.com/sotry-about-sqrt.html 好吧,我承认我标题党了,不过既然你来了,就认真看下去吧,保证你有收获. 我们平时经常会有一些数据运算 ...
- sqrt函数实现(神奇的算法)
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- php sqrt()函数 语法
php sqrt()函数 语法 作用:sqrt()函数的作用是对参数进行求平方根 语法:sqrt(X) 参数: 参数 描述 X 进行求平方根的数字 说明:返回将参数X进行开平方后的结果江苏大理石平台 ...
- PHP sqrt() 函数
实例 返回不同数的平方根: <?phpecho(sqrt(0) . "<br>");echo(sqrt(1) . "<br>"); ...
随机推荐
- Java Hour 56 Spring 和 Hibernate 的集成
上一章节我们完成了一个简单的Spring 的试验品,这章要让Spring 上战场了,不要慌,步骤都是一样的. Spring 对 Hibernate 的支持是很多方面的,第一个战场是SessionFac ...
- [杂] ASP.NET MVC 之 Route To MvcHandler
首先,本文参考了不少东东,仅供Q_L_H自己使用,ZZ自己负责.先上一张全家福: HttpModules and HttpHandlers ASP.NET MVC 是 HttpHandler. Url ...
- blend 从无到有系列之添加自定义Rectangle样式指定到资源文件
相关链接 http://www.cnblogs.com/wildfeng/archive/2012/03/30/2425248.html http://www.cnblogs.com/jv9/arch ...
- dos基本命令
dir :列出当前目录下的文件及文件夹 md :插件目录 rd :删除目录 cd :进入指定目录 cd.. :退回到上一级目录 cd/ :退回到根目录 del :删除文件 exit ...
- Android Studio 1.0首次安装遇到的问题,无法下载SDK
相信,在安装Android Studio的过程中会遇到很多问题,特别是第一次启动下载不了sdk.郁闷了吧. 可以去官网下载,也可以点击这里下载Android Studio和sdk. 一.不下载SDK启 ...
- Xamarin iOS开发中的编辑、连接、运行
Xamarin iOS开发中的编辑.连接.运行 创建好工程后,就可以单击Xamarin Studio上方的运行按钮,如图1.37所示,对HelloWorld项目进行编辑.连接以及运行了.运行效果如图1 ...
- [LintCode] Trapping rain water II
Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 ...
- Javascript中call的使用
Javascript中call的使用自己感觉蛮纠结的,根据文档很好理解,其实很难确定你是否真正的理解. call 方法应用于:Function 对象调用一个对象的一个方法,以另一个对象替换当前对象.c ...
- IE6/IE7下绝对定位position:absolute和margin的冲突问题解决
首先我们来看一个代码: 复制代码代码如下:<div id=”layer1″ style=”margin:20px; border:1px solid #F88; width:400px; “&g ...
- ZOJ 3805 (树形DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...