Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Fun with conditional logic...

Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
05-20-2009 14:43
so I'm playing around with some scripts today, trying to rebuild them a bit more efficiently and I hit two common problems in the same script...

first problem: possible divide by zero.
obvious way to handle it?
CODE

if (x) {
llOwnerSay( (string)( y / x ) ); //-- return (y / x)
}else{
llOwnerSay( "error, undefined" ); //-- if x = 0, return error.
}

fun way to handle it? replacing zero automatically to get default values
CODE

llOwnerSay( (string)( y / (x | !x) ); //-- if x = 0, returns y.
llOwnerSay( (string)( y / (x | (!x * y)) ); //-- if x = 0, returns 1.



problem two: three way switch (negative,, zero, positive)
obvious way to handle it?
CODE

if (x) {
if (x > 0) {
llOwnerSay( "Positive" );
}else{
llOwnerSay( "Negative" );
}
}else{
llOwnerSay( "Zero" );
}

fun way?
CODE

llOwnerSay( llList2String( ["Zero", "Positive", "Negative"], (x > 0) - (0 > x) ) );

like that little trick with the list2String? it works with llGetSubstring too!
CODE

llOwnerSay( llGetSubString( "UNTRUE" , (x != 0) * 2, - 1 );



an old one that relies on an oddity of the implementation of != for lists (thanks strife)
CODE

x = (myList != []);
x = ([] != myList);
//--same as
x = llGetListLength( myList );
x = -llGetListLength( myList );



key validity?
CODE

if (vKeyTest){
//-- true if vKeyTest, Type = Key, and has a valid key
}else{
//-- false if vKeyTest has NULL_KEY or is invalid
}



index found?
CODE

if (!FindSomeIndex( thing2search, thing2look4 )){}
if (~FindSomeIndex( thing2search, thing2look4 )){}
if (!~FindSomeIndex( thing2search, thing2look4 )){}
//-- same thing as
if (FindSomeIndex( thing2search, thing2look4 ) == 0){}
if (FindSomeIndex( thing2search, thing2look4 ) != -1){}
if (FindSomeIndex( thing2search, thing2look4 ) == -1){}


have any fun tricks with conditionals that you use alot? share!
_____________________
|
| . "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...
| -
Phate Shepherd
Addicted to code
Join date: 14 Feb 2008
Posts: 96
05-22-2009 14:00
Ohhh... I like the llList2String construct... almost a replacement for inline conditionals.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
05-22-2009 22:08
the 3 way logic construct is probably one of the more useful in there (although I really love the inline if functionality)

you can tell if a number is positive negative or 0
((0 < X) - (0 > X))

if a number is above, below, or at another number
((Y < X) - (Y > X))

if a number is outside a range, and which direction (A high end , B low end)
((A < X) - (B > X))

inside a Range
!((A < X) - (B > X))

return positive or negative for TRUE FALSE switches
((FALSE < X) - (TRUE > X))
also works as
(2 * (X > FALSE) - 1)
or
(-2 * !X + 1)

speaking of which, there's a few math shorcuts....
X * llPow( 2, Y ) == X << Y;
//-- faster: works to replace any number where Y is an integer
X / llPow( 2, Y ) == X >> Y;
//-- same idea... making y negative reverses the operation
//-- eg
X * 8 == X << 3

X % llPow( 2, Y ) == X & llPow( 2, Y ) - 1;
//-- eg
X % 8 == x & 7

and I'm not sure of the speed difference, but...
-~x == ++x
~-x == --x
_____________________
|
| . "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...
| -