平方根(sqrt)算法 的 详解 及 扩展


版权所有, 禁止转载, 如有商业需求, 请站内联系.

本文地址: http://blog.csdn.net/caroline_wendy/article/details/17114939


平方根(sqrt, square root)是数学中常见的数学的公式; 

使用程序进行求平方根主要分为两步:

第一步: while()循环, 控制循环次数小数的位数, 防止无限循环和出现多位小数;

第二步: 通过分解平方根, 使用循环, 逐渐减小,接近平方根;


同理, 其他方根也可以类似扩展, 不过需要注意的是, 

偶数次方根需要确保输入正数;

奇数次方根需要转换为正数, 确保循环收敛, 再进行结果正负判断;


代码如下:

/*  * 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