java 中平方根(sqrt)算法
平方根(sqrt, square root)是数学中常见的数学的公式;
使用程序进行求平方根主要分为两步:
第一步: while()循环, 控制循环次数及小数的位数, 防止无限循环和出现多位小数;
第二步: 通过分解平方根, 使用循环, 逐渐减小,接近平方根;
立即学习“Java免费学习笔记(深入)”;
同理, 其他方根也可以类似扩展, 不过需要注意的是,
偶数次方根需要确保输入正数;
奇数次方根需要转换为正数, 确保循环收敛, 再进行结果正负判断;
代码如下:
/*
* Algorithms.java
*
* Created on: 2013.12.03
* Author: Wendy
*/
/*eclipse std kepler, jdk 1.7*/
public class Algorithms
{
public static double sqrt(double c)
{
if(c<0) return Double.NaN; //NaN: not a number
double err = 1e-15; //极小值
double t = c;
while (Math.abs(t-c/t) > err*t) //t^2接近c, 防止小数
t = (c/t + t)/2.0;
return t;
}
public static double cbrt(double c)
{
boolean b = (c>0) ? true : false; //保存c的符号
c = (c>0) ? c : -c;
double err = 1e-15;
double t = c;
while(Math.abs(t*t-c/t) > err*t)
t = (c/(t*t)+t)/2.0;
t = (b) ? t : -t;
return t;
}
public static void main(String[] args)
{
double r = sqrt(4.0);
System.out.println("sqrt(4.0) = " + r);
double rc = cbrt(-27.0);
System.out.println("cbrt(9.0) = " + rc);
}
}输出:
sqrt(4.0) = 2.0 cbrt(9.0) = -3.0
以上就是java求平方根(sqrt)的代码演示的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号