Results 1 to 11 of 11

Thread: which is greater

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    15

    Question comparison which is greater

    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

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: comparison which is greater

    You can perform the substraction and compare the result with 0 (zero):
    VB Code:
    1. Dim A As Double, B As Double
    2.     A = 0.1
    3.     B = 0.2
    4.         Select Case (A - B)
    5.             Case 0
    6.                 Debug.Print "A equals B"
    7.             Case Is < 0
    8.                 Debug.Print "B is grater then A"
    9.             Case Else
    10.                 Debug.Print "A is grater then B"
    11.         End Select

  3. #3
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: which is greater

    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
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  4. #4
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: comparison which is greater

    Quote Originally Posted by abhi.ashok
    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
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  5. #5
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: comparison which is greater

    Quote Originally Posted by ganeshmoorthy
    But why do you want to do so
    Isn'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.

  6. #6
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: comparison which is greater

    What is your purpose of doing so? Why make your life difficult?

  7. #7
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: comparison which is greater

    See my solution here:
    Your Thread in the math forum
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: which is greater

    Duplicate threads merged.

    Please do not double post the same question.

  9. #9
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: which is greater

    Alternatively to the above, test:

    VB Code:
    1. 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.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  10. #10
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: comparison which is greater

    Quote Originally Posted by abhi.ashok
    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.
    Last edited by superbovine; Dec 29th, 2006 at 02:07 PM.

  11. #11
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: which is greater

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width