Monday, November 21, 2016

Floating point comparison operation - its fatal error

The JSF AV C ++ Coding Standard and the MISRA-C Coding Standard prohibit the use of floating-point counters as the loop counter. If you use a floating-point variable as the loop counter, you will inevitably perform a floating-point comparison operation, which makes it impossible for a computer to store and compare exact floating-point values with its inherent limitations. Therefore, if floating point is used in an operation, rounding or discarding problems will inevitably occur.

"Oh, I know that. Who uses it?" I will introduce the actual case.

Example of using a floating-point variable as a loop counter

I searched through KLDP and found one case. The programmer used a floating-point variable as the loop counter, which caused him to struggle with the error for a week.


Some programmers used the floating-point counter in the for statement. Under some assumptions, I have written the code in C ++ to reproduce this situation.

Assumptions 1. Run the FOR loop for 1000 runs
Assumption 2. The programmer does the work with the loop counter


The programmer would have expected 100 to be in i. But what about the result?


The result is not exactly 100. It is a programmer's mistake to perform the floating-point comparison operation in the above code, and it is a representative example that violates the rule of JSF or MISRA-C. Many coding standards, including JSF AV C ++, require that floating-point variables are not directly compared. Floating point datatype is not exactly falling off during the operation. Even if you look at the result of the above code, you can see 99.6991 instead of 99.7 by adding 997 times by 0.1. Here, if the programmer expects true, and if (f32 == 99.7), the comparison is written like this, depending on the system, it may be false.

The above result is an example of the result of GCC(3.2, mingw) on an Intel CPU(Intel Core2 Duo). If you run it on another CPU or compiler, you can not guarantee the same result. This kind of error is hard to find unless you check the value by hanging a breakpoint. If you wrote the code with coding rules in mind, the programmer who wrote the story on KLDP would not have spent a week on debugging.

Floating point comparison method

So what if you need to compare floating-point numbers? Java provides a comparison function in the library. C / C ++ uses floating point precision.










No comments:

Post a Comment