11-18-2024 06:52 AM
Why are the values not equal?
Solved! Go to Solution.
11-18-2024 07:05 AM
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.
11-18-2024 07:50 AM
11-18-2024 08:01 AM - edited 11-18-2024 08:02 AM
@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.
11-18-2024 08:18 AM
@maxnoder1995 wrote:
Why are the values not equal?
Because they're not.
Expanding to 20 significant digits shows it well.
11-18-2024 08:19 AM
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.
11-18-2024 09:12 AM
Read through this Wiki if you want an explanation of this computer programming issue: Machine epsilon - Wikipedia
11-18-2024 11:30 AM
@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 😉
11-18-2024 12:07 PM - edited 11-18-2024 12:13 PM
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
11-18-2024 04:13 PM
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.