ignore Arithmetic on upvalue error?

Lets say you want lua to calculate a formula for you, for example
x = (a*b)/s

you can do this by using textinputs and let the user state the difrent values, my problem comes when not
all the variables are given a value, lets say

a = 6
b = 4
s = …

then this error occurres

attempt to perform arithmetic on upvalue ‘x’

thats okey with one formula cause it doesnt stop the app or anything, but when you for example want to calculate two formulas, and the first one get this error the second one wan’t calculate even tough you have stated all the variables on the second one.

example code

local a = 6
local b = 4
local s = …
local i = 16
local t = 12
local f = 3

local function calculate()
x = (a*b)/s
y = (i*t)/f
end

i have found some solutions to this but i’m curios in how some of you people would solve it?

thanks Fredrik [import]uid: 104309 topic_id: 35064 reply_id: 335064[/import]

[I think you should wrap your code samples in \< lua \> tags]

The message you’re seeing is an exception (error.) In your code, above, it’s happening because the ‘s’ variable is nil. You should not try to execute code when you know that a variable is going to be nil. Check the values and respond appropriately. If you are doing user input this way you’ll need a lot of checking of their input.

If you really need to do it, you can catch the “exceptions”. To catch exceptions in lua code you wrap it in a ‘pcall’:

[lua]if pcall(foo) then
– no errors while running foo' ... else -- foo’ raised an error: take appropriate actions

end[/lua]

You can read more about it here, but if you are trying to execute code which you know has exceptions you really should provide some logical checking of the values and avoid the nil variables.

http://www.lua.org/pil/8.4.html [import]uid: 8271 topic_id: 35064 reply_id: 139445[/import]

Thanks, i’ve already thought of that, but in my case this method would require me to do alot of variable checking, are you sure there is no other way, maybe someway to ignore the arithmetic message. the solution i use is to use timers so that the calculations happens after one another, it seems like lua then ignores the first arithmetic message so that the second one can be calculated.

and could you give me a example on how you would check and process the variables.

thanks again! [import]uid: 104309 topic_id: 35064 reply_id: 139449[/import]

It sounds like you’re writing a calculator app. If your code literally contains [lua]x = (a*b)/s[/lua] then you should be checking each variable for nil before you execute the calculation. If you are allowing the user to free-form their equations, then you’re going to need much greater value checking because you’ll never know what they are entering. I’ve not written a calc app, but I know it’s a bit of a nightmare.

At a stretch (but I still think this is the wrong approach) in your function “calculate()” you should be using pcall to wrap the individual calculations, like this:
[lua]local a = 6
local b = 4
local s = …
local i = 16
local t = 12
local f = 3

local function calculate()
local x, y
if (not pcall(function() x = (a*b)/s end) then
return “failed first”
end
if (not pcall(function() y = (i*t)/f end) then
return “failed second”
end
return “success”, x, y
end[/lua] [import]uid: 8271 topic_id: 35064 reply_id: 139454[/import]

thank you! it works pretty good! have a great saturday! [import]uid: 104309 topic_id: 35064 reply_id: 139590[/import]

[I think you should wrap your code samples in \< lua \> tags]

The message you’re seeing is an exception (error.) In your code, above, it’s happening because the ‘s’ variable is nil. You should not try to execute code when you know that a variable is going to be nil. Check the values and respond appropriately. If you are doing user input this way you’ll need a lot of checking of their input.

If you really need to do it, you can catch the “exceptions”. To catch exceptions in lua code you wrap it in a ‘pcall’:

[lua]if pcall(foo) then
– no errors while running foo' ... else -- foo’ raised an error: take appropriate actions

end[/lua]

You can read more about it here, but if you are trying to execute code which you know has exceptions you really should provide some logical checking of the values and avoid the nil variables.

http://www.lua.org/pil/8.4.html [import]uid: 8271 topic_id: 35064 reply_id: 139445[/import]

Thanks, i’ve already thought of that, but in my case this method would require me to do alot of variable checking, are you sure there is no other way, maybe someway to ignore the arithmetic message. the solution i use is to use timers so that the calculations happens after one another, it seems like lua then ignores the first arithmetic message so that the second one can be calculated.

and could you give me a example on how you would check and process the variables.

thanks again! [import]uid: 104309 topic_id: 35064 reply_id: 139449[/import]

It sounds like you’re writing a calculator app. If your code literally contains [lua]x = (a*b)/s[/lua] then you should be checking each variable for nil before you execute the calculation. If you are allowing the user to free-form their equations, then you’re going to need much greater value checking because you’ll never know what they are entering. I’ve not written a calc app, but I know it’s a bit of a nightmare.

At a stretch (but I still think this is the wrong approach) in your function “calculate()” you should be using pcall to wrap the individual calculations, like this:
[lua]local a = 6
local b = 4
local s = …
local i = 16
local t = 12
local f = 3

local function calculate()
local x, y
if (not pcall(function() x = (a*b)/s end) then
return “failed first”
end
if (not pcall(function() y = (i*t)/f end) then
return “failed second”
end
return “success”, x, y
end[/lua] [import]uid: 8271 topic_id: 35064 reply_id: 139454[/import]

thank you! it works pretty good! have a great saturday! [import]uid: 104309 topic_id: 35064 reply_id: 139590[/import]