ENIGMA Forums
Contributing to ENIGMA => Function Peer Review => Topic started by: RetroX on December 13, 2010, 04:15:09 pm
-
double clamp(double x, double lower, double upper) { return (x < lower ? lower : (x > upper ? upper : x)); }
Simple function that would be useful.
-
What does it do?
-
Get out your minmax, let's go out tonight, it'll be a bang and the whole gang'll rival up on in with the llllllllllittle cherries and sweeeeeeeeeeet comparies. So get out your min(max(x,lower),upper) tonight
-
You mean median()?
-
You mean median()?
No he means clamp(). There is no equivalent in GM. It returns "lowest" when x<lowest, and highest when x>highest, and x when its between the two. Dunno if serpex's method is better thou. Because using at most two if cycles could be better than using always two function calls.
-
That's what median does in GML.
-
Median works in its place on a technicality.
Consider three cases.
1) X < L < H
2) L < X < H
3) L < H < X
in each of these three cases, median picks the middle number. If X < L, median picks L since L happens to be in the middle. If X > H, median picks H since H happens to be in the middle. If L<X<H, median picks X.
However newbies aren't privy to this fact, since the name isn't exactly self explanatory, and it's not normally documented for this purpose. Having a clamp that simply delegates to median would work fine.
-
Thanks. Didn't know that. I did use median for a different purpose, and having up to 16 arguments (in GML) didn't help me get the idea either.
-
Another neat thing about median is that argument order doesn't matter.
-
eh I really wasn't paying attention when I wrote this
But yeah, median works. :V So does minmax.
-
Clamp is more efficient than median, unless median is specialized for each argument count.
-
Clamp is more efficient than median, unless median is specialized for each argument count.
Which is why min(max(x,min),max) is the best