Unit Testing Group

cancel
Showing results for 
Search instead for 
Did you mean: 

Testing if two floating point numbers are "equal"

I just came across this package for testing if two floating point numbers are almost equal and I thought this group would find it useful.

https://lavag.org/topic/18712-cr-floating-point-almost-equal/

When I started doing unit testing, I found that sometimes two numbers that appeared to be equal would give false results. The Unit Test Framework offers a setting to select how many decimals need to be used to make the comparison. In the Advanced section of the lvtest, the default for "Digits of precision for floating-point comparison" is set to 6. There are also options for the different comparison methods that can be used, including:

equal, range, symmetric tolerance, asymmetric tolerance and user-defined. For the user-defined option, one can give the path of the VI to be used.

The JKI VI Tester doesn't have such a feature, so when comparing two floating point numbers what I do is make the comparison directly in the Test VI and then use the passIf.vi to report the result to the VI Tester.

In either case, it seems like the VIs in this package can be used to compare two floating point numbers.

What other tips/tricks do you guys have for dealing with comparison of floating point numbers?

Regards,

Fab

For an opportunity to learn from experienced developers / entrepeneurs (Steve, Joerg, and Brian amongst them):
Check out DSH Pragmatic Software Development Workshop!

DQMH Lead Architect * DQMH Trusted Advisor * Certified LabVIEW Architect * Certified LabVIEW Embedded Developer * Certified Professional Instructor * LabVIEW Champion * Code Janitor

Have you been nice to future you?
0 Kudos
Message 1 of 5
(7,077 Views)

I didn't know this addon existed in the repository - we have rolled our own previously (our in-house unit test framework uses this internally and has a "Assert Numerics Approximately Equal" test method). That'll teach me for not turning to lava first. I also wasn't aware of the different comparison strategies available in the UTF (shows how much I use it); definitely handy. Makes me wish it was faster and more manageable so I could swap over to it full-time

The number of floating point comparison bugs I've found ... I make it a point now that anywhere I compare two floating point numbers I delegate to our internal library instead of the equals primitive.

However I thought that one of the VI Tester assertion Equal methods had an option for floating point comparison ("delta") that was used only when the input variant types were numeric. I don't have it installed at the moment to verify this.

EDIT: I think this is the one.

vi.png

0 Kudos
Message 2 of 5
(5,812 Views)

Out of interest here is the one we use from our own rolled framework GUnit:

vi.png

0 Kudos
Message 3 of 5
(5,812 Views)

You are correct, the delta works as an "epsilon" definition. I had forgotten about this. For certain cases, you can even use the constant "Machine Epsilon" found in the palettes.

One caveat is that it only works if x and y are scalar. It does not work for arrays of floating points. In that case, one still has to roll a custom version.

For an opportunity to learn from experienced developers / entrepeneurs (Steve, Joerg, and Brian amongst them):
Check out DSH Pragmatic Software Development Workshop!

DQMH Lead Architect * DQMH Trusted Advisor * Certified LabVIEW Architect * Certified LabVIEW Embedded Developer * Certified Professional Instructor * LabVIEW Champion * Code Janitor

Have you been nice to future you?
0 Kudos
Message 4 of 5
(5,812 Views)

No API is perfect right?

When we do bulk floating-point comparisons (such as for unit tests) we do them via a For loop and an "Almost Equals" method such as in the addon and then pass the resulting array to our "Assert Pass If All True" test method. We added this assertion to our own kit when we noticed this functionality wasn't "out of the box" with VI Tester, although you could easy add your own wrapper for that.

How often do you find yourself rolling your own type of "assertions"? What sort of tests do they perform? Our testing tends not to vary much from the primitives and I'd like to hear more about other people;s experiences.

EDIT: Scratch that, I'll start a fresh topic.

0 Kudos
Message 5 of 5
(5,812 Views)