LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

While loop doesn't stop when two values are equal using equal-to comparitor

Solved!
Go to solution

Hi everyone,

 

I have a really, really odd bug. I have a LabVIEW VI which changes the voltage of a channel on a power supply unit. I have a start voltage and a stop voltage and use a while loop to increment the device. E.g. if I want to scan from 1.2 V to 2.2 V in 0.2 V incremements the program will end when "Current Voltage" = "Stop Voltage". And this works fine!

 

However, when I start -3 V and want to stop at say -0.8 (again in 0.2 V incremements) the program does not stop when "Current Voltage" = "Stop Voltage". I have checked with the probe near what should be the end of the run and -0.8 V is going in both of the "Equal-to" comaprison operator inputs, but this deson't trigger a true result.

 

This is very strange to me. Espeically as if I go from -0.8 V to -2 V but decrement by -0.2 V the program stops correctly!!

 

I am very puzzled!!

 

Cheers!

0 Kudos
Message 1 of 10
(4,417 Views)
Do not ever, Ever, EVER use an equal comparator on a floating point number. This is not a bug, it's how floating point numbers work. Rather than me explaining this for the 9423rd time, it would be more useful for you to search the forum and find your own answer.

Hint: The example where it worked was a fluke.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 2 of 10
(4,412 Views)

Ahhh, okay!! I did try searching the forum for a similar problem but couldn't find a similar problem. If you know of a relevant thread could you point me to it? Thanks!

0 Kudos
Message 3 of 10
(4,407 Views)
Solution
Accepted by quantisedpenguin
Do a search on: Comparing floating point numbers

The second thread is particularly relevant. That discussion was from 2009, but you can find the same "bug" being reported going back to the late 1980s.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 4 of 10
(4,396 Views)

Would I be correct in thinking that I should use one of the conversion fucntions like "fixed point precision"?

 

Cheers

0 Kudos
Message 5 of 10
(4,394 Views)

For anyone who stumbles across this post:

 

http://forums.ni.com/t5/LabVIEW/When-I-subtract-two-DBL-numbers-and-try-to-compare-the-result/m-p/96...

 

Thanks to Mike for the pointer!

0 Kudos
Message 6 of 10
(4,384 Views)

@quantisedpenguin wrote:

Would I be correct in thinking that I should use one of the conversion fucntions like "fixed point precision"?


No.  Just use the In Range And Coerce to look for a range.  Since you are going in steps of 0.2V, check to see if you are in the range of your final voltage +-0.05V.  So if your final voltage is 2, look for the value to be in the range of 1.95 and 2.05.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 10
(4,367 Views)

Thanks for the help. what is the best way to make this robust for any current voltage and any stop? The voltage supply is only capable of 1 mV precission so I should use somethink like Range and coerce with input of (Stop voltage + 0.0001) for upper limit and (Stop voltage - 0.0001 for lower limit)?

 

I attach what I think the solution should be!

 

Cheers!

0 Kudos
Message 8 of 10
(4,344 Views)

You should do something more like this.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 9 of 10
(4,321 Views)

The problem with these solutions is the fact that they won't do any good if e.g. the voltage changes very rapidly and one pont is still below, but the next point is already above the termination condition. (These things can happen in the presence of poorly chosen inputs (e.g. increment of 0.3, stop at 1), noise or hardware malfunction.) For example if you are ramping up from zero volts and want to stop at 2V, but the successive measurements are 0.4V, 1,2V, 1.9V, 2.2V, ....1000V, it will never stop and possibly fry the device. Ramping up, you need to stop at ~equal or larger, and ramping down, you need to stop at ~equal or smaller. Make it bulletproof!

 

Looking at your question, it seems that your issue does not involve any measurement and is purely mathematical. The correct solution would be to reformulate the problem and calculate the number of steps instead. The number of voltage steps can be calculated from first principles form your inputs and since it is an integer, there are no resolution issues to deal with. You could even pre-calculate all voltages as an array (look e.g. at "ramp pattern.vi"), then you can simply autoindex on a FOR loop and there is not even a need to inspect any termination conditions ever.

 

 

Message 10 of 10
(4,310 Views)