牛顿迭代法

#include <stdio.h>
#include <math.h>
int main(){
    int a;
    scanf("%d", &a);
    const double ERR_RANGE = 1.0 / (1 << 20);
    double res = a;
    while (fabs(res * res - a) > ERR_RANGE){
        res = (res + a / res) / 2;
    }
    printf("sqrt(a) = %.6lf.\n", res);
    return 0;
}

shiyang6017
164 声望60 粉丝