我编写了一个程序来计算sqrt(x)的估计值:
以下代码说明
我正在尝试找到最接近的数字(S)到(x),可以表示为 S = sqrt * sqrt ,保持循环直到fabs(_sqrt - sqrt) >= 1e-8
不正确,这意味着那个sqrt和_sqrt非常接近。
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
double val, min, max;
double sqrt;
double S, _sqrt;
int cnt = 0;
// enter an argument,
if (argc < 2) {
printf ("Usage: sqrt < number >\n");
return 1;
}
val = fabs (atof (argv[1]));
min = 0;//minimum
max = sqrt = val;//maximum
_sqrt = 1;
//1e-8 = 10^(-8) = 0.00000001
while (fabs(_sqrt - sqrt) >= 1e-8) {
_sqrt = sqrt;//keep the old sqrt
sqrt = (max + min) / 2;
S = sqrt * sqrt;
printf("test n(%d)\tsqrt(%lf) = %.6lf\n",++cnt,val, sqrt);
if (S>val){
max = sqrt;//setting max to the current sqrt
} else{
min = sqrt;//setting min to the current sqrt
}//end else
}//end while
puts("");
puts("\t====================================================");
printf ("\tfinal value.\tsqrt(%lf) = %.6lf\n", val, sqrt);
puts("\t====================================================");
return 0;
}//end main()
输出
[ar.lnx@host square-root] $ ./sqrt 2
test n(1) sqrt(2.000000) = 1.000000
test n(2) sqrt(2.000000) = 1.500000
test n(3) sqrt(2.000000) = 1.250000
test n(4) sqrt(2.000000) = 1.375000
test n(5) sqrt(2.000000) = 1.437500
test n(6) sqrt(2.000000) = 1.406250
test n(7) sqrt(2.000000) = 1.421875
test n(8) sqrt(2.000000) = 1.414062
test n(9) sqrt(2.000000) = 1.417969
test n(10) sqrt(2.000000) = 1.416016
test n(11) sqrt(2.000000) = 1.415039
test n(12) sqrt(2.000000) = 1.414551
test n(13) sqrt(2.000000) = 1.414307
test n(14) sqrt(2.000000) = 1.414185
test n(15) sqrt(2.000000) = 1.414246
test n(16) sqrt(2.000000) = 1.414215
test n(17) sqrt(2.000000) = 1.414200
test n(18) sqrt(2.000000) = 1.414207
test n(19) sqrt(2.000000) = 1.414211
test n(20) sqrt(2.000000) = 1.414213
test n(21) sqrt(2.000000) = 1.414214
test n(22) sqrt(2.000000) = 1.414214
test n(23) sqrt(2.000000) = 1.414213
test n(24) sqrt(2.000000) = 1.414214
test n(25) sqrt(2.000000) = 1.414214
test n(26) sqrt(2.000000) = 1.414214
test n(27) sqrt(2.000000) = 1.414214
test n(28) sqrt(2.000000) = 1.414214
====================================================
final value. sqrt(2.000000) = 1.414214
====================================================
但它只适用于每个x&gt; = 1,如果我想计算0.5或0.254或0.1的sqrt ......它不起作用!
[ar.lnx@host square-root] $ ./sqrt1 0.5
test n(1) sqrt(0.500000) = 0.250000
test n(2) sqrt(0.500000) = 0.375000
test n(3) sqrt(0.500000) = 0.437500
test n(4) sqrt(0.500000) = 0.468750
test n(5) sqrt(0.500000) = 0.484375
test n(6) sqrt(0.500000) = 0.492188
test n(7) sqrt(0.500000) = 0.496094
test n(8) sqrt(0.500000) = 0.498047
test n(9) sqrt(0.500000) = 0.499023
test n(10) sqrt(0.500000) = 0.499512
test n(11) sqrt(0.500000) = 0.499756
test n(12) sqrt(0.500000) = 0.499878
test n(13) sqrt(0.500000) = 0.499939
test n(14) sqrt(0.500000) = 0.499969
test n(15) sqrt(0.500000) = 0.499985
test n(16) sqrt(0.500000) = 0.499992
test n(17) sqrt(0.500000) = 0.499996
test n(18) sqrt(0.500000) = 0.499998
test n(19) sqrt(0.500000) = 0.499999
test n(20) sqrt(0.500000) = 0.500000
test n(21) sqrt(0.500000) = 0.500000
test n(22) sqrt(0.500000) = 0.500000
test n(23) sqrt(0.500000) = 0.500000
test n(24) sqrt(0.500000) = 0.500000
test n(25) sqrt(0.500000) = 0.500000
test n(26) sqrt(0.500000) = 0.500000
====================================================
final value. sqrt(0.500000) = 0.500000
====================================================
有人可以帮我理解问题是什么以及如何解决它?
答案 0 :(得分:4)
问题是您正在将max
初始化为val
。对于小于1的数字,平方根将更大而不是更少。例如,对于0.5,平方根为0.707 ...如果max
小于1,则简单的修复方法是将val
初始化为1.
答案 1 :(得分:3)
原因是您的转化逻辑仅在val > 1
请注意您对sqrt(.5)
进行测试的方式自val == 0.5
S = val * val
0.25
后min
。这会将0.25
重置为sqrt = (max + min) / 2;
,因为max
和初始val
为0.5
(0.5),您永远无法超越val
。
您必须确定,因为您的初始max
小于1,您希望您的初始val < sqrt < 1
为1,以便您的平均值为1.5/2 == .75
如果你这样做,那么你应该收集正确的值。
我目前无法访问C编译器。但是,通过初始检查,第一个平均值为1.25/2 == .625
,它会重置最大值。新的平均值为public static final char blank = ' ';
public static final char endOfSequence = '\n'; //the lines in the file end with '\n'
private static final int MAX = 20;
public static char[] letters;
public static int length;
private static char letter = ' ';
private static char[] phrase = null;
private static int index;
public Word() {
letters = new char[MAX];
length = 0;
}
public static Word read() {
Word wd = new Word();
jumpBlanks();
while ((letter != endOfSequence) && // Sequence hasn't finished
(letter != blank)) {
wd.letters[wd.length] = letter;
letter = readCharKeyB();
}
return wd;
}
public static void jumpBlanks() { //reading blanks until finding a word
while (letter == blank) {
letter = readCharKeyB();
}
}
static public char readCharKeyB() {
char res = '.';
if (phrase != null) {
res = phrase[index++];
}
return res;
}
,它会重置最小值。然后您应该继续,直到收敛到正确的值。
仅供参考,您还可以将while循环的内容用作不同的函数,以便自学递归。也就是说,如果S> 1,则不是循环一段时间。 lim {调用新的估计函数}一旦它通过递归,它将最终估计值传递回初始调用函数。
答案 2 :(得分:2)
问题是你的max
的价值,它必须大于或等于预期的结果。如果您的输入小于1.0
,则结果大于输入且小于1.0
。您max
的价值必须至少为1.0
。像这样调整你的代码。
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
double val, min, max;
double sqrt;
double S, _sqrt;
int cnt = 0;
// enter an argument,
if (argc < 2) {
printf ("Usage: sqrt < number >\n");
return 1;
}
val = fabs (atof (argv[1]));
if ( val < 1.0 )
{
min = val;
max = sqrt = 1.0; // if val < 1.0 result is greater than val but less than 1.0
}
else
{
min = 0; //minimum
max = sqrt = val; //maximum
}
do
{
_sqrt = sqrt;//keep the old sqrt
sqrt = (max + min) / 2;
S = sqrt * sqrt;
printf("test n(%d)\tsqrt(%lf) = %.6lf\n",++cnt,val, sqrt);
if (S>val){
max = sqrt;//setting max to the current sqrt
} else{
min = sqrt;//setting min to the current sqrt
}//end else
}
while ( fabs(_sqrt - sqrt) >= 1e-8); //1e-8 = 10^(-8) = 0.00000001
puts("");
puts("\t====================================================");
printf ("\tfinal value.\tsqrt(%lf) = %.6lf\n", val, sqrt);
puts("\t====================================================");
return 0;
}