36 lines
827 B
C
36 lines
827 B
C
|
/*
|
|||
|
Program: binary-search-square-root.c (Report comments/bugs to chikh@yuntech.edu.tw)
|
|||
|
Function:
|
|||
|
*/
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <math.h>
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
int i;
|
|||
|
float x;
|
|||
|
double a = 0, b, c;
|
|||
|
|
|||
|
printf("\n*** <20><><EFBFBD>Jx<4A><78>(x>0)<29>p<EFBFBD><70><EFBFBD>䥭<EFBFBD><E4A5AD><EFBFBD><EFBFBD> ***\n\n<EFBFBD><EFBFBD><EFBFBD>Jx => ");
|
|||
|
scanf("%f",&x);
|
|||
|
b = (x < 1)? 1: x;
|
|||
|
|
|||
|
printf("\n<EFBFBD>G<EFBFBD><EFBFBD><EFBFBD>j<EFBFBD>M<EFBFBD>k<EFBFBD>D<EFBFBD>ѹL<EFBFBD>{<7B>p<EFBFBD>U ...\n\n<EFBFBD><EFBFBD><EFBFBD>N <20>϶<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n"
|
|||
|
"----+----------------------------------+----------------\n");
|
|||
|
for (i = 0;;) {
|
|||
|
c = (a+b)/2;
|
|||
|
printf("#%-2d [%.10f,%.10f]\t<EFBFBD><EFBFBD>=%.10f\n",++i,a,b,c);
|
|||
|
if (fabs(c*c-x) < 1e-10) break;
|
|||
|
if (c*c < x)
|
|||
|
a = c;
|
|||
|
else
|
|||
|
b = c;
|
|||
|
}
|
|||
|
printf("\n[<5B>G<EFBFBD><47><EFBFBD>j<EFBFBD>M<EFBFBD>k] sqrt(%g)=%.10f<EFBFBD>A<EFBFBD>۸<EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><EFBFBD><EFBFBD>ب<EFBFBD><EFBFBD>Ƹѻ~<7E>t%.1f%%\n",x,c,fabs(c-sqrt(x))/sqrt(x)*100);
|
|||
|
|
|||
|
system("pause");
|
|||
|
return 0;
|
|||
|
}
|