difference between number variables

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
coenzyme
Posts: 5
Joined: Thu Apr 17, 2014 9:20 pm

difference between number variables

Post by coenzyme » Wed Apr 23, 2014 2:45 pm

I am trying to find the difference between 2 variables.

Say I have one variable which has a value of 10 and a second variable that has a value of 12.

The code:

Code: Select all

Subtract variable1 from variable2
does not work.

I just need variable2 or a third variable to contain the difference. In the above case I would like a variable containing 2.

Is there a function that compares two variable and returns the difference?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: difference between number variables

Post by Dixie » Wed Apr 23, 2014 2:50 pm

Code: Select all

on mouseUp
   put 10 into var1
   put 12 into var2
   put var2 - var1 into theanswer
   
   put theanswer
end mouseUp

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: difference between number variables

Post by Klaus » Wed Apr 23, 2014 2:51 pm

Hi coenzyme,

"does not work" ERROR: Invalid error description! :D

Hm, just made a test with a button:

Code: Select all

on mouseUp
   put 10 into variable1
   put 12 into variable2
   Subtract variable1 from variable2
   put variable2
end mouseUp
gives me 2 in the message box, so this definitively works!
Could you please post the rest of your script(s)?


Best

Klaus

coenzyme
Posts: 5
Joined: Thu Apr 17, 2014 9:20 pm

Re: difference between number variables

Post by coenzyme » Wed Apr 23, 2014 2:52 pm

Easy enough. Thanks!!

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: difference between number variables

Post by magice » Wed Apr 23, 2014 2:56 pm

I love writing functions. This will give you the difference regardless of which variable is higher.

Code: Select all

function compareVariables x y
  If x>y 
   then
    put x-y into tResult
   else
    put y-x into tResult
  end if
return tResult
end CompareVariables

coenzyme
Posts: 5
Joined: Thu Apr 17, 2014 9:20 pm

Re: difference between number variables

Post by coenzyme » Wed Apr 23, 2014 2:59 pm

I am comparing two times

so when the card opens I have the code

Code: Select all

On cardOpen
get the seconds
put it into tStart
end cardOpen
I then have a button with the code in that card:

Code: Select all

On mouseUp
get the seconds
put it into tClick
Subtract tClick from tStart
End mouseUp
I am trying to find the time elapsed between the card opening and when the user clicks the button.

I am not with my code and I don't remember the error, but I can look it up when I get home.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: difference between number variables

Post by magice » Wed Apr 23, 2014 3:02 pm

I think your problem is variable persistence. Look up global and local in the dictionary.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: difference between number variables

Post by Klaus » Wed Apr 23, 2014 3:13 pm

AHA!
What magice says! :D

Do this in the card script:

Code: Select all

global tStart

## Actually it is: OPENCARD
On opencard

  ## No need to GET anything, you can put everything directly into a variable!
  put the seconds into tStart
end opencard
and this in your button(s):

Code: Select all

global tStart
On mouseUp
  put the seconds into tClick
  Subtract tClick from tStart
End mouseUp
If you do not define a variable as either LOCAL or GLOBAL, it will be gone after your handler has finished.


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: difference between number variables

Post by dunbarx » Wed Apr 23, 2014 3:50 pm

Magice,

You can also use your handler with the "abs" function, just to make it a bit shorter:

Code: Select all

function compareVariables x y
 return abs(x-y)
end CompareVariables

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: difference between number variables

Post by magice » Wed Apr 23, 2014 3:53 pm

dunbarx wrote:Magice,

You can also use your handler with the "abs" function, just to make it a bit shorter:

Code: Select all

function compareVariables x y
 return abs(x-y)
end CompareVariables
You just took all the fun out of it :?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: difference between number variables

Post by Klaus » Wed Apr 23, 2014 4:12 pm

You have a real strange idea of fun! :D

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: difference between number variables

Post by [-hh] » Wed Apr 23, 2014 4:17 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 12:59 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: difference between number variables

Post by dunbarx » Wed Apr 23, 2014 4:19 pm

Klaus.

I completely get what Magice meant. Jacque has been doing this sort of thing to me for 25 years.

Craig

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: difference between number variables

Post by magice » Wed Apr 23, 2014 5:16 pm

Klaus wrote:You have a real strange idea of fun! :D
Sometimes it's fun to reinvent the wheel, just like an artist copying the works of the masters or a musician covering someone else song. Then again, I think a clock with the gears on the outside is more fun than one with them on the inside, and I like hot rods that leave the hood off so you can see the engine, so maybe I am just strange.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: difference between number variables

Post by jacque » Thu Apr 24, 2014 4:26 pm

dunbarx wrote:Klaus.

I completely get what Magice meant. Jacque has been doing this sort of thing to me for 25 years.

Craig
Only because you're such a good sport about it. May we have 25 more. :-)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply