Difference between revisions of "Prefix Calculator"

From Second Life Wiki
Jump to navigation Jump to search
(New page: Created by Xaviar Czervik. <pre> list stack; push(float i) { stack += (integer)(i*10000000); // Float To Integer - Not good to use for large values, but it works. Sue me... } float...)
 
m
Line 1: Line 1:
Created by Xaviar Czervik.
Created by [[User:Xaviar CzervikXaviar Czervik]]. Do whatever you wish with this function: Sell it (good luck), use it, or modify it.


<pre>
<pre>

Revision as of 20:55, 7 June 2007

Created by User:Xaviar CzervikXaviar Czervik. Do whatever you wish with this function: Sell it (good luck), use it, or modify it.

list stack;

push(float i) {
    stack += (integer)(i*10000000);  // Float To Integer - Not good to use for large values, but it works. Sue me...
}

float pop() {
    float i = peek();
    stack = llList2List(stack, 0, -2);
    return i;
}

float peek() {
    return ((float)llList2Integer(stack, -1))/10000000; // Float To Integer - Not good to use for large values, but it works. Sue me...
}

default {
    state_entry() {
        llListen(0, "", llGetOwner(), "");
    }
    listen(integer i, string n, key id, string m) {
        string data = m;
        
        list parsed = llParseString2List(data, [" "], []);
        
        integer i = llGetListLength(parsed);
        while (i--) {
            string data = llList2String(parsed, i);
            string check = (string)((float)data);
            while (llGetSubString(check, -1, -1) == "0") {
                check = llGetSubString(check, 0, -2);
            }
            if (llGetSubString(check, -1, -1) == ".")
                check = llGetSubString(check, 0, -2);
            if (check == data) { // Is it a number?
                push((float)data);
            } else {
                if (llStringLength(data) == 1) {
                    float first = pop();
                    float second = pop();
                    if (data == "+") {
                        push(first + second);
                    }
                    if (data == "-") {
                        push(first - second);
                    }
                    if (data == "*") {
                        push(first * second);
                    }
                    if (data == "/") {
                        push((float)first / (float)second);
                    }
                } else {
                    float first = pop();
                    if (data == "sin") {
                        push(llSin(first));
                    }
                    if (data == "cos") {
                        push(llCos(first));
                    }
                    if (data == "tan") {
                        push(llTan(first));
                    }
                    if (data == "sqrt") {
                        push(llSqrt(first));
                    }
                }
            }
        }
        llOwnerSay((string)pop());
    }
}