C语言 有什么方法可以在不使用 math.h 和 sqrt() 的情况下获得数字的平方根?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29018864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any way to obtain square root of a number without using math.h and sqrt()?
提问by Hotbod Handsomeface
I've written a program for class that uses MPI to perform Fox's Block Matrix Multiplication method. I was able to do this by using the sqrt() function in , but in order to compile the program I'm having to type in "mpicc -lm -o ...". Instructions for the hw state to compile the program using "mpicc -o ...", without the -lm. I was just wondering whether there was a way to find the square root of a number (without having to write a separate program to do so). If not, I'll just put the disclaimer in the comments at the top of my .txt file. Thought this might be a good place to ask though. Thanks!
我为使用 MPI 执行 Fox 块矩阵乘法方法的类编写了一个程序。我可以通过在 中使用 sqrt() 函数来做到这一点,但是为了编译程序,我必须输入“mpicc -lm -o ...”。硬件状态使用“mpicc -o ...”编译程序的说明,没有 -lm。我只是想知道是否有办法找到一个数字的平方根(而不必编写单独的程序来这样做)。如果没有,我会将免责声明放在我的 .txt 文件顶部的评论中。认为这可能是一个询问的好地方。谢谢!
回答by Weather Vane
This method uses successive approximations. It doesn't take many iterations. Because the value of rootcan dither, I check the convergence to a small error.
该方法使用逐次逼近。它不需要很多迭代。因为root可以抖动的值,我检查收敛到一个小错误。
//#define MINDIFF 2.2250738585072014e-308 // smallest positive double
#define MINDIFF 2.25e-308 // use for convergence check
double sqroot(double square)
{
double root=square/3, last, diff=1;
if (square <= 0) return 0;
do {
last = root;
root = (root + square / root) / 2;
diff = root - last;
} while (diff > MINDIFF || diff < -MINDIFF);
return root;
}
Or, you could do it more simply by iterating a fixed number of times
或者,您可以通过迭代固定次数来更简单地做到这一点
double sqroot(double square)
{
double root=square/3;
int i;
if (square <= 0) return 0;
for (i=0; i<32; i++)
root = (root + square / root) / 2;
return root;
}
回答by Tal Darom
There is an old computer graphics trick for computing 1/sqrt: (original code from Quake III)
有一个用于计算 1/sqrt 的旧计算机图形技巧:(来自 Quake III 的原始代码)
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 is this?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
You can read all about it here
你可以在这里阅读所有相关信息
BTW, I suggest that you just use the compile flag...
顺便说一句,我建议你只使用编译标志......
回答by Robin Halder
For this You can read babylonian-method. Then with the help of this theorem You can find sqrt()I think.
为此,您可以阅读babylonian-method。然后在这个定理的帮助下你可以找到sqrt()我认为。
回答by vs97
Here's an implementation of square root function using Newton-Raphson method.
这是使用 Newton-Raphson 方法的平方根函数的实现。
The basic idea is that if yis an overestimate to the square root of a non-negative real number xthen x/ywill be an underestimate, or vice versa, and so the average of these two numbers may reasonably be expected to provide a better approximation.
其基本思想是,如果y是高估到非负实数的平方根x,然后x/y会被低估,反之亦然,所以一般这两个数字可以合理地预期,以提供更好的近似。
#define ABS(n) (((n) < 0) ? -(n) : (n)) /* Absolute function */
#define TOL 0.001 /* Tolerance */
float sqrt2(float x)
{
float y = 1.0;
while (ABS(x/y - y) > TOL )
{
y=(y+x/y)/2.0;
}
return y;
}
回答by KALP Patel
This code give output upto 0.000001 so check this out. The Program limited to find between 1 to 1050000000.
这段代码给出的输出高达 0.000001,所以检查一下。该程序仅限于查找 1 到 1050000000 之间。
{
int x;
printf ("Enter number : ");
scanf ("%d",&x);
int i=1,j=1;
float x0=1.0;
float xn=1.0;
for(i=1,j=1;i<x;i=i*10,j++)
if(x/i==0)
i=x;
i=i/10;
j=j-1;
if(j>1)
x0=j*power(10,j/2);
int a;
for(a=1;a<=10;a++)
{
xn=0.5*(x0+(x/x0));
x0=xn;
}
printf("\nSquare root of %d is %f",x,xn);
}
int power(int x,int n)
{
int pow=1;
int i;
for(i=1;i<n;i++)
pow=pow*x;
return pow;
}

