Function llClamp(integer val, integer min, integer max)
under review
Log In
SamanthaTekcat Resident
This really should be 'float llClamp(float val, float min, float max)'
rhet0rica Resident
SamanthaTekcat Resident That could be llFclamp()—we already have llAbs() and llFabs(), and llFrand() (but somehow no llRand() for integers)
Spidey Linden
under review
RestrainedRaptor Resident
And while we're at it, give us the damn ternary operator! Is there a ticket for that yet?
Woolfyy Resident
simple, less memory use and much quicker if existing in standard functions ... let's cross fingers and dream of such a feature existing for integer, float, vector and rot ... ouch ... LL ... please ...
Bleuhazenfurfle Resident
This should definitely be floats, if not both.
We do have llAbs and llFabs, so there is precedent for llClamp and llFclamp (despite how much I detest that naming scheme). It's an annoying function, yet one we need way too often.
Vincent Nacon
integer llClamp(integer Value, integer Max, integer Min){
return ((Value>Max)*Max) + ((Value<Min)*Min) + ((!(Value<Min)&&!(Value>Max))*Value);
}
Bleuhazenfurfle Resident
Vincent Nacon: Have you measured that code against simple if()s…? Such long-winded micro-optimisations generally rely on pipelining, which is almost never a consideration for evaluated bytecode (which is what I expect LSL to be), and you're doing several repeated comparisons there.
And I'd expect this to be better (8 operators vs. 11):
return Value + ((Value>Max)*(Max-Value)) + ((Value<Min)*(Min-Value));
Vincent Nacon
Bleuhazenfurfle Resident Yeah, it's generally better to have it done in one line process than doing it through if()s, but I like your formula better. Nice one. 👍
rhet0rica Resident
Bleuhazenfurfle Resident Vincent Nacon I'll raise you both your ideas and offer this terrible one:
llListStatistics(LIST_STAT_MEDIAN, [min, value, max]);
Bleuhazenfurfle Resident
rhet0rica Resident:
shudder
You're right, that is
a terrible one!For anyone unaware: llListStatistics is slooooow…
- You have to build a list. (slow)
- LIST_STAT_MEDIAN is probably sorting that list. (slow)
- Then you throw that list you made away.
- And you likely have to cast it back to integer too.
- You may have issues from that trip through float-land.
Because I tend to be kinda OCD, I tossed together a quick test to compare the mathy one to llListStatistics, and yeah, llListStatistics
is
as horrible as I'd expected (and doesn't even save terribly much typing). (About half the slowdown is simply building that three-item list.)Then, since I had the test function handy, I replaced the llListStatistics version with a trivial if()y version… And it seems to beat out the mathy method by a hair — which I suspect is due to what I was saying before; bytecode interpreters lose the benefits of pipelining.
Kristy Aurelia
Both: integer and float versions would be ideal. Maybe even vector and rotation too.
Peter Stindberg
I assume you mean this: https://en.wikipedia.org/wiki/Clamping_(graphics) ?