Pages: [1]
  Print  
Author Topic: clamp()  (Read 2431 times)
Offline (Male) RetroX
Posted on: December 13, 2010, 04:15:09 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Code: [Select]
double clamp(double x, double lower, double upper) { return (x < lower ? lower : (x > upper ? upper : x)); }
Simple function that would be useful.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Post made December 14, 2010, 04:09:23 AM was deleted at the author's request.
Offline (Female) serprex
Reply #2 Posted on: December 14, 2010, 09:02:45 AM
Smooth ER
Developer
Joined: Apr 2008
Posts: 106

View Profile WWW
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
Logged
Post made December 14, 2010, 10:40:30 AM was deleted at the author's request.
Offline (Unknown gender) TheExDeus
Reply #4 Posted on: December 14, 2010, 11:14:51 AM

Developer
Joined: Apr 2008
Posts: 1886

View Profile
Quote
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.
Logged
Offline (Male) polygone
Reply #5 Posted on: December 14, 2010, 12:20:05 PM

Contributor
Location: England
Joined: Mar 2009
Posts: 803

View Profile
That's what median does in GML.
Logged
I honestly don't know wtf I'm talking about but hopefully I can muddle my way through.
Offline (Female) IsmAvatar
Reply #6 Posted on: December 14, 2010, 12:58:26 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 886

View Profile Email
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.
Logged
Offline (Unknown gender) TheExDeus
Reply #7 Posted on: December 14, 2010, 01:38:34 PM

Developer
Joined: Apr 2008
Posts: 1886

View Profile
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.
Logged
Offline (Female) IsmAvatar
Reply #8 Posted on: December 14, 2010, 05:54:39 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 886

View Profile Email
Another neat thing about median is that argument order doesn't matter.
Logged
Offline (Male) RetroX
Reply #9 Posted on: December 14, 2010, 07:34:40 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
eh I really wasn't paying attention when I wrote this

But yeah, median works. :V  So does minmax.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #10 Posted on: December 14, 2010, 07:56:34 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2933

View Profile Email
Clamp is more efficient than median, unless median is specialized for each argument count.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #11 Posted on: December 16, 2010, 03:37:13 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
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
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: [1]
  Print