[SOLVED] Simple math

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

[SOLVED] Simple math

Post by anmldr » Wed Sep 12, 2012 8:58 pm

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
Last edited by anmldr on Thu Sep 20, 2012 3:57 pm, edited 1 time in total.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Simple math

Post by mwieder » Wed Sep 12, 2012 9:16 pm

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>

Code: Select all

if tDividend is not 0 then
  -- do the divisiion here
end if
but you can also do something like this to catch the division errors after the fact

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
and checking for nil is just checking for empty...

Code: Select all

if tDivisor is empty then
  -- don't do the division here
end if

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Simple math

Post by mwieder » Wed Sep 12, 2012 9:36 pm

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

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: Simple math

Post by anmldr » Wed Sep 12, 2012 9:59 pm

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?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Simple math

Post by mwieder » Wed Sep 12, 2012 10:07 pm

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?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Simple math

Post by sturgis » Wed Sep 12, 2012 10:10 pm

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.

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: Simple math

Post by anmldr » Wed Sep 12, 2012 10:14 pm

I knew that it had to be something simple. That was it. Apply button made it work.

Thanks,
Linda

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Simple math

Post by mwieder » Wed Sep 12, 2012 10:16 pm

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. :P

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: Simple math

Post by anmldr » Wed Sep 12, 2012 10:18 pm

I went back to your answer about divide by zero checking.

Code: Select all

if tDividend is not 0 then
  -- do the divisiion here
end if
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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Simple math

Post by mwieder » Wed Sep 12, 2012 10:52 pm

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

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"
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.

Post Reply