[英]Is there any square root function more accurate than java.lang.Math.sqrt(double)
I am looking for square root functions (to use in java) that can give square roots upto atleast to 5-6 decimal places accurately.我正在寻找平方根函数(在 java 中使用),它可以准确地给出至少 5-6 位小数的平方根。 If there is a way to control accuracy of java.lang.Math.sqrt(double) or any external math library, please mention it.如果有办法控制 java.lang.Math.sqrt(double) 或任何外部数学库的准确性,请提及。
What problem are you trying to solve?你想解决什么问题? I believe java.lang.Math's sqrt is supposed to be accurate to the full double width .我相信java.lang.Math 的 sqrt 应该准确到 full double width 。 Do you need more than this?您还需要更多吗?
Try out the code at this link, which uses Newton's method to compute square roots for large numbers (and gets BigDecimal output) to see if it fits your needs.试试这个链接上的代码,它使用牛顿法计算大数的平方根(并获得 BigDecimal 输出),看看它是否符合您的需要。 If not, you can modify it :) .如果没有,您可以修改它 :) 。
http://www.merriampark.com/bigsqrt.htm http://www.merriampark.com/bigsqrt.htm
You can use this formula to get it to an arbitrary accuracy:您可以使用此公式使其达到任意精度:
http://en.wikipedia.org/wiki/Newton's_method#Square_root_of_a_number
I doubt that it will be particularly fast though.我怀疑它会特别快。 You'd also need a way to store numbers with larger than type(double) accuracy.您还需要一种方法来存储大于 type(double) 精度的数字。
You can always implement your own using Newton's Method .您始终可以使用Newton's Method实现您自己的方法。 Then you can make it as accurate as you want (at the cost of CPU of course).然后您可以根据需要使其准确(当然以 CPU 为代价)。
Numerical Recipes in C has code and in-depth discussion. Numerical Recipes in C有代码和深入讨论。 However make sure you check the license before using their code in your product.但是,请确保在您的产品中使用他们的代码之前检查许可证。
You could roll your own using BigDecimal and a method like Newton-Raphson.您可以使用 BigDecimal 和 Newton-Raphson 之类的方法自行推出。 That would allow you to specify a precision.这将允许您指定精度。
Have a look at Numerical Recipes if you need any inspiration.如果您需要任何灵感,请查看数字食谱。
Math.sqrt数学.sqrt
public static double sqrt(double a) public static double sqrt(double a)
Returns the correctly rounded positive square root of a double value .返回双精度值的正确舍入的正平方根。 Special cases:特别案例:
Otherwise, the result is the double value closest to the true mathematical square root of the argument value.否则,结果是最接近参数值的真实数学平方根的双精度值。
Parameters:参数:
Returns:返回:
This question has been answered thoroughly enough already, but here is an experimental way to verify that Math.sqrt
is accurate:这个问题已经得到了足够彻底的回答,但这里有一个实验方法来验证Math.sqrt
是否准确:
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
double max = 0;
for (int i = 0; i < 100; i++) {
double r = random();
double err = abs(pow(sqrt(r), 2) - r) / ulp(r);
if (err > max) max = err;
}
System.out.println(max);
}
}
This prints out 1.0
, confirming what the documentation says — that the value returned from sqrt
will be within one unit of precision to the exact answer.这将打印1.0
,确认文档所说的 - 从sqrt
返回的值将在精确答案的一个精度单位内。
public class SquareRoot {
public void sqRoot_Efficient(int n){ //Time Complexity: O(log(n))
for(int i=1;i<=n;i++){
if((n/i)*(n/i) < n){
System.out.println(n/i);
break;
}
}
}
public static void main(String[] args) {
SquareRoot sr=new SquareRoot();
sr.sqRoot_Efficient(27);
}
}
我通常建议的Apache公地作为第一个地方去寻找大多数的任务。但它出现在commons.math不包括开方()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.