float 与 double类型都是有精度限制的,不能直接拿来与0.0直接比较
求方程a*x*x+b*x+c=0的实数解。
#include<math.h>
void main()
{
float a,b,c,x1,x2,disc;
printf(“input a b c:”);
scanf(“%f %f”,&a,&b,&c);
if(fabs(a)<1e-6) /*精度限制,当满足该条件的时候就认为fabs(a)等于0 */
printf(“The equation has not real root”);
else
if(fabs(disc)<1e-6)
printf(“”The equation has two equal roots:%8.4f”.-b/(2*a));
else
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf(“The equation has distinct real roots:%8.4f”,x1,x2);
}
}