hi all ,
i want to perform comparison between two variable and find out which is greater without using comparison operator.
give me suitable answer,
regards,
abhishek mishra
Printable View
hi all ,
i want to perform comparison between two variable and find out which is greater without using comparison operator.
give me suitable answer,
regards,
abhishek mishra
You can perform the substraction and compare the result with 0 (zero):VB Code:
Dim A As Double, B As Double A = 0.1 B = 0.2 Select Case (A - B) Case 0 Debug.Print "A equals B" Case Is < 0 Debug.Print "B is grater then A" Case Else Debug.Print "A is grater then B" End Select
Althoug I think it's utter nonsense to do so:
Sub test3()
A = 50
B = 20
C = A - B
If (C = Abs(C)) Then MsgBox "A = larger"
End Sub
But why do you want to do soQuote:
Originally Posted by abhi.ashok
Isn't it obvious? :)Quote:
Originally Posted by ganeshmoorthy
@abhi.ashok: i don't believe it's possible, but if it is, it may not be so easy as you may think.
What is your purpose of doing so? Why make your life difficult? :p
See my solution here:
Your Thread in the math forum
Duplicate threads merged.
Please do not double post the same question.
Alternatively to the above, test:
VB Code:
MyVal = sqr(1-mod(A/B))
If A > B, then mod(A/B) > 1 and so you attempt to sqrt a negative number, which will throw an error.
If B > A, then mod(A/B) < 1 and the sqrt succeeds.
Not as good a method as Dnereb's, because you specifically design an error into it, but an alternative nevertheless.
give me a suitable answer. I seemed to remember this as a homework quesiton from college which I am sure this is.Quote:
Originally Posted by abhi.ashok
On a very low very you can do bitwise operation to compare the bits of the variable. Pretend you are designing a circuit for a comparison operator on a cpu.
ok, i am going to assume you know binary, how to add and subtract in binary, 2 compliment, and you know boolean math. If you don't know these you arent going to understand this nor will understand how a processor works.
anyway, just do binary subtraction for each bit of the 2 numbers using AND and Invert. if the resultant MSB is 1 then number is negative and first number was smaller, expect in the case of two negative numbers.
It could also be done with boolean operators.. Are you allowed to compare the numbers 1 bit at a time? Or are you forced to compare the entire value on a single line?
Boolean math can actually eliminate/transform virtually any IF statement..
Doh, how did I overlook the fact that what I am saying was just said by SuperBovine..:)