A function to do maths

Share your scripts and packages with other Mudlet users.
Post Reply
Wingard
Posts: 9
Joined: Thu Jan 14, 2010 6:41 pm

A function to do maths

Post by Wingard »

Code: Select all

function calc(exp)
	return assert(loadstring("return "..exp))() or 0
end
You give it an expression, such as (1 + 2) * (3 - 1) and it returns what it equals.
The advantages to this over the other function available for the same thing:

* It doesn't require a library
* It's cross-platform
* It's a one liner

I'm using it in an alias, like so:

Pattern: ^bc (.*)$
Send: echo("Math: "..calc(matches[2]))

but you could probably do a lot more with it.

Post Reply