10-21-2015 08:41 PM
Dear NI Engineers
I just discovered a strange phenomenon of labVIEW, code as below:
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!
10-21-2015 08:50 PM
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. 😉
10-21-2015 09:53 PM - edited 10-21-2015 10:00 PM
@daed wrote:
Dear NI Engineers
I just discovered a strange phenomenon of labVIEW, code as below:
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.
10-21-2015 11:10 PM
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.
10-22-2015 01:40 AM - edited 10-22-2015 01:41 AM
10-22-2015 02:53 AM
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.
10-22-2015 07:16 AM - edited 10-22-2015 07:22 AM
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.
10-22-2015 07:50 AM - edited 10-22-2015 07:50 AM
@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.
10-22-2015 07:50 AM
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!!!
10-22-2015 07:53 AM
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.