[SOLVED] Simple math
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
[SOLVED] Simple math
First time user of LiveCode.
Trying to understand the syntax.
If I have 2 text fields and a calculate button.
kgs is for kilograms
lbs is for pounds
If the user enters a number into the kgs field, I want to divide that number by 2.2 and display it in the lbs field when the user clicks the CalcBtn. I want to round it to 2 decimal places.
Another question:
How do you check for divide by zero and correct for that?
Is there a way to check for nil, null to give an error message?
What is the syntax for this?
Linda
Trying to understand the syntax.
If I have 2 text fields and a calculate button.
kgs is for kilograms
lbs is for pounds
If the user enters a number into the kgs field, I want to divide that number by 2.2 and display it in the lbs field when the user clicks the CalcBtn. I want to round it to 2 decimal places.
Another question:
How do you check for divide by zero and correct for that?
Is there a way to check for nil, null to give an error message?
What is the syntax for this?
Linda
Last edited by anmldr on Thu Sep 20, 2012 3:57 pm, edited 1 time in total.
Re: Simple math
Hi, Linda, and welcome here.
There are a number of ways to do error checking like this... probably the easiest is to check for a zero before doing the division <g>
but you can also do something like this to catch the division errors after the fact
and checking for nil is just checking for empty...
There are a number of ways to do error checking like this... probably the easiest is to check for a zero before doing the division <g>
Code: Select all
if tDividend is not 0 then
-- do the divisiion here
end if
Code: Select all
try
-- do the division here
catch e
if tDividend is 0 then
answer "you tried to divide by zero"
end if
end try
Code: Select all
if tDivisor is empty then
-- don't do the division here
end if
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Simple math
Try (without the above error checking)
Code: Select all
on mouseUp
set the numberFormat to "#.00"
put field "kgs" / 2.2 into field "lbs"
end mouseUp
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Simple math
Then I am missing something *very* basic because that did not work for me.
Steps:
File Menu > New Mainstack
Drag 2 fields onto the new Mainstack.
Name 1 of them kgs
Name 1 of them lbs
Drag Push Button onto new Mainstack and name it CalcBtn
Bring up code for CalcBtn and enter:
on mouseUp
set the numberFormat to "#.00"
put field "kgs" / 2.2 into field "lbs"
end mouseUp
Choose the Run button (not the edit button)
Enter a number into the kgs field
Click the CalcBtn
Nothing happens in the lbs field.
So, what step am I missing? Did I need to save the project first? Do I need a card below the Mainstack? Something else?
Steps:
File Menu > New Mainstack
Drag 2 fields onto the new Mainstack.
Name 1 of them kgs
Name 1 of them lbs
Drag Push Button onto new Mainstack and name it CalcBtn
Bring up code for CalcBtn and enter:
on mouseUp
set the numberFormat to "#.00"
put field "kgs" / 2.2 into field "lbs"
end mouseUp
Choose the Run button (not the edit button)
Enter a number into the kgs field
Click the CalcBtn
Nothing happens in the lbs field.
So, what step am I missing? Did I need to save the project first? Do I need a card below the Mainstack? Something else?
Re: Simple math
No, that should work. Nothing wrong with the code or the creation procedure. The only thing I can think of that's missing is the compilation step. Did you click the Apply button before trying to run the code?
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Simple math
Or do you have messages turned off? Check the main toolbar and make sure the messages button has not been clicked. If it has, toggle messages back on.
Re: Simple math
I knew that it had to be something simple. That was it. Apply button made it work.
Thanks,
Linda
Thanks,
Linda
Re: Simple math
Yay! You're on your way. Be patient with it - it'll take about six weeks before all this sinks in.
Then there's no going back.
Then there's no going back.

PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Simple math
I went back to your answer about divide by zero checking.
I understand that you do not have to formally declare a variable like tDividend other than what you did in the if statement.
However, what I don't understand is how LiveCode "knows" that tDividend would be the dividend in the equation.
Linda
Code: Select all
if tDividend is not 0 then
-- do the divisiion here
end if
However, what I don't understand is how LiveCode "knows" that tDividend would be the dividend in the equation.
Linda
Re: Simple math
Sorry - I was trying to jot down some stuff quickly without putting in a lot of code (note to self: don't do that). In your particular example there's no possibility of a zero divisor because you're explicitly using 2.2. However in a more generic case for a division
You're correct in that you don't have to declare variables (unless you have that option turned on in preferences) but I *do* have that option turned on in preferences and I *always* declare my variables. It helps keep me out of trouble. There are two schools of thought on this (don't get me started...) and I believe the other side of the argument is that the freedom of *not* having to declare variables makes coding easier and more natural-reading.
Me, I like to let the compiler help me find mistakes rather than have to go hunting them down at runtime. YMMV.
Code: Select all
function SafeDivision pDividend, pDivisor
local tQuotient
if pDivisor is not 0 then
put pDividend / pDivisor into tQuotient
else
answer "you tried to divide by zero!"
put "invalid" into tQuotient
end if
return tQuotient
end SafeDivision
put SafeDivision(454, 3) into field "lbs"
Me, I like to let the compiler help me find mistakes rather than have to go hunting them down at runtime. YMMV.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev