LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Two number comparison

Solved!
Go to solution

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.

 

0 Kudos
Message 1 of 10
(12,909 Views)

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

0 Kudos
Message 2 of 10
(12,899 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 10
(12,876 Views)

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.

 

Sum.png

 

If you need to do equlity testing on floating point values, two comparisons with plus or minus some small tolerance should be used.

 

Lynn

0 Kudos
Message 4 of 10
(12,852 Views)

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

 

0 Kudos
Message 5 of 10
(12,831 Views)

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 😉

 

Untitled.png

0 Kudos
Message 6 of 10
(12,816 Views)

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

0 Kudos
Message 7 of 10
(12,791 Views)

Here's another one, same as crossrulz, but a bit simpler 🙂

 

Untitled.png

Message 8 of 10
(12,769 Views)

@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.

0 Kudos
Message 9 of 10
(12,683 Views)
Solution
Accepted by topic author Vindhyachal.Takniki

@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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 10 of 10
(12,650 Views)