码迷,mamicode.com
首页 > 其他好文 > 详细

递归练习之求平方根

时间:2014-11-25 00:23:42      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   ar   os   sp   on   2014   log   

/*********************************************************************************  
 Copyright (C), 1988-1999, drvivermonkey. Co., Ltd.  
 File name:   
 Author: Driver Monkey  
 Version:   
 Mail:bookworepeng@hotmail.com  qq:196568501
 Date: 2014.04.02  
 Description:   递归练习之求平方根
 *********************************************************************************/  

#include <iostream>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <memory.h>
#include <thread> 
#include <stdlib.h>     /* labs */
#include <math.h>

using namespace std;

static double square_root(double x, double guess);
static double average(double value1, double value2);

int main()
{
    cout<<"square_root = " <<square_root(2, 1.5)<<endl;

    while(1);
    return 0;
}



static double square_root(double x, double guess)
{
    double quotient = x / guess;

    double average_value = average(quotient, guess);
    if(fabs(average_value -guess) < 0.000000000000000001L)
    {
        return guess;
    }else
    {
    	return square_root(x, average_value);
    }
}

static double average(double value1, double value2)
{
    return (value1 + value2) / 2;
}

递归练习之求平方根

标签:des   blog   io   ar   os   sp   on   2014   log   

原文地址:http://blog.csdn.net/drivermonkey/article/details/41457273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!