Jump to content
Porter

Floating point almost equal

Recommended Posts

The other day I encountered a situation where I wanted to test if two floating point values were equal. Of course, one of the first things that you are told about floating point values is that you should never test for exact equality. So I proceed to wire up:

 

Is X = Y +/- some acceptable tolerance?

 

And that worked perfectly for my use case. That is, I knew that the max. value was 1000 and the min value was 0 and the resolution (or increment) was 0.01. A tolerance of 0.001 was sufficient.

 

This got me thinking. Wouldn't it be nice to have some reusable VIs for testing if floating point values are almost equal? Unfortunately this meant taking a broader view of the problem and lead me to this interesting blog post:

 

http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

 

I've attached VIs for the Absolute Epsilon comparison, Relative Epsilon comparison and the ULP comparison described in the blog post for double precision floating point values.

 

post-40255-0-58730200-1416369553.png

 

Perhaps this would be a nice addition to the somewhat barren OpenG Comparison Pallet?

 

Uploaded package to Code Repository: http://lavag.org/files/file/259-floating-point-almost-equal/

AlmostEqual_LV2013.zip

AlmostEqual_LV86.zip

Edited by Porter

Share this post


Link to post
Share on other sites

Altenbach has a version, MGI has a version, and I've seen several other implementations of it.  There also exists a primitive in LabVIEW that isn't on the palette that does this, but several NI employees have said it is not stable and not to use it.

Share this post


Link to post
Share on other sites
Thanks for the info.

 

MGI's solution looks somewhat like the relative epsilon solution proposed in Bruce Dawson's blog post however they use a fixed maximum relative difference of 1%. Perhaps in some cases that would be too high.

 

Altenbach's version looks identical to the AlmostEqual2sComplement() function described in another article by Bruce Dawson:


 

Notice at the top of that article he states that you should not be using that method of comparison as it has a couple of major flaws. For example, comparing 2 and -2 will always return true. 

 

I have added the relative epsilon comparison to the package for completeness. This way, you can try all 3 methods if you are interested. Most of the time I really only need the absolute epsilon comparison because I know beforehand what range of values I'm comparing.

Share this post


Link to post
Share on other sites

Added support for SGL and EXT types. Uploaded package to Code Repository: http://lavag.org/files/file/259-floating-point-almost-equal/

Nice idea, but please don't use pale-yellow for addons.

The pale-yellow background color makes it easily mistaken to be a primitive in LabVIEW, and I think NI also recommends against it.

 

/J

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Similar Content

    • By Taylorh140
      So I have a serious question. Does LabVIEW have a way (built in or library) of calculating the next larger or next smaller floating point value.
      C standard library "math.h" has two functions:
      nextafter, and nexttowards.
      I have put together a c function: that seems to do the trick nicely for single floats (well only for stepup):
      #include <math.h> #include <stdint.h> uint32_t nextUpFloat(uint32_t v){ uint32_t sign; = v&0x80000000; if (v==0x80000000||v==0){ //For zero and neg zero return 1; } if ((v>=0x7F800000 && v<0x80000000)||(v>0xff800000)){ //Check for Inf and NAN return v; //no higher value in these cases. } sign = v&0x80000000; //Get sign bit v&=0x7FFFFFFF; //strip sign bit if(sign==0){ v++; }else{ v--; } v=v|sign; //re merge sign return v; } I could put this in labVIEW, but these things are tricky and there are some unexpected cases. So its always better to use a reference. 
       
    • By Porter
      This package contains VIs for testing equality of floating point numbers within a specified tolerance. Absolute Epsilon comparison, Relative Epsilon comparison and the ULP comparison have been implemented as described in this blog post by Bruce Dawson: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ Note that the ULP comparison has not been implemented for extended precision floating point type. Installation and instructions: Install VIP package using VI Package Manager 2014. Examples: "<LabVIEW>\examples\LAVA\AlmostEqual\AlmostEqual_Example 1.vi" Demonstrates the usage of all three AlmostEqual functions. "<LabVIEW>\examples\LAVA\AlmostEqual\AlmostEqual_Example 2.vi" Demonstrates a case where exactly equal fails to produce the expected result.   Development version available on GitHub: https://github.com/rfporter/FP-Equal
    • By Porter
      View File Floating Point Almost Equal
      This package contains VIs for testing equality of floating point numbers within a specified tolerance. Absolute Epsilon comparison, Relative Epsilon comparison and the ULP comparison have been implemented as described in this blog post by Bruce Dawson: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ Note that the ULP comparison has not been implemented for extended precision floating point type. Installation and instructions: Install VIP package using VI Package Manager 2014. Examples: "<LabVIEW>\examples\LAVA\AlmostEqual\AlmostEqual_Example 1.vi" Demonstrates the usage of all three AlmostEqual functions. "<LabVIEW>\examples\LAVA\AlmostEqual\AlmostEqual_Example 2.vi" Demonstrates a case where exactly equal fails to produce the expected result.   Development version available on GitHub: https://github.com/rfporter/FP-Equal Submitter Porter Submitted 12/19/2014 Category General LabVIEW Version  
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.