loadstring function

In lua there is loadstring function, that is so useful.
Why it is dissabled? How can I enable?

PD: Waiting for dumb votes :P.

It’s called RunString in GMod Lua.

If possible, never use user input in it.
If required, check/clean/secure/verify/check again.

Here’s a replica with most of the same functionality.

[lua]loadstring_cache = { }

local loadstr = [[ loadstring_cache[ %s ] = function( ) %s end ]]

function _G.loadstring( content )
local index;

index = util.CRC( content )
index = tonumber( index )

if !( loadstring_cache[ index ] ) then
    RunString( loadstr:format( index, content ) )
end

return loadstring_cache[ index ];

end[/lua][lua]
concommand.Add( “say_loadstring”, function( _, _, arg )
local msg = table.concat( arg, " " )

if ( msg:find( "$(%b[])" ) ) then
    msg = msg:gsub( "$(%b[])", function( x )
            return loadstring( "return " .. x:sub( 2, -2 ) )( )
        end )
    
    RunConsoleCommand( "say", msg )
end

end )[/lua]



] say_loadstring 2 + 2 = $[ 2 + 2 ]
Modulo Sally: 2 + 2 = 4


Don’t worry I just was using it to make a shit equation solver.

Something like this?

No. I want use an ‘x’ too.

PD: On that panda, try to do 2 - 2…

You cant use x, or at least have it solve algebraic equations without some extreme mathmatical background. We have already gone over this in another thread.

I’ve finished mine :P.

It sucks because it does a loop until finds the value but it doesn’t take much time. If you want to test, tell me a linear equation with one x and I’ll write here how much time takes to solve it.

The main idea is to use something like “x * 4 = 16” and then divide it into “x * 4” and “16”. Next of that, do something like:

RunString(“function leftside() return x * 4 end”)
RunString(“function rightside() return 16 end”)

Then give x values until leftside() == rightside().

Now that’s a stupid way of doing it.

[lua]function solve(str) – 3x + 5 = 3
local a, s, b, r = str:match("^(%d+)x%s?([±])%s?(%d+)%s?=%s?(%d+)");
if not (a and s and b and r) then
error(“Invalid Syntax”);
end
return (r-tonumber(s…b))/a;
end[/lua]

[lua]print(solve(“3x + 5 = 3”))
print(solve(“10x - 7 = 3”))
print(solve(“25x - 4 = 1”))[/lua]

Output:


-0.66666666666667
1
0.2

Yeah but MINE can solve all kind of equations. Not only Ax + B = C…

It took 0.0 seconds to solve:

3*(x - 2)^2*(x + 5) = 3*(x + 1)^2*(x - 1) + 3

x = 1.3333333333333…

With 13 decimals.

Make it solve 2x+2-1*3 = 5x+1-6/2.

A math solver is only complete when it follows PEMDAS.

0.33333333333333

With 13 decimals.

It took no time to solve (maybe I’m wrong):

print(os.clock()) – 0
print(CalculateEq(…)) – 0.33333333333333
print(os.clock()) – 0

Another example:

“14 = cosine(x)”

Returns No valid value for x.

“cosine(x) = 1”

Returns x = 0

“x*x = 15”

Returns x = 3.8729833462074 negative and positive.

“x^2 + 5 = 0”

Returns x = 2.0949999964863e-008 negative and positive.
(Error :P: this equation has no result).

You can also request only an aproximation:

“x * 3 = 15/16” With 2 decimals:

Returns x >= 0.305 and x < 0.315

PD: What is PEMDAS?

As you could have found with a single Google search: The order of operations.

We call it BIDMAS here. (Brackets , indices , division, multiplication, addition, subtraction)

I was taught BODMAS (of replacing indices, like something to the power of). Later one of my maths teachers compromised with BOIDMAS as the two previous teachers taught opposing Mnemonics.

Brackets of Multiplication Division Addition Subtraction,

BOMDAS Here in Aus

it’s DADGUM here in alabamy

And I’m sure in iceland it’s AQL;3¥ but last time I checked, this wasn’t the “post your PEMDAS variants” thread. :v:

/rant of the week

Now bed.

[editline]12:24AM[/editline]

Also 1337th post derp