Math.min and Math.max

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

zambani(Posted 2008) [#1]
Is there an equivalent to JavaScript Math.min and Math.max in BlitzMaX?


Koriolis(Posted 2008) [#2]
Err, like "Min" and "Max"?
Well, "Min" and "Max" :)


GfK(Posted 2008) [#3]
Dunno. What's it do?


Retimer(Posted 2008) [#4]
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)


Dabhand(Posted 2008) [#5]
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! ;)


zambani(Posted 2008) [#6]
Koriolis: Yes like Min and Max,

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


Gabriel(Posted 2008) [#7]
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.


Retimer(Posted 2008) [#8]
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


zambani(Posted 2008) [#9]
ReTimer: Thanks a lot for the function


Retimer(Posted 2008) [#10]
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.


Bremer(Posted 2008) [#11]
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




Retimer(Posted 2008) [#12]


I'm an optimization freak =P


Michael Reitzenstein(Posted 2008) [#13]
.