Jump to content




[Maths] Doing a sum that is in a string


  • You cannot reply to this topic
9 replies to this topic

#1 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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


#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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)


#3 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

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.

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 November 2012 - 03:14 AM

View Postluanub, on 23 November 2012 - 01:42 AM, said:

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.

View PostOrwell, on 23 November 2012 - 01:53 AM, said:

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]'

#5 billysback

  • Members
  • 569 posts

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.

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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 :

#7 billysback

  • Members
  • 569 posts

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


#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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 :?

#9 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 23 November 2012 - 04:07 PM

View PostremiX, on 23 November 2012 - 03:14 AM, said:

*snip*

View PostOrwell, on 23 November 2012 - 01:53 AM, said:

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.

#10 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 November 2012 - 11:03 PM

View PostOrwell, on 23 November 2012 - 04:07 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users