LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

-0.002 and -0.002 are not equal

Solved!
Go to solution

@littlesphaeroid wrote:

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. 


That's my quick n dirty favorite method.  Really multiply both by 10^x where x is the number of places to compare to and then do your casting.  Negative powers give you places on the left e.g., 10^-2 gives you comparisons to the nearest 100.  And 10^0 gives you comparisons to the nearest integer.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 11 of 29
(324 Views)

Thank you for the responses. I've realized what I had overlooked. My question now is: what would be the most efficient way to perform this comparison? I tested converting to a String and then back to a Double, and it appears to be the fastest method. Are there any other approaches you would recommend? I also tried GPower's "IsEqualize" function, which gives accurate results as well. Any advice would be greatly appreciated.

0 Kudos
Message 12 of 29
(288 Views)

Number to string and back to number being the fastest method? If you mean fastest in terms of getting some result that works most of the time maybe. But efficient in terms of CPU power it definitely is not!

Rolf Kalbermatter
My Blog
0 Kudos
Message 13 of 29
(284 Views)

 

I know it doesn’t seem logical, but that’s what my tests have shown. Beyond finding a solution, I would also appreciate advice on comparing two pieces of code that perform the same operation. Currently, I measure runtime by recording the start and end times and calculating the difference. According to these measurements, the conversion method appears to be the fastest.

0 Kudos
Message 14 of 29
(276 Views)

We learned in engineering college: "Wer misst misst Mist"

 

That is German and means roughly translated: "When you measure you measure shit"

This is generally true with every form of measurement unless you are extremely careful and understand exactly all the possible inaccuracies, potential pitfalls and measurement errors.

 

It is double as much true when measuring runtime performance of software code! Our resident specialist on benchmarking software in general and LabVIEW code in special is Christian Altenbach. I haven't seen him reviewing any benchmark VI and not having at least a few remarks about why that benchmark doesn't measure what the original developer thought he was measuring. 😁

Rolf Kalbermatter
My Blog
Message 15 of 29
(268 Views)

@maxnoder1995 wrote:

Thank you for the responses. I've realized what I had overlooked. My question now is: what would be the most efficient way to perform this comparison? I tested converting to a String and then back to a Double, and it appears to be the fastest method. Are there any other approaches you would recommend? I also tried GPower's "IsEqualize" function, which gives accurate results as well. Any advice would be greatly appreciated.


See the link in my previous post.   That attached code is as peer reviewed and documented as possible (including a trip through a thread on the Champion board to get feedback when I used to be a non-emeritous LabVIEW Champion.)    The linked thread also discusses other approaches to floating point comparisons; 

  • Round to place
  • Subtract and compare to 0
  • Divide and compare to 1 ( if dividend and divisor are nearly equal the result is 1 +/- epsilon)
  • The attached Units in last place algorithm (has trouble with oppositely signed values)

The related documentation also links to other sources directly related to floating point comparisons.   


"Should be" isn't "Is" -Jay
0 Kudos
Message 16 of 29
(233 Views)
Solution
Accepted by maxnoder1995

There is of course the (build in) "floating point compare" function, that we are recommended not to use for undisclosed reasons, although it does exactly what you're looking for:

 

Floating Pont Equal.png

 

wiebeCARYA_0-1732185829693.png

 

Message 17 of 29
(166 Views)

How can I use this function? Can you please attach a VI file that I can open?

0 Kudos
Message 18 of 29
(142 Views)

wiebe@CARYA wrote:

There is of course the (build in) "floating point compare" function, that we are recommended not to use for undisclosed reasons, although it does exactly what you're looking for:

I think that could be my favorite sentence this week.  Helpful, but terrifying at the same time.  Ya feel good, but still have hold of the vomit bag.

0 Kudos
Message 19 of 29
(140 Views)

@maxnoder1995 wrote:

How can I use this function? Can you please attach a VI file that I can open?


Something like this:

FPCompareUsage.png

 

0 Kudos
Message 20 of 29
(126 Views)