05-25-2015 06:50 AM
1. I have ot make a VI for checking two nuumber if equal, greater or lesser.
2. Attached is VI.
3. if numbers are equal then print zero, if one is greater than 1 , if its less than print 2.
4. I don't understand how to do it. When I put output of three to output numeric then its error, because we cannot do so.
Solved! Go to Solution.
05-25-2015 06:57 AM
Use just one case structure. Combine the boolean outputs from the comparisons into an array. Change the array to a number. Wire the number to the case selector terminal.
Lynn
05-25-2015 07:54 AM - edited 05-25-2015 07:55 AM
Subtract the numbers and then use the Sign function. The Sign function returns a -1 if negative, 0 if 0, and 1 for positive. Just wire the result into your case selector, make your three cases ("..-1", "0", and "1..") and have them output whatever they should.
05-25-2015 11:30 AM
crossrulz solution is much simpler than mine. The one advantage of mine is that it can be extended to other comparisons, either different types or more of them.
Also, be cautions about equals? comparsions on floating point numbers. Small differences out in the 14th or 15th decimal place due to the way numbers are represented in binary can cause two values which are very close to not be equal. This is especailly noticeable when the values being compared are the result of calculations. For example the code show below generates a value for sum which ideally should be 1 be actually is 0.999999999999999889 due to the fact that 0.1 cannot be represented exactly in the binary format used by computers.
If you need to do equlity testing on floating point values, two comparisons with plus or minus some small tolerance should be used.
Lynn
05-25-2015 01:03 PM
The other possibility is to be "old-fashioned" and take a mathematical approach. Here is a paraphrase of the question you asked:
Given two numbers, a and b, decide if a=b, a>b, or a<b.
Play Mathematician and consider the first question, a=b. This is either True or False (though as has been pointed out, "equality" for Floating Point quantities can be a tricky thing ...).
Suppose it is true. Then we are done.
Suppose it is false. Then test a>b. If it is true, we are done. If it is false, then a<b, and we are done.
There are two (and only two) comparisons here, and two Case statements. One comparison (a>b) is only done if the first comparison (a=b) is false. Do you see how to code this? Where do you put the comparison and the resulting Case statement for this comparison?
Bob Schor
05-25-2015 01:25 PM
For simple things like this, I'd rather not use case structure (very personal though).
There is one best answer, but tons of possibilities, here's mine 😉
05-25-2015 03:52 PM
For a problem this simple (and, indeed, for many programming problems), there are multiple "solutions" to get the right answer. Which one is "best"? Depends on the definition of "Best". Some obvious candidates for metrics include: fastest, smallest (either in Block Diagram Space occupied or Memory Used), "most correct", "most robust", and "most intuitive".
The last three are clearly judgement calls, and may well depend on the "intended audience" -- a beginning student of LabVIEW will have a different view of "intuitive" than someone who has been developing advanced applications for many years.
I tend to work more with "beginners" than with "experts", hence my natural preference (particularly when dealing with beginners) is for "most intuitive", which sometimes means "simplest", or "using the most basic LabVIEW functions and structures". Note that in many cases, LabVIEW may well work "behind our backs" to change the graphic code we develop into rather more efficient machine code, so without doing actual timing tests, determining which of two methods is "fastest" or "smallest" may not be obvious. However, "simplest" (particularly for beginners), while definitely subjective, is something to consider (in my humble opinion).
Bob Schor
05-25-2015 04:49 PM
Here's another one, same as crossrulz, but a bit simpler 🙂
05-27-2015 03:49 AM - edited 05-27-2015 03:55 AM
@johnsold, how to do that. I have attached a VI. When a place a array it appears on front panel also, which I don't play. On front panel, there will be two number & one output.
05-27-2015 06:42 AM
@Vindhyachal.Takniki wrote:
@johnsold, how to do that. I have attached a VI. When a place a array it appears on front panel also, which I don't play. On front panel, there will be two number & one output.
You use Build Array to create an array of booleans from your comparison results and then Boolean Array To Number to get a corresponding number. You can then wire that number result into the case selector.