Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

min? max?

Status
Not open for further replies.

voodoojon

Technical User
Dec 18, 2003
107
GB
I can't seem to find any functions in vbscript for min/max. I knew the language was a bit limited, but this seems excessive. I can make my own with
min(a,b) = 0.5*((a + b)- abs(a-b))
max(a,b) = 0.5*((a + b) + abs(a-b)),
but shurely there's a better way?
 
Hello voodoojon,

The basic of your one-line rendering of min/max is sound in math. It should be acceptable for most scripting application. The only problem is type conversion and truncation error which might get propagated for calculation intensive application. So, though cumbersome, using simple custom functions using if-else and comparison (>,<) should have no cause to feel the script be considered less concise. (People might think using built-in function of the language/compiler make the program concise---that's not my opinion.)

regards - tsuji
 
I agree. The arithmetic errors could catch up with you, and it doesn't work for other types like Strings either.

I hesitate to post large chunks of code, but here is an approach that takes advantage of a trick to let you have a Min( ) that can accept more than just two arguments, along with a simple Min2( ) along the lines tsuji suggests:
Code:
Option Explicit

Function Min(a, b, c, d, e, f)
  'Compares 1 to 6 arguments, returns the
  'one with the smallest value.  Just leave
  'out arguments you do not have.

  'Last arg must be present to avoid a
  'VBScript syntax error.

  'Bad example:
  '  j = Min(1, , , ,7, )

  'Good Example:
  '  j = Min(1, , , , ,7)

  Dim args, i

  args = Array(a, b, c, d, e, f)
  For i = 0 To 5
    If VarType(args(i)) <> vbError Then
      If VarType(Min) <> vbEmpty Then
        If args(i) < Min Then Min = args(i)
      Else
        Min = args(i)
      End If
    End If
  Next
End Function

Function Min2(a, b)
  If a < b Then Min2 = a Else Min2 = b
End Function

MsgBox CStr(Min(1, , , , ,7))
MsgBox CStr(Min(7, , , , ,1))
MsgBox CStr(Min( , ,1, , ,7))
MsgBox CStr(Min( ,-2,1, , ,7))
MsgBox CStr(Min( ,&quot;a&quot;,&quot;A&quot;, , ,&quot;Z&quot;))

MsgBox CStr(Min2(5,-3))
You can create a Max( ) the same way quite easily.

Most scripts will only need a Min2( ) as above though, or maybe a couple of such tailored functions such as a Min7( ) if you need it.
 
Why not just create a function that manages an array that is sent to it and leave it up to the programmer to use in however they want...I.E.

'The following code will generate an error if the array has true sting items. You can of course modify it to take care of that.

info=&quot;1,2,3,4,8,9,123123,55,-5647,1321,109882903,234&quot;
InfoArray=Split(info,&quot;,&quot;)

msgbox min(InfoArray)
msgbox max(InfoArray)

DataArray=Array(1,2,3,4,8,9,123123,55,-5647,1321,109882903,234)
msgbox Min(dataarray)
msgbox Max(dataarray)

Function Min(AnArray())
MinItem=cdbl(AnArray(0))
For Each Item In Anarray
If cdbl(Item) < Minitem Then Minitem = cdbl(Item)
next
min=Minitem
End Function


Function Max(AnArray())
MaxItem=Cdbl(AnArray(0))
For Each Item In AnArray
result = Maxitem < Item
If cdbl(Item) > MaxItem Then Maxitem = Cdbl(Item)
Next
max=Maxitem
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top