PDA

Click to See Complete Forum and Search --> : which is greater


abhi.ashok
Dec 28th, 2006, 04:25 AM
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

gavio
Dec 28th, 2006, 04:42 AM
You can perform the substraction and compare the result with 0 (zero):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

Dnereb
Dec 28th, 2006, 04:46 AM
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

ganeshmoorthy
Dec 28th, 2006, 04:50 AM
i want to perform comparison between two variable and find out which is greater without using comparison operator.But why do you want to do so

gavio
Dec 28th, 2006, 04:58 AM
But why do you want to do soIsn't it obvious? :)

@abhi.ashok: i don't believe it's possible, but if it is, it may not be so easy as you may think.

zynder
Dec 28th, 2006, 05:06 AM
What is your purpose of doing so? Why make your life difficult? :p

Dnereb
Dec 28th, 2006, 05:12 AM
See my solution here:
Your Thread in the math forum (http://www.vbforums.com/showthread.php?t=444783)

Hack
Dec 28th, 2006, 11:05 AM
Duplicate threads merged.

Please do not double post the same question.

zaza
Dec 28th, 2006, 11:54 AM
Alternatively to the above, test:

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.

superbovine
Dec 29th, 2006, 12:16 PM
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

give me a suitable answer. I seemed to remember this as a homework quesiton from college which I am sure this is.

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.

triggernum5
Dec 30th, 2006, 08:01 AM
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..:)