LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

strange behavior with greater?

Solved!
Go to solution

I just noticed something strange when comparing two doubles with greater?.

This code returns true somehow:

 

Vinc42_2-1585574678631.png

 

 

I tested it with LabVIEW 2017 and 2019, and also the probes show the same value.

However it doesn't return true for lesser and it only works for 0,1 and 0,3.

I tested a lot  but it doesn't make any sense.

 

Did I miss something or is this really a bug?

Can anyone reproduce this or explain whats going on?

 

0 Kudos
Message 1 of 13
(3,232 Views)
Solution
Accepted by topic author Vinc42

Create indicators on the result of the subtraction.  Then expand the precision of the display on indicator and all the constants to 15, 16 decimal places.  You'll see that 0.3 is not exactly 0.3.  And when you do a subtraction, those last decimal places might mean you are slightly over or slightly under 0.3.

 

It is not a bug, it is just a common issue for any program on any computer system related to doing math on floating point numbers.

 

 

Message 2 of 13
(3,224 Views)

Since a computer only works in binary number in the end, how would you write 0.3?

Instead of the 1, 2, 4 of the integer side, you'd use 'bimals' of 1/2, 1/4, 1/8 and so on.

So, convert 0.3 and tell us the result. 🙂

 

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

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 3 of 13
(3,199 Views)

If you do need to check if 2 doubles are equal, you should not use the the equals the function. You can subtract them, take the absolute value, and see if it is less than a very small number, like the machine epsilon constant.

0 Kudos
Message 4 of 13
(3,174 Views)

@Gregory wrote:

If you do need to check if 2 doubles are equal, you should not use the the equals the function. You can subtract them, take the absolute value, and see if it is less than a very small number, like the machine epsilon constant.


Technically, the OP isn't using an equal function, just the greater than.

 

But they are doing A-B to get C, then comparing to see if C is greater than a constant of C on the block diagram.  sometimes it is, sometimes it isn't depending on the value they used for B.

 

It all relates to the rounding errors in trying to represent decimal values in computer floating point values.

 

The classic example is using old-fashioned BASIC and doing 1 / 3 * 3 and finding out it is no longer equal to 1, but .999999999.....

0 Kudos
Message 5 of 13
(3,162 Views)

A fun calculator to play around with this:

 

https://www.h-schmidt.net/FloatConverter/IEEE754.html

 

Enter "0.3" and see what "Value stored in float" actually is.

Message 6 of 13
(3,156 Views)

@BertMcMahan wrote:

A fun calculator to play around with this:

 

https://www.h-schmidt.net/FloatConverter/IEEE754.html

 

Enter "0.3" and see what "Value stored in float" actually is.


That is a fun tool.  But I do have to state that it is using what we refer to as Single Precision Floating Point (SGL).  Most floating point numbers in modern times is DBL (Double Precision), which uses 64 bits instead of 32.  Regardless, we still have the same issue.


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 13
(3,152 Views)

Another approach might be to multiply the result by ten and convert to an integer before the comparison. 

_____________
Creator of the BundleMagic plugin for LabVIEW!
Message 8 of 13
(3,147 Views)
Solution
Accepted by topic author Vinc42

NI's short explanation.

NI's longer explanation, read about the machine epsilon there, which can be used when comparing floating-point numbers.

Read about this idea. , in the discussion there are some examples how to compare floating point numbers approximately.

Certified LabVIEW Architect
Message 9 of 13
(3,113 Views)

Ah ok. Interesting that I never noticed this before and I didn't find an explanation after a quick google search.

Thanks for the solution and for the link to NI's explanation!

0 Kudos
Message 10 of 13
(3,101 Views)