float x if statement compared to "zero value"

Write an if statement that compares float x with "zero value"

Please write an if statement comparing float x with "zero value": 
const float EPSINON = 0.00001;  
if ((x> =-EPSINON) && (x <= EPSINON) Do  
not use "==" or "!" For floating point variables = "Compared with numbers, you should try to convert it into"> = "or" <= ". 

EPSINON should be a very small value. Because the computer has an error when processing floating point numbers, so judge the two floats Whether the points are the same, it is necessary to judge whether they fall in the same interval, this interval is [-EPSINON, EPSINON] EPSINON is generally very small, below 10 to the -6th power, the specific seems to be uncertain, and it is related to the machine

[Conclusion] The
equivalent comparison of floating-point numbers uses the following formula:
#include 
#include 
fabs (a-b) <FLT_EPSILON

three EPSILON:
FLT_EPSILON
DBL_EPSILON
LDBL_EPSILON


Why ca n’t floating-point numbers be directly used for "equivalent comparison"?
I have known this before reading books or articles. I know it is because of "precision", but I have never really thought about the severity of the problem.

The following content is quoted from Lin Rui's "Guide to Writing High-Quality C / C ++ Code"

4.3.3 Compare floating-point variables with zero values
? [Rule 4-3-3] Do not compare floating-point variables with "==" or "! =" To any number.
It must be noted that no matter whether it is a float or a double type variable, there is a precision limit. Therefore, we must avoid comparing floating-point variables with “==” or “! =” To numbers, and we should try to convert them into “> =” or “<=” forms.
Assuming the name of the floating-point variable is x,
if (x == 0.0) // implied error comparison should be 
converted to 
if ((x> =-EPSINON) && (x <= EPSINON))
where EPSINON is the allowed error (Ie precision).




It is best to define a symbolic constant to do it. #define EPSINON 1e-6


I think it's because floating point numbers have a large range. If a number is already very small, it can be regarded as 0, epsinon, limit, limit or something.
Also think about it, is 0.9 infinite loop equal to 1?
If a certain value is equal to 0.9 cycles, the floating point number can only give a "determined" value, it will "do the wrong thing".

I think the principle is about this. Specifically, I haven't figured it out yet :)

After searching again, write the following paragraph:


Well, I finally found an incomplete answer. Anyway, I will never compare floating point numbers directly again:
see the post: http://blog.joycode.com/joe/archive/2004/12/07/40592.aspx

I wrote this example with reference to this article:
#include <stdio.h>
#include <stdlib.h>

main()
{
    float d1, d2, d3, d4;

    d1 = 194268.02;
    d2 = 194268;
    d4 = 0.02;
    
    d3 = d1 - d2;
    if (d3 > d4)
       printf(">0.02/n");
    else if (d3 < d4)
       printf("<0.02/n");
    else
       printf("=0.02/n");    

    printf("%f - %f = %f /n", d1,d2,d3);

    system ("pause");
}

Please see the result:
<0.02
194268.015625-194268.000000 = 0.015625

, ie: 194268.02-194268.0 is not equal to 0.02!
The number stored in it will change! Are you afraid? Change the

four variables to double type, and then test:
this is the result
<0.02
194268.020000-194268.000000 = 0.020000 is
clearly 0.02, why is it less than?
Did n’t I change the number I saved this time? WHY?

I said, I'm scared, and I don't dare to use floating point numbers to compare equality directly!

Still the same sentence: floating-point numbers are limited in precision.
So the number you save is not necessarily the number you want.

Although this matter is very depressing, but I am still very happy to know something.


About EPSINON, it can't be defined casually!
And you should be able to think that the EPSINON of double and float are different.
What is defined as? You don't need to define it. ANSI C has defined these constants: you can refer to FLT_EPSILON DBL_EPSILON LDBL_EPSILON constants by
loading the header file
#include  .






 

Published 115 original articles · Like 29 · Visitors 50,000+

Guess you like

Origin blog.csdn.net/huabiaochen/article/details/100113665