Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Clamping (or range limiting) Vector Elements?

Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-19-2009 20:59
From: Hewee Zetkin
I'd say the OP gets the prize. Readability and maintainability over performance any day of the week, even in memory-/performance-constrained LSL! :p

(Very bad form to depend on the true value of a boolean expression always being one! Yes, a triple-operand conditional expression would be very nice to have.)

I'm curious about something, and I just wanted to make sure... was your maintainability statement based strictly on the boolean return value(which we disagree as to the stability of)? or was there something further that I missed?

(I'm shooting for commenting to clarify readability issues, I agree it's a very valid concern)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
04-20-2009 08:56
From: Void Singer
I'm curious about something, and I just wanted to make sure... was your maintainability statement based strictly on the boolean return value(which we disagree as to the stability of)? or was there something further that I missed?

(I'm shooting for commenting to clarify readability issues, I agree it's a very valid concern)


While I see portability--yes, even in LSL where possible--as a very desirable thing, I think the main maintainability concern with this code stems from the readability. The code doesn't exactly, "say what it means." The code in the original post pretty much did.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-20-2009 11:54
From: Hewee Zetkin
While I see portability--yes, even in LSL where possible--as a very desirable thing, I think the main maintainability concern with this code stems from the readability. The code doesn't exactly, "say what it means." The code in the original post pretty much did.

fair point, and being a self documenting code junkie, I relate. I'll see about some more verbose commenting for the library version... about the best I can do considering the conditions I set.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Cerise Sorbet
Registered User
Join date: 8 Jun 2008
Posts: 254
04-20-2009 20:26
Sorry everybody, I get into the same trouble at work. One liners turn into a day of meetings and a page of comments. I will hold the prize through Friday, RFL gets it if you don't.
Boreal Latte
Registered User
Join date: 15 Nov 2007
Posts: 104
04-20-2009 22:13
While I agree with the the readability issue, it is fun with a challenge like this every once in a while;-)
CODE

vector fVecRange0X(vector v, float max){
return <(v.x>0.0)*( (v.x>max)*max+(v.x<=max)*v.x),
(v.y>0.0)*( (v.y>max)*max+(v.y<=max)*v.y),
(v.z>0.0)*( (v.z>max)*max+(v.z<=max)*v.z)>;
}
CODE

It seems to cut of an other 21 bytes of the best so far. Unfortunately, if a number is negative, it returns -0.0, rather than +0.0 - was that within the specs ;-)?

-- Boreal
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-21-2009 02:55
From: Cerise Sorbet
Sorry everybody, I get into the same trouble at work. One liners turn into a day of meetings and a page of comments. I will hold the prize through Friday, RFL gets it if you don't.

don't worry about it. as far as the requirements go you've passed, size was the requirement, I may footnote the original version for reference if I can't suitably comment it, what you do with the prize is up to you since I already closed out on it. recycle it, send it RFL, keep it... that's all you =)

@boreal:
that's amazingly similar to Cerise' original, and I'm wondering why it tested lower than either that or the optimized version I made. I'll experiment a little tonight if I can, and report the results asap. (I'll version it against my optimization too, just so we can see what's going on.)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-21-2009 11:37
@Boreal:
tested, yours runs the same size as Cerise' 189 Bytes (my math optimized version of that 174 Bytes

reference script
CODE

integer cBASE_BYTES = 16120;
string cTEXT = " Bytes";

//--// insert function here //--//

default{
state_entry(){
llOwnerSay( (string)(cBASE_BYTES - llGetFreeMemory()) + cTEXT );
}
}

(the MONO version with base bytes of 62142 returns 512 Bytes for all three)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Viktoria Dovgal
Join date: 29 Jul 2007
Posts: 3,593
04-21-2009 12:31
From: Void Singer
I may footnote the original version for reference if I can't suitably comment it

// You are not expected to understand this.

SHIP IT!
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
04-21-2009 12:34
From: Viktoria Dovgal
// You are not expected to understand this.
I was talking to Dennis Ritchie one day when someone came up and started explaining what the code below that original comment in the UNIX source was about... quite unaware that he was explaining it to one of the guys who wrote it. Dennis kept his face perfectly straight through the whole process, though everyone else was clearly struggling to hold it in.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Boreal Latte
Registered User
Join date: 15 Nov 2007
Posts: 104
04-21-2009 12:48
Super Cool Singer. Part of the problem might have been I run
Second Life 1.22.11 (0) Apr 9 2009 21:29:08 (Cool SL Viewer)
Second Life Server 1.26.2.117266

I get your version of the fVecRangeXY function at 228 bytes compared to the 224 you write in the library.

My version below says 213 bytes in contrast (Using the reference script you gave).

CODE

vector fVecRangeXY( vector v, float min, float max ){
return < (v.x)+(v.x<min)*(min-v.x)+(v.x>max)*(max-v.x),
(v.y)+(v.y<min)*(min-v.y)+(v.y>max)*(max-v.y),
(v.z)+(v.z<min)*(min-v.z)+(v.z>max)*(max-v.z)>;
}
CODE


Best, Boreal
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-21-2009 13:11
From: Boreal Latte
Super Cool Singer. Part of the problem might have been I run
Second Life 1.22.11 (0) Apr 9 2009 21:29:08 (Cool SL Viewer)
Second Life Server 1.26.2.117266

I get your version of the fVecRangeXY function at 228 bytes compared to the 224 you write in the library.

My version below says 213 bytes in contrast (Using the reference script you gave).

CODE

vector fVecRangeXY( vector v, float min, float max ){
return < (v.x)+(v.x<min)*(min-v.x)+(v.x>max)*(max-v.x),
(v.y)+(v.y<min)*(min-v.y)+(v.y>max)*(max-v.y),
(v.z)+(v.z<min)*(min-v.z)+(v.z>max)*(max-v.z)>;
}


Best, Boreal

I need to edit that library page, all the values are 4 bytes off (don't ask me what I did), I'll check the XY version in a bit (or have my sister)... ps you need to put a slash in front of your second PHP tag like so [/PHP]

had an idea to auto-determine whether a script is running in LSO or MONO, and found the perfect jira that doesn't look like they'll try to match the behavior between the two... so I give you the new and improved Function Bye Size Test
CODE

//-- This Hack To Automatically Determine Whether a script is LSO or MONO brought to you by
//-- https://jira.secondlife.com/browse/SVC-3760
//-- Unicode behaviour of llToLower() inconsistent between LSO and MONO

//-- WARNING!!! --//
//-- MONO readings are inconsistent as of 21 April 2009

integer gIntLSO = 15917;
string gStrLSO = "LSO";
integer gIntMONO = 61512;
string gStrMONO = "MONO";
string gStrBase = " Bytes in ";
string gStrTest = "Ü";

//--// Insert Function Here //--//

default{
state_entry(){
if (llToLower( gStrTest ) == gStrTest){
llOwnerSay( (string)(gIntLSO - llGetFreeMemory()) + gStrBase + gStrLSO );
}else{
llOwnerSay( (string)(gIntMONO - llGetFreeMemory()) + gStrBase + gStrMONO );
}
}
}


in case you're wondering why I coded it this way, MONO is VERY inconsistent in the treatment of undeclared data (like "blah, blah, blah";). figured I'd avoid that as much as possible. (in fact putting extra whitespace in front of an assignment operator changed the memory reported in one case!)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Cerise Sorbet
Registered User
Join date: 8 Jun 2008
Posts: 254
04-21-2009 14:29
There is a big change in these new frameworks. They do not call the function any more. The change alters the results. Boreal's version is smaller if it isn't called, bigger if it is. I thought it was the extra argument but that didn't make a big difference. What is LSL doing in there?
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-21-2009 14:46
there is a huge overhead for each function call, and each variable it needs in the call. but you'll notice that the size difference between my original and yours are exactly the same, as is the difference between yours and my tweak. plus I threw in code to make it more readable and friendly between the test bed versions. the change in removing the cal doesn't affect the outcome, because the original calls were the same for all version (which was why I specified it in the original requirements.)
ETA:
I get 199 Bytes in LSO by removing the first v.x|y|z and addition operator, and adding + v to the results on Boreal's XY version.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Cerise Sorbet
Registered User
Join date: 8 Jun 2008
Posts: 254
04-21-2009 15:03
The first framework checked free memory before any function calls. The difference is not consistent with what Boreal posted, something balloons between the different frameworks and the difference is not constant. There should not be that increase.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-21-2009 18:04
From: Cerise Sorbet
The first framework checked free memory before any function calls. The difference is not consistent with what Boreal posted, something balloons between the different frameworks and the difference is not constant. There should not be that increase.

if you take the original script, remove the function code and comment the function call out you'll get

16172

if you add in just your posted funtion (but don't uncomment the call) you'll get

15983
16172 - 15983 = 189 bytes (same as reported by the newer versions)

so I'm not sure where you are seeing inconsistency, unless you read Boreal's last entry (which is being compared against the extrapolated variable minimum & maximum which I added to the Library post) as being the simpler variety we tested?
-------------------------------
to take it further, if you uncomment the function call you get

15918
16172 - 15918 = 254 Bytes
254 - 189 = 65 Bytes for just the default function call

21 bytes to call a user function
+ 16 for an undeclared vector
+ 6 for an undeclared float
- 2 for # of parameters passed
+ 2 for a vector return
+ 2 for typecast to string
= 45
65 - 44 = 18 Bytes for the llOwnerSay?

reference:
https://wiki.secondlife.com/wiki/LSL_Script_Memory

ETA: the above pages is off, I get 6B for undeclared floats, and 2 for (string) typecasts, which agrees with my testing.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Cerise Sorbet
Registered User
Join date: 8 Jun 2008
Posts: 254
04-21-2009 18:36
The numbers are not coming out the same in LSO every time. There is something small wrong with the interpreter or the compiler. Or something that varies in the interpreter is not on that wiki page. It is interesting that your numbers do not add up either.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
04-21-2009 18:46
From: Cerise Sorbet
The numbers are not coming out the same in LSO every time. There is something small wrong with the interpreter or the compiler. Or something that varies in the interpreter is not on that wiki page. It is interesting that your numbers do not add up either.

just edited the above (wiki page was close but not 100% and they do some wierd definitions too that don't fit what I know of variable size, for instance, floats should be calculated as 2+4 not 1+5)

but I'm not sure what you mean about the numbers not coming out the same each time in LSO... none of my tests with any of the scripts has generated different numbers for the function code size, old or new (the new ones just factor out the test code, and the free memory, leaving the actual written code size, so you don't have to do math)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Cerise Sorbet
Registered User
Join date: 8 Jun 2008
Posts: 254
04-21-2009 19:31
The sim I was on for testing looks like it rolled back to yesterday. That could be why a few more things were weird too. I will start over on another sim that isn't puking all over the rug.
1 2