📄 sqrt1.cpp
字号:
// sqrt1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sqrt1.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
float G_SqrtTable[262144];
long G_SqrtTable1[262144];
using namespace std;
unsigned long SquareRoot(unsigned long nSquare)
{
unsigned long nRoot, nTest=1;
do
{
nRoot = nTest;
nTest = (nSquare/nRoot+nRoot) >> 1;
} while ((nTest - nRoot > 1) || (nRoot - nTest > 1));
//while ((nTest >> 1) != (nRoot >> 1));
return nRoot;
}
long sqrt1(long xk)//开平方函数
{
long y1,y0;
y1=0;
y0=0;
if(xk<0)
return 0;
else if(xk==0 || xk==1)
return xk;
y0=xk;
while(1)
{
y1=y0/2+xk/(2*y0);
if(y1>(y0-2) && y1<(y0+1))
break;
else
y0=y1;
}
return y1;
}//end
float kf(float shu)
{
float a,b,i;
a=shu/2;
for(i=1;i<100;i++)
{
b=a-(a*a-shu)/(2*a);
a=b;
}
return a;
}
float sqrt2(DWORD tk)//开平方函数
{
// if(xk<256)
return G_SqrtTable[tk];
// int m = 12;
// DWORD tk = xk;
// while (/*0 == */!(tk & 0xC0000000))
// {
// tk = tk<<2;
// m--;
// }
//DWORD sk = tk >> 24;
// float y1=0,y0=0;
// return G_SqrtTable1[tk >> 24]<<m;
// y0=G_SqrtTable1[sk]<<m/*(12-m)*/;
// return y0;
/* y1=y0/2+(xk>>1)/y0;
y0=y1/2+(xk>>1)/y1;
y1=y0/2+(xk>>1)/y0;
return y1;
while(1)
{
y1=y0/2+(xk>>1)/y0;
if(fabs(y1-y0)<0.1)
break;
else
y0=y1;
}
return y1;*/
}//end
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int i,j,k=100;;
float sq;
for (i=0;i<262144;i++)
{
G_SqrtTable[i] = sqrt(i);
G_SqrtTable1[i] = int(G_SqrtTable[i]);
}
printf("%d\n",sizeof(float));
goto l1;
unsigned long shu;
printf("请输入一个正数:");
scanf("%ld",&shu);
if(shu<=0)
printf("输入错误!");
else
printf("%d\t%f\n",SquareRoot(shu),sqrt(shu));
return 0;
l1: DWORD t1,t2;
t1=GetTickCount();
for( j=1;j<100000;j++ )
for (i=69;i<79;i++)
{
sq = sqrt(i);
}
t2=GetTickCount();
printf("lib sqrt:\t%d\n",t2-t1);
t1=GetTickCount();
//for( j=1;j<10;j++ )
unsigned long ui;
unsigned long usq;
for(j=0;j<100000;j++)
for (ui=69;ui<79;ui++)
{
//k=100/10;
//sq = G_SqrtTable[i];//sqrt2(i);
usq = SquareRoot(ui);
}
t2=GetTickCount();
printf("my sqrt:\t%d\n",t2-t1);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -