開平方根

 avatar
user_3763047219
c_cpp
2 years ago
489 B
1
Indexable
Never
#include <iostream>
#include <stdio.h>
#define _CRT_SECURE_NO_WARNING
int main()
{
	double n = 3.0;
	double error = 0.00001;
	double low = 0.0, high = n;

	double m = (low + high) / 2.0;
	while ((m * m - n) > error || (n - m * m)>error) {
		if (m*m < n) {
			low = m;
		}
		else
			high = m;
		m = (low + high) / 2.0;
	}
	printf("m = %9.8f \t error = %9.8f\n", m, m*m - n);
	//總共9個數 小數點後面有8位
	// 0    1.5    m    3
	// n=3.0    m=1.72   2.9584
}