平方根程序 [英] program of square root
本文介绍了平方根程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何制作不包含库的平方根程序
how to make a program of square root without including library
推荐答案
Hi
尝试下面的代码
//平方根逼近-巴比伦方法
double sqroot(const double s){
Hi
Try the below code
// Square root approximation - Babylonian method
double sqroot(const double s) {
double xn = s/2.0;
double lastX = 0.0;
//以这种方式循环执行可确保我们仅执行所需的尽可能多的计算.
//如果您确实愿意,可以将其替换为静态的for循环.
while(xn!= lastX){
double lastX = 0.0;
//以这种方式循环执行可确保我们仅执行所需的尽可能多的计算.
//如果您确实愿意,可以将其替换为静态的for循环.
while(xn!= lastX){
double xn = s / 2.0;
double lastX = 0.0;
// Looping this way ensures we perform only as many calculations as necessary.
// Can be replaced with a static for loop if you really want to.
while(xn != lastX) {
double lastX = 0.0;
// Looping this way ensures we perform only as many calculations as necessary.
// Can be replaced with a static for loop if you really want to.
while(xn != lastX) {
lastX = xn;
xn =(xn + s/xn)/2.0;
xn =(xn + s/xn)/2.0;
lastX = xn;
xn = (xn + s/xn) / 2.0;
xn = (xn + s/xn) / 2.0;
}
返回xn;
}
return xn;
}
}
看看http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Have a look at http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
这篇关于平方根程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文