Comparison of floating point numbers with '0'

    For integer data a, to judge whether it is equal to 0, just write if(a == 0) to judge.
    And what about floating point numbers? First we need to understand floating point numbers, let's look at an example:
    We define a data a of type float, assign it to 123456789 and output:
intmain()
{
	float a;
	a = 123456789;
	printf("%f\n",a);
	return 0;
}

    The output is:
       It can be seen from this that the result output by the program is not the value it is assigned. That is to say, there is a certain error in floating-point numbers. And this error is related to its own accuracy.
       First of all, the binary representation of floating-point numbers in computers determines that most floating-point numbers cannot be accurately expressed. Most of today's computers are digital computers, not analog computers. The discrete data expression method of digital computers naturally cannot accurately express most of the data.
       Secondly, the precision of computer floating-point numbers is only 7 bits under the single-precision float type. When performing floating-point operations, this precision often leads to errors between the results of the operations and the actual expected results. Below we use specific examples to understand the problem of accuracy.
       It is known that the actual distance between two buildings is 200m, and a measurement tool with an error range of 1m is used to measure, and 4 sets of data are obtained. They are: 199.3m, 189.7m, 200.3m, 201.5m; according to the allowable range of errors, only the two data of 199.3m and 200.3m are valid. That is to say, when the error range is 1m, we believe that the valid data is within the range of (200-1)m~(200+1)m, that is, 199m~201m.
       In general, if the true value is y, the effective value is z, and the error is x, then (yx) <= z <= (y+x)
       Now that we understand this example, let's look at the comparison of floating point numbers to '0'.
       Under normal circumstances, the C language generally determines the error of floating-point numbers as 0.000001. Therefore, the error can be macro-defined #define EPS 0.000001, followed by several expressions for comparing floating-point numbers with '0':
        Determine whether a is equal to 0: if(-EPS <= a && a <= EPS)
        Determine whether a is greater than 0: if (a > EPS)
        Determine whether a is less than 0: if (a < EPS)
        The following is an example of program application for the above method:
 
void Fun(double a,double b,double c)
{
	double x1;
	double x2;
	double d = b*b - 4*a*c;
	if(-EPS<=a && a<=EPS)
	{
		if(-EPS<=b && b<=EPS)
		{
			printf("This equation has infinite solutions!\n");
		}
		else
		{
			x1=x2 =-c/b;
			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 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
	{
		printf("No real root\n");
	}
}

     The above code is to find the root of the binary linear equation a*x^2+b*x+c=0. When comparing the d data of double type with '0', EPS is used, which makes the program appear rigorous and error-prone. Therefore, when we use floating-point data in the future, we should consider the problem of precision.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325367349&siteId=291194637