LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Something strange about the"Search 1D Array" function,if there is some kind of bug of LabVIEW?

Dear NI Engineers

 

I just discovered a strange phenomenon of labVIEW, code as below:

 

001.PNG

 

When I search element as 5.2, 7.8, this Index will show -1 which means data not found. This is very strange because the data is just in the array.

Please help to know if there is some kind of bug of LabVIEW, thank you!

 

 

0 Kudos
Message 1 of 25
(4,134 Views)

Not strange.  You are using floating point numbers.  It is highly likely that the two numbers are NOT actually equal.  Google "IEEE floating point numbers" so I don't have to type a few paragraphs explaining.  😉

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 2 of 25
(4,119 Views)

@daed wrote:

Dear NI Engineers

 

I just discovered a strange phenomenon of labVIEW, code as below:

 

001.PNG

 

When I search element as 5.2, 7.8, this Index will show -1 which means data not found. This is very strange because the data is just in the array.

Please help to know if there is some kind of bug of LabVIEW, thank you!

 

 


This is NOT a bug!

 

Not all real numbers can be exactly represented in binary!  (IEEE 754 Standard)  There are only so many bits to try to represent real numbers in your computer that only knows how to interperet a bunch of 1's and 0's.  Comparing Floating point numbers is always a challenge.  you need to see if they are "Cose Enough" to equal.  

See here

 

 


"Should be" isn't "Is" -Jay
0 Kudos
Message 3 of 25
(4,100 Views)

Thanks, Jeff.

 

now I know that "the element to search" and the one in the "Array" are not exactly equal because they are all DBL format, is this right?

But how should I use the"Search 1D Array" function if my "1D Array" and "element" are all decimal

 

Looking forward to your reply.

0 Kudos
Message 4 of 25
(4,071 Views)
Try to use "Threshold 1D Array" If search element is not exists in array, it gives nearest fraction Index.
Munna
0 Kudos
Message 5 of 25
(4,043 Views)

But how should I use the"Search 1D Array" function if my "1D Array" and "element" are all decimal

 

Looking forward to your reply.


Just as you showed. If your numbers represented in LV as I32 types, this search function will work, try your own VI but change everything into I32 from DBL.

0 Kudos
Message 6 of 25
(4,023 Views)

Basically you have to multiply both sides by the same power of ten as the number of decimal places you want to compare to, round to nearest integer and then do your search working with integers.

 

Someone else had a method that seemed easier than that and they used strings, but I couldn't remember what they did.

 

Oh and you can multiply the whole array at once.  You don't have to index each element and multiply separately.

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.
0 Kudos
Message 7 of 25
(3,983 Views)

@Munna232 wrote:
Try to use "Threshold 1D Array" If search element is not exists in array, it gives nearest fraction Index.

This isn't going to work if you have instances where the next element is less than the one before it.  I guess you could sort the elements is ascending order, though.

 

I think that comparing integers is the better way to go because it is scalable and you can exactly the same comparison that you wanted to do in the first place.

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.
0 Kudos
Message 8 of 25
(3,963 Views)

 Hello Daed,

 

Yes I guess you will have trouble with floating point numbers(dbl) like mentioned before, but one trick would be to convert to fractional string and then do search function.

 

Hope it works for you!

 

God Bless!!! 

 

 conv to string.png

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding. Proverbs 4:7
0 Kudos
Message 9 of 25
(3,960 Views)

If you know the number of digits of precision you need (e.g. currency is 2 digits after the decimal place), then you can multiply by a factor of 10, convert to an integer, do the search and then divide back by the same factor of 10.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 10 of 25
(3,950 Views)