Math.min and Math.max

BlitzMax Forums/BlitzMax Programming/Math.min and Math.max

Is there an equivalent to JavaScript Math.min and Math.max in BlitzMaX?

Err, like "Min" and "Max"?
Well, "Min" and "Max" :)

Dunno. What's it do?

Min returns the lowest number, max the highest.


The following code illustrates how these methods work

public class MaxMin{
public static void main(String argv[]){
System.out.println(Math.max(-1,-10));
System.out.println(Math.max(1,2));
System.out.println(Math.min(1,1));
System.out.println(Math.min(-1,-10));
System.out.println(Math.min(1,2));
}
}
Here is the output

-1
2
1
-10
1




edit:
In blitzmax

Min(num1,num2)
Max(num1,num2)

If its what I think it is make your own.

Method Min:int(number1:int, number2:int)
if number1 < number2
   return number1
else
   return number2
end if
End Method


Is that what it does?

EDIT: Retimer beat me too it! :)
EDIT 2: Oh, Max has them, didnt realise that! Lesson to all... check docs! ;)

Koriolis: Yes like Min and Max,

ReTimer: Cool thanks. How would one write a function for that if the number of inputs can vary?

How would one write a function for that if the number of inputs can vary?

The number of inputs cannot vary in a BlitzMax function. The nearest you could do is to pass an array of ints, as the size of an array is not fixed in function declarations.

Local blah:Int[] = [41,5125,4323,123,25,216,7547,8,101]

Notify NewMin(blah)
Notify NewMax(blah)

Function NewMin:Int(Nums:Int[])
	Local SelNum:Int=2147483647
	For Local i:Int = EachIn Nums
		If I < SelNum Then SelNum = I
	Next
	
	Return SelNum
End Function

Function NewMax:Int(Nums:Int[])
	Local SelNum:Int=-2147483646
	For Local i:Int = EachIn Nums
		If I > SelNum Then SelNum = I
	Next
	
	Return SelNum
End Function


@Dabhand

Yeah I didn't realise max had it either. I had to edit my post to remove what I previously said =P

ReTimer: Thanks a lot for the function

No problem. To clarify the -2147483647 and 2147483647, that is so the values are at the farmost minimum or maximum. Without declaring those, you wouldn't be able to get a correct min/max value from a list of values under 0.

How about:

Local blah:Int[] = [41,5125,4323,-352,123,25,-42,216,7547,8,101]

Print nMin(blah)
Print nMax(blah)

Function nMin:Int(nums:Int[])
	Local tmp:Int[] = nums[..]
	tmp.sort()
	Return tmp[0]
End Function

Function nMax:Int(nums:Int[])
	Local tmp:Int[] = nums[..]
	tmp.sort()
	Return tmp[tmp.length-1]
End Function



Local blah:Int[] = [41,5125,4323,123,25,216,7547,8,101]
Local TestNum:Int
Local TimeCost:Int

TimeCost = MilliSecs()
For Local i:Int = 1 To 1000000
	TestNum = NewMax(blah)
Next

Print MilliSecs()-TimeCost

TimeCost = MilliSecs()
For Local t:Int = 1 To 1000000
	TestNum = nMax(blah)
Next

Print MilliSecs()-TimeCost

Function NewMin:Int(Nums:Int[])
	Local SelNum:Int=2147483647
	For Local i:Int = EachIn Nums
		If I < SelNum Then SelNum = I
	Next
	
	Return SelNum
End Function



Function NewMax:Int(Nums:Int[])
	Local SelNum:Int=-2147483646
	For Local i:Int = EachIn Nums
		If I > SelNum Then SelNum = I
	Next
	
	Return SelNum
End Function

Function nMax:Int(nums:Int[])
	Local tmp:Int[] = nums[..]
	tmp.sort()
	Return tmp[tmp.length-1]
End Function

Function nMin:Int(nums:Int[])
	Local tmp:Int[] = nums[..]
	tmp.sort()
	Return tmp[0]
End Function



I'm an optimization freak =P

.