LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

My vi proves that 5 does not equal 5! What did I do wrong?

Solved!
Go to solution
I’m trying to debug a sub-vi that tells me 5 is not equal to 5.  It insists that 5 is less than 5.

When you run the program, it pulls one element, “Period” out of a cluster each pass through the loop.   If the increment button is pressed, it compares the current value of Period with the upper limit (set to 5 in this example).  In theory, if Period is less than the upper limit,but not equal or greater, the code will add 0.1 and put the result back in the cluster.  Things don't get much simpler!

Similarly, decrement will subtract 0.1 from Period provided the input value is larger than the lower limit (1).

However, this code will execute the increment case when the input value of Period is 5, resulting in an output value of 5.1.  I’ve turned on Highlight Execution, added probes to the inputs and outputs of the less-than function, and put a break point after the increment case structure to catch the misbehavior.

I even added the x=y? indicator to prove to my coworkers that this code does not think 5=5.  Do you see what I’m doing wrong?  Thanks in advance for any and all enlightenment!

Jeff
0 Kudos
Message 1 of 4
(2,998 Views)
Solution
Accepted by topic author jstevens

What you did wrong is not understand the nature of floating point numbers.

 

Please see HERE for more information.

 

Equality checks on processed floating point numbers is a no-no.

 

Shane.

Message 2 of 4
(2,995 Views)

Thank you!  I guess this underscores the danger of letting non-software types like me write code.  I'll probably end up making every mistake they warn you about in school. Smiley Happy

 

Jeff

Message 3 of 4
(2,986 Views)

Tell me about it.

 

I didn't learn programming in school.  I learned all these "nasty" things the hard way.  Just like you did on this topic.  Takes longer, but you never forget what you've learned.....

 

Shane.

Message 4 of 4
(2,977 Views)