Unexpected or Inconsistent Numeric Comparison Logic in LabVIEW

Updated Sep 24, 2019

Reported In

Software

  • LabVIEW
  • LabVIEW NXG

Programming Language

  • Python
  • C++
  • Visual Basic .NET
  • C# .NET

Issue Details

  • I am comparing floating-point numeric values in LabVIEW. Sometimes the logic executes as expected and other times it does not. 
  • I am measuring data until a certain values is measured. However, sometimes the comparison function does not recognize that both values (value measured and stop value defined in code) are equal. I am having trouble understand why the issue occurs, and even more why it occurs sometimes only.

Solution

This is a known problem in numerical mathematics. Therefore it is expected in all programming languages. As most floating point numbers do not have an exact representation in binary, comparing them for equality can yield unexpected results.


Floating-point numbers are represented internally as a binary approximation, causing calculations that would be exact in decimal notation to often not be exact in binary as small rounding errors occur in outer decimal places of the values. For example, a computer can and will often compare 0.1 + 0.2 = 0.3 as false, as the binary representation of the equation's both sides could execute to a value only close to the exact value like e.g. 0.3000000000044 (or any number of similar rounded-off variants depending on the machine and compilation order), rendering the equality false.


To avoid this, when using comparison logic with floating-point values, use one of the following methods:
  1. Compare for "greater or equal" or "less or equal" instead for "equal".
  2. Compare while allowing an absolute difference (absolute epsilon).
  3. Compare to within relative epsilon.
  4. Compare to within a given digit of precision by converting the numbers to strings.
  5. Compare using integers.