【发布时间】:2019-08-18 07:53:31
【问题描述】:
我在使用必须计算给定参数的平方根的 Java 代码时遇到问题。 但是,经过一番研究,我发现了一个我不知道如何正确实现的代码。
// read in the command-line argument
double c = Double.parseDouble(args[0]);
double epsilon = 1.0e-15; // relative error tolerance
double t = c; // estimate of the square root of c
// repeatedly apply Newton update step until desired precision is achieved
while (Math.abs(t - c/t) > epsilon *t) {
t = (c/t + t) / 2.0;
}
// print out the estimate of the square root of c
System.out.println(t);
-
我不完全理解的第一件事是为什么它们在第 8 行除以二。
t = (c/t + t) / 2.0; -
我不明白的第二件事是 while 循环中的条件,更准确地说:
while(Math.abs(t - c/t) > epsilon*t) -
不需要只有:
while(Math.abs(t - c/t) > epsilon)
【问题讨论】:
-
你从哪里得到
epsilon*t?这篇文档/文章对这个表达式写了什么以及为什么使用因子*t?
标签: java newtons-method