dcsimg
Results 1 to 4 of 4

Thread: Smallest of 3 numbers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2017
    Posts
    206

    Smallest of 3 numbers

    What is the easiest and simplest way to get the smallest value of three numbers. There is the possibility of two or three values the same

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,572

    Re: Smallest of 3 numbers

    May want to test this well, off top of my head...
    Code:
    If Nr1 =< Nr2 And Nr1 =<  Nr3 Then
       ' nr1 is smallest; though numbers 2 & 3 may be equal or greater
    ElseIf Nr2 =<  Nr3 Then
       ' nr2 is smallest; though Nr3 may be equal or greater, but Nr1 is greater else triggered above
    Else
       ' nr3 is smallest by elimination
    End If
    edited: you can remove the = from the comparisons above if first-in doesn't matter when a tie exists. In other words, comparing 1,2,1 will return the 3rd number as the smallest instead of the 1st number.
    Last edited by LaVolpe; Oct 21st, 2019 at 10:03 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,677

    Re: Smallest of 3 numbers

    I would say depending on the need, creating a function to use would be easiest in the long run, and also simplest to use once written. You could use variant parameter array to the function to handle 2 to n comparisons, i.e. z = Min(a, b) or z = Min(a, b, c, d), ...

    If you just want to compare three values you should need two If tests as a minimum, and I would use an extra variable to store the result, to keep it to two.
    You could use IIf and put the two IIf statements in one line, to appear simplest
    Code:
    d = IIf(a < b, a, b): d = IIf(d < c, d, c)

    p.s. As mentioned in the first part of my post, but limited to comparing two values, a Min function could be written to be reused.
    Code:
    Private Function Min(a As Variant, b As Variant) As Variant
      If a < b Then
        Min = a
      Else
        Min = b
      End If
    End Function
    And then you could set d to the minimum value like this:
    Code:
    d = Min(Min(a,b), c)
    Last edited by passel; Oct 21st, 2019 at 10:06 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,027

    Re: Smallest of 3 numbers

    IIRC we had a thread exactly like this one a few years back.

    EDIT: Found it
    http://www.vbforums.com/showthread.p...so-the-largest
    Last edited by Zvoni; Oct 22nd, 2019 at 01:39 AM.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width