Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Math Scripting Questions

Logic Gears
Registered User
Join date: 28 Dec 2007
Posts: 2
12-28-2007 18:25
Im looking to assign a mathmatic symbol to a integer or something like that..

can this be done?

like can the + symbol be assigned to the integer "add"

so i can go Total = a add b;

i know this is silly but im looking to randomly switch out multiplication subtraction and addition on a whim. can this be done?
Okiphia Anatine
Okiphia Rayna
Join date: 22 Nov 2007
Posts: 454
12-28-2007 18:37
From: Logic Gears
Im looking to assign a mathmatic symbol to a integer or something like that..

can this be done?

like can the + symbol be assigned to the integer "add"

so i can go Total = a add b;

i know this is silly but im looking to randomly switch out multiplication subtraction and addition on a whim. can this be done?


This is possible... I've done it... I think you just put

integer a= var1 + var2;

or something similar.. though I can't remember atm..just like a lot of teleporters use subtraction.
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
12-28-2007 19:00
First you can only declare that the integer add = an integer, not anything else.

Secondly you can only typecast to a string, list, rotation, vector, key, float or integer. You can't typecast to a binary operator such as +.-.*./ or %. So although you could say that (string)a = "+", it would only be a string and not be useable in a formula.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Logic Gears
Registered User
Join date: 28 Dec 2007
Posts: 2
12-28-2007 19:02
damn... ok.. thanks. lol i guess im gonna have to use if statements.. ugh..
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
12-28-2007 19:07
From: Logic Gears
damn... ok.. thanks. lol i guess im gonna have to use if statements.. ugh..

Once you get an idea of how you think you want it, then post a snippet of the script here and we will pitch in and help streamline it some and maybe make it more efficient with you.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Wyatt Burton
Registered User
Join date: 11 Jan 2007
Posts: 49
12-28-2007 19:07
From: Logic Gears
Im looking to assign a mathmatic symbol to a integer or something like that..

can this be done?

like can the + symbol be assigned to the integer "add"

so i can go Total = a add b;

i know this is silly but im looking to randomly switch out multiplication subtraction and addition on a whim. can this be done?


You could if you were using C++ or Smalltalk. This would be called operator overloading.

In SL and other languages you would use a function similar to this:

From: someone

integer doOper(integer x, string oper, integer y)
{
if (oper=="+";) {return x+y;}
if (oper=="-";) {return x-y;}
if (oper=="*";) {return x*y;}
return -999999999999999; // bad oper
}

default
{
state_entry()
{
llSay(0, "Hello, Avatar!";);
}
touch_start(integer total_number)
{
llSay(0, "Touched: "+(string)doOper(1,"+",2));
}
}
Ace Cassidy
Resident Bohemian
Join date: 5 Apr 2004
Posts: 1,228
12-28-2007 19:14
Yes, you'll need a bunch of "if" statements, but you could put them into a function. For example :

float evaluate(float operand1, string operator, float operand2)
{
if ( operator == "+" )
return operard1 + operand2;
if ( operator == "-" )
return operard1 - operand2;
if ( operator == "*" )
return operard1 * operand2;
if ( operator == "/" )
return operard1 / operand2;
}

then you could have code like :

z = evaluate( x, op, y );
_____________________
"Free your mind, and your ass will follow" - George Clinton
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
12-29-2007 11:56
From: Logic Gears
i know this is silly but im looking to randomly switch out multiplication subtraction and addition on a whim. can this be done?


as stated above you will need a block of else if's however you may find it easyer to implement in Reverse Polish Notation i.e.

5 4 + 3 * is equvelent to 3*(5+4) and can be implemented easyly on a chat channel or linked message basis. I am busy with an implementation of the HP10c calculator that I will publish soon but to get you started.

CODE

// VeryCalc CPU - General purpuse CPU
// Simple Instruction set version
//
// Constants
integer PriCh = PUBLIC_CHANNEL; //for testing only set to your non-zero working channel
//registers
float x; float y; float z; float t; float lstx;

//working variables
list cmdlin; integer cmdlen; string curcmd; integer prgctr;integer tmp;

//data stack functions
push(float val){t=z;z=y;y=x;x=val;}
fnxy(float val){lstx=x;x=val;y=z;z=t;}

//utils
integer isnum(string v)
{
if("-"==v)return FALSE;
if(~llSubStringIndex("0123456789.-",llGetSubString(v,0,0))
&& ~llSubStringIndex("0123456789.-",llGetSubString(v,-1,-1)))return TRUE;
else return FALSE;
}
exec(string cmd)
{
//Stack Operations
if("enter" == cmd){push(x);}
else if("clrx"==cmd){x=0;}
else if("lastx" == cmd){push(lstx);}
else if("rold" == cmd){lstx=x;x=y;y=z;z=t;t=lstx;}
else if("swap" == cmd){lstx=y;y=x;x=lstx;}
//arithmetic
else if("+"==cmd){fnxy(y+x);}
else if("-"==cmd){fnxy(y-x);}
else if("*"==cmd){fnxy(y*x);}
else if("/"==cmd){fnxy(y/x);}
else if("^"==cmd){fnxy(llPow(y,x));}
else if("sqrt"==cmd){x=llSqrt(lstx=x);}
else if("sqr"==cmd){x=(lstx=x)*x;}
}

default
{
state_entry()
{
llListen(PriCh,"",NULL_KEY,"");
}
listen(integer channel, string name, key id, string message)
{
cmdlin = llParseString2List(message,[" "],[]);
prgctr=0; cmdlen = llGetListLength(cmdlin);
while(prgctr < cmdlen)
{
curcmd=llToLower(llList2String(cmdlin,prgctr++));
if(isnum(curcmd)){push((float)curcmd);}
else exec(curcmd);
}
llSay(PriCh,(string)x);
}
}
CODE


to use it place it in a prim seperate to your, object and chose a value for PrvCh.
have your object say the equation on PrvCh as plain text and then listen on the same channel for the result, which you can display or cast as a Float to use in further calculations.