Comparing Floating Points Does Not Seem to be Working ProperlyPrimary Software: LabVIEW Development Systems>>LabVIEW Full Development System Primary Software Version: 7.1 Primary Software Fixed Version: N/A Secondary Software: Measurement Studio, LabWindows/CVI Development Systems, LabVIEW Development Systems
Problem: I am comparing two floating points in LabVIEW, LabWindows/CVI, Measurement Studio, or any programming language that supports floating points. Even though the numbers appear to be the same, the comparison returns false. For example, in LabVIEW I have three front panel numeric controls (floatingpoint representation): one contains a value of 0.62, the second has 0.60, and the third 0.02. When I subtract the .60 from the .62 and compare it with the .02, LabVIEW tells me that they are not equal. What is happening? Solution: This problem is not inherent to LabVIEW, CVI, Measurement Studio, or any other language but is common among all programming languages that support floating points. It arises because of the way floating point numbers are represented. Floating points are actually represented to a very high precision in binary. Because of this, numbers that appear to be the same could be slightly different. In our LabVIEW example, if you increase the precision of these numbers, you can see that about 15 or 20 digits after the decimal point the two numbers are not really equal. You can also flatten these numbers to a string and display them as hexadecimal numbers. You will see that the difference and the third number are off by about 4 bits. Therefore, technically the numbers are not equal. If you need to compare floating points, one good work around is to decide on a certain number of digits after the decimal point (x), multiply your numbers by 10^x, convert the result to an integer, and then do the comparison. See the example below for an illustration of this. When the code below is run with certain values, the Expected = to Actual Value? indicator will be false, even if the Double Subtraction Expected Result input is the same number. After the scaling done however, the Equal After Adjustment? indicator will returns true.
This example code is attached below. Also, there is a SubVI attached which can be placed into any project to make comparisons between doubles. The zip file contains LabVIEW 2013 and LabVIEW 2009 versions of both files. Related Links: LabVIEW 2010 Help: FloatingPoint Numbers Developer Zone Tutorial: An Introduction to FloatingPoint Behavior in LabVIEW KnowledgeBase 4VUFUS00: Why does my Equal Comparison Not Work with Floating Point Numbers? Attachments: Double Precision Examples and SubVIs.zip
Report Date: 07/30/1998 Last Updated: 02/17/2014 Document ID: 1BTCIC0W
