Posted on 2007-06-30 21:27
停留的风 阅读(469)
评论(0) 编辑 收藏 所属分类:
C语言学习历程
#include <stdio.h>
float absoluteValue(float x)
{
if(x<0)
{
x=-x;
}
return x;
}
float squareRoot(float x)
{
const float epsilon= 0.0001;
float guess=1.0;
if(x<0)
{
printf("Negative argument to squareRoot.\n");
return -1.0;
}
while (absoluteValue((guess*guess)-x)>=epsilon)
{
guess=(x/guess+guess)/2.0;
}
return guess;
}
int main(void)
{
float number;
printf("Enter an float number!\n");
scanf("%f",&number);
printf("The squareRoot of number %f is %f.\n",number,squareRoot(number));
return 0;
}