LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

-0.002 and -0.002 are not equal

Solved!
Go to solution

Why are the values ​​not equal?

0 Kudos
Message 1 of 29
(577 Views)

I cannot open your VI because it is in LV2024.

Please save your code for previous version (e.g. 20.0) so that most people can open it.

 

I will try to take a guess though:

 - Right-click your numeric constants or controls > "Display Format"

 - Increase the number of significant digits e.g. to 18

 - Are they still the same?

 

Checking equality on floating-point numbers is generally a bad practice.

Use a range check instead (e.g. with "In Range And Coerce").

 

Regards,

Raphaël.

 

Message 2 of 29
(571 Views)

Didn<t open your vi but this is probably a classic. Double are not exact representation of real values, display 20 digits after the decimal point and you will probably see why they are not equal. Do not use the equal sign with double but look for delta value less than epsilon (e.g. 1E-10).

 

Ben

0 Kudos
Message 3 of 29
(537 Views)

@raphschru wrote:

I will try to take a guess though:

 - Right-click your numeric constants or controls > "Display Format"

 - Increase the number of significant digits e.g. to 18

 - Are they still the same?

 

Checking equality on floating-point numbers is generally a bad practice.

Use a range check instead (e.g. with "In Range And Coerce").


I just did this for you.

 

As @raphschru stated, direct comparisons with floating point numbers is an exercise in futility. This is not a LabVIEW issue; this is a computer science issue. More specifically, it is IEEE 754. Instead of a check for equality, do a subtraction between your two values and check if the difference is small enough.


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
Message 4 of 29
(521 Views)

@maxnoder1995 wrote:

Why are the values ​​not equal?


Because they're not.

Expanding to 20 significant digits shows it well.

Yamaeda_0-1731939485423.png

 

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 29
(497 Views)

Aside from the my instrument doesn't answer despite me sending it "<command>" (or sometimes even "<command>\n") this must be probably the second most occurring problem of new LabVIEW users.

 

And usually they are quite upset and claim it is a bug! 😁 Except it isn't, as has been already explained before. Wonder how that could be made more prominent. It would seem quite basic knowledge that should be part of any programming class. But it is probably considered to much into details of computer science to really teach that anymore. 

 

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 29
(496 Views)

Read through this Wiki if you want an explanation of this computer programming issue:  Machine epsilon - Wikipedia

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 7 of 29
(470 Views)

@rolfk wrote:

 

And usually they are quite upset and claim it is a bug! 😁 Except it isn't, as has been already explained before. Wonder how that could be made more prominent. It would seem quite basic knowledge that should be part of any programming class. But it is probably considered to much into details of computer science to really teach that anymore. 

 


I don't recall them going into binary representation of numbers in core 1/2... but that was over a decade ago 😉

~ Self-professed LabVIEW wizard ~
Helping pave the path to long-term living and thriving in space.
0 Kudos
Message 8 of 29
(437 Views)

I've got a lvlib of polymorphic vis that handles floating point comparisons with a Units in last place algorithm that works very well and has excellent documentation. A search for that attachment should turn it up.

 

EDIT: Found it here 


"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 29
(426 Views)

Another way to do this comparison is to multiple by some factor (in your case, 1000) to then cast to (signed) integers (both will be -2), and compare those. 

_____________
Creator of the BundleMagic plugin for LabVIEW!
Message 10 of 29
(394 Views)