#include<stdio.h>
#include<math.h>
#include<float.h>
void Fun(double a,double b,double c)
#define EPS 0.00001
{
double x1;
double x2;
double d=b*b-4*a*c;
if(-EPS<=a &&a <=EPS)//a==0
{
x1=x2=-c/b;
printf("x1=%f,x2=%f\n",x1,x2);
}
else
{
if(d>EPS)//d>0
{
x1=(-b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
printf("x1=%f,x2=%f\n",x1,x2);
}
else
{
if(-EPS<=d&&d<=EPS)//d<0
{
x1=x2=-b/2*a;
printf("x1=%f,x2=%f\n",x1,x2);
}
else//d<0
{
printf("无实根\n");
}
}
}
}
int main()
{
Fun(0,1,2);
Fun(1,4,1);
return 0;
}
浮点数的比较,浮点数存在误差
最新推荐文章于 2024-03-12 17:30:54 发布