This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
remiX's profile picture

[Maths] Doing a sum that is in a string

Started by remiX, 23 November 2012 - 12:02 AM
remiX #1
Posted 23 November 2012 - 01:02 AM
Hey guys, is it possible print the answer of what is in a string which is mathematically correct, ie:


sum = '2/3+4'
-- I've tried print(tonumber(sum)) and print(sum)
-- returns nil
Luanub #2
Posted 23 November 2012 - 01:42 AM
The single quotes ' are literal so sum would == 2/3+4, remove them and sum should == 4.6666665.

By simple doing this the code works fine for me

sum = 2/3+4
print(sum)
Orwell #3
Posted 23 November 2012 - 01:53 AM
I believe that he actually wants to use strings. You could do this, it's quite unsafe though:

local sum = '(math.sqrt(5)+1)/2'
print(loadstring('return '..sum))

Edit: It's better to parse it of course. I had to do it using stacks for an assignment once, but there are other methods too.
remiX #4
Posted 23 November 2012 - 03:14 AM
The single quotes ' are literal so sum would == 2/3+4, remove them and sum should == 4.6666665.

By simple doing this the code works fine for me

sum = 2/3+4
print(sum)

I know you can do that, but I have something that adds a math operation, *, /, -, + to a string.

I believe that he actually wants to use strings. You could do this, it's quite unsafe though:

local sum = '(math.sqrt(5)+1)/2'
print(loadstring('return '..sum))

Edit: It's better to parse it of course. I had to do it using stacks for an assignment once, but there are other methods too.

You have the idea of what I want, but with that the output i get is 'function: [random letters representing the function name I think]'
billysback #5
Posted 23 November 2012 - 04:36 AM

local function addMath(val1, val2, op)
	if op == "+" then return val1+val2
	elseif op == "-" then return val1-val2
	elseif op == "/" then return val1/val2
	elseif op == "*" then return val1/val2
	elseif op == "^" then return val1^val2
	else return val1 end
end

local function doMath(val)
	local splt = {}
	local op = "+"
	if string.find(val, "+") ~= nil then --add
		splt = split(val, "+")
		op = "+"
	elseif string.find(val, "-") ~= nil then --take
		splt = split(val, "-")
		op = "-"
	elseif string.find(val, "/") ~= nil then --divide
		splt = split(val, "/")
		op = "/"
	elseif string.find(val, "*") ~= nil then --multiply
		splt = split(val, "*")
		op = "*"
	elseif string.find(val, "^") ~= nil then --power
		splt = split(val, "^")
		op = "^"
	else
		splt = {tonumber(val)}
	end
	local ok = false
	if type(splt) ~= "number" then
		if #splt == 1 then
			return splt[1]
		elseif #splt == 0 then
			return nil
		else
			ok = true
		end
	else
	    return splt
	end
	if ok then
		local n = 0
		for i=1,#splt do
			local cn = doMath(splt[i])
			n = addMath(n, cn, op)
		end
		return n
	end
   end
end

I created this for another project; use:

local result = doMath("1+1/2*3^2")
cannot use brackets.
remiX #6
Posted 23 November 2012 - 04:45 AM
I'm getting attempt to call nil on this line:

splt = split(val, "+")

I don't have the split function :
billysback #7
Posted 23 November 2012 - 04:45 AM
Oh, lol ;)/>/>
here:

function split( _sInput, _sDelimiter )
    local tReturn = {}
    local delimiter = string.gsub( _sDelimiter, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1" )
    local searchPattern = "([^"..delimiter.."]+)"..delimiter.."?"

    for match in string.gmatch( _sInput, searchPattern ) do
	    table.insert( tReturn, match )
    end
    return tReturn
end
remiX #8
Posted 23 November 2012 - 08:17 AM
Add is worked but everything else is failing :/

I did 10-5 and it returned -15
100/100 = 0
and 10*1 = 0

What could be the problem :?
Orwell #9
Posted 23 November 2012 - 04:07 PM
*snip*

I believe that he actually wants to use strings. You could do this, it's quite unsafe though:

local sum = '(math.sqrt(5)+1)/2'
print(loadstring('return '..sum))

Edit: It's better to parse it of course. I had to do it using stacks for an assignment once, but there are other methods too.

You have the idea of what I want, but with that the output i get is 'function: [random letters representing the function name I think]'

Indeed I made a mistake, the hexadecimal string is probably the address of the function in memory (correct me if I'm wrong). I just forgot to call that function, here is the correction:

local sum = '(math.sqrt(5)+1)/2'
print(loadstring('return '..sum)())
I believe this should work.
remiX #10
Posted 23 November 2012 - 11:03 PM
Indeed I made a mistake, the hexadecimal string is probably the address of the function in memory (correct me if I'm wrong). I just forgot to call that function, here is the correction:

local sum = '(math.sqrt(5)+1)/2'
print(loadstring('return '..sum)())
I believe this should work.

Works perfectly. Thanks man.