C語言不使用系統庫函數,使用循環實現求任意正數的開平方,怎麼寫???
float foo(float n)
{
float f1=0.0, f2=n, fm=(f1+f2)/2.0,differ=1.0;
while (differ > 0.0001)
{
if (fm*fm>n) {
f2=fm;
differ=fm*fm-n;
}
else {
f1=fm;
differ=n-fm*fm;
}
fm=(f1+f2)/2.0;
}
return fm;
}
int main()
{
float x = 2.0;
printf("%f", foo(x));
}
1.41426