Using a given result

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

ninjabunny14
Posts: 27
Joined: Wed May 29, 2013 10:32 am

Using a given result

Post by ninjabunny14 » Thu Jun 06, 2013 6:19 pm

Hi guys

I am nearly finished with my whole app but the last part where I seem to be forever going wrong won't work.
The basic idea is for the imputed price to be used to work out how big the percentage should be based on the price and then multiply the price by the percentage and for it to be displayed in a field.
This is probably a maths question because of the results I get.

Here is the whole thing:
Card "Stamp Duty Calculator" Script:

Code: Select all

function calculatePercentage pType, pPrice
   -- the parameters passed to this function have been given more meaningful names
   
   -- check if the pType parameter was "Private" or not
   if pType = "Private" then
      -- yes it is, so do our other checks on pPrice and return a value accordingly
      if pPrice > 2000000.01 then
         return (7/100)
      else if pPrice > 1000000.01 then
         return (5/100)
      else if pPrice > 500000.01 then
         return (4/100)
      else if pPrice > 250000.01 then
         return (3/100)
      else
         return (1/100)
      end if
   else
      -- no pType was not "Private" so return the appropriate value
      return 15
   end if
   
end calculatePercentage

on Calculate
   put (fld "UO" * (45/100)) into field "ITS fee"
   put (fld "ITS Fee" * (20/100)) into field "VAT"
   put (fld "ITS Fee" + fld "VAT") into fld "Total Fee"
   put (fld "UO" - fld "Total Fee") into fld "Savings"
end Calculate
Button code:

Code: Select all

on mouseUp
   -- get the values from the field and the menu button label and put into variables
   -- simply to make it easier to pass them to the function
   put the label of button "Purchase Type" into tPurchaseType
   put field "Price Input" into tPrice
   
   -- pass those values to the function (which is on the card script so all the buttons on the card can use it)
   -- and take the returned value from the function
   -- and store it in the destination field 
   put calculatePercentage (tPurchaseType, tPrice) into field "UO %age"
   -- this sends the values in variables tPurchaseType and tPrice to the calculatePercentage function
   -- gets the value returned from the function and puts it in the field
   put (fld "Price Input" * fld "UO %age") into field "UO"
   send "Calculate" to card "Stamp Duty Calculator"
   
end mouseUp
I can't believe I still have something wrong I am so incompetent. I tried 3 things with with this:
1) I added the calculate code to the button instead
2) I added it to the card (as is above)
3) I changes the outcomes of the if statement so that the code would follow BODMAS (as is above)

All 3 got different results. Where have I gone wrong please anyone?

ninjabunny14 :) ... still going...
Getting there... Very slowly ;)

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

Re: Using a given result

Post by Klaus » Thu Jun 06, 2013 6:31 pm

Hi ninjabunny14,

1. It is bad style to create a new thread for an already existing and posted problem only because noone answered so far:
http://forums.runrev.com/phpBB2/viewtop ... 387#p77358

2. I have absolutely no idea what you mean with "All 3 got different results" nor what exactly your problem is?

3. What is BODMAS?


Best

Klaus

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

Re: Using a given result

Post by jacque » Thu Jun 06, 2013 6:36 pm

What were the three different results? If they were close, it may be a consequence of rounding errors.

Give us some test values and we can run your script and see what we get. I don't see anything immediately wrong with the handlers themselves. We need to see some actual test data.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am

Re: Using a given result

Post by icouto » Fri Jun 07, 2013 7:46 am

Klaus wrote:3. What is BODMAS?
A mnemonic device used to remember the 'right' order of operations in arithmetic:

Anything in Brackets first,
followed by numbers of higher Orders (exponents),
then Division and Multiplication,
and finally, Addition and Subtraction.

Brackets, Orders, Division, Multiplication, Addition, Subtraction = BODMAS. :)

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a given result

Post by Simon » Fri Jun 07, 2013 8:26 am

There was another maths mnemonic posted here recently.
Something about "Aunt Mary something something."
If we could get those both together I'd actually write them down.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Using a given result

Post by Klaus » Fri Jun 07, 2013 10:05 am

AHA! Thanks for clarification!

Looks like this is not really important to know for NON native english speakers :-)

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

Re: Using a given result

Post by dunbarx » Fri Jun 07, 2013 6:01 pm

PEMDAS

Parenthesis,exponents,mult/divide.add/subtract.

Common in the United States, where we assume (and expect that) everyone on the planet speaks English.

Craig Newman

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a given result

Post by Simon » Fri Jun 07, 2013 8:17 pm

Thanks Craig,
Now I have written both down FWIW. :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ninjabunny14
Posts: 27
Joined: Wed May 29, 2013 10:32 am

Re: Using a given result

Post by ninjabunny14 » Sat Jun 08, 2013 11:03 am

ok thanks guys. a test value would be price = 525000
should read UO %age: 4%
UO: 21000
ITSfee: 9450
VAT: 1890

sorry i keep reposting this I have a serious deadline that i am already overrunning :(
Getting there... Very slowly ;)

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a given result

Post by Simon » Sat Jun 08, 2013 8:42 pm

Hi nija,
Any chance you can post your stack?
Too many controls for me, to replicate what you are seeing.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Using a given result

Post by jacque » Sat Jun 08, 2013 9:53 pm

I couldn't repro all the stack controls either but when I substituted variables instead of fields I got consistent results. I agree that a sample stack with everything laid out already would be much easier to diagnose.

This hasn't anything to do with the immediate problem, but you don't need to "send calculate" to the card. The button is on the card and the card is in the hierarchy, so it will receive the message. The line only needs to read "calculate". Since I don't have your card layout, I changed the button script to this for testing:

Code: Select all

on mouseUp
  put "private" into tPurchaseType
  put 525000 into tPrice
  put calculatePercentage (tPurchaseType, tPrice) into percnt
  put (tPrice * percnt) into UO
  calculate tPurchaseType,tPrice
end mouseUp
I did something similar for your other handlers so I wouldn't need to reproduce all your fields. Putting data into fields and getting it back out is one of the slowest operations in LiveCode, so it's better to work directly with variables whenever possible. Grab the field data you need, calculate it, pass the values to other functions and handlers, and only put the data back into the fields when everything is done.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

ninjabunny14
Posts: 27
Joined: Wed May 29, 2013 10:32 am

Re: Using a given result

Post by ninjabunny14 » Sun Jun 09, 2013 11:30 am

Hi all. Thank you for your support. I have uploaded a lite version for you guys to work with. You can see the expected results in my previous posts.

Thank you for all the support,

ninjabunny14 :) :)
Attachments
Calculator - lite.zip
Lite version of my calculator (unzip first).
(2.76 KiB) Downloaded 346 times
Getting there... Very slowly ;)

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

Re: Using a given result

Post by Klaus » Sun Jun 09, 2013 12:35 pm

Code: Select all

on preOpenStack
   send "resizeStack" to card "Stamp Duty Calculator"
   wait 0.05 seconds
   send "resizeStack" to card "Stamp Duty Calculator"
   wait 0.05 seconds
   send "resizeStack" to card "Stamp Duty Calculator"
end preOpenStack
ERROR: No such card "Stamp Duty Calculator"!
And three times "resizestack"? You don't have any "resizestack" handler!
Come on, are you kidding?

ninjabunny14
Posts: 27
Joined: Wed May 29, 2013 10:32 am

Re: Using a given result

Post by ninjabunny14 » Sun Jun 09, 2013 4:07 pm

yes I know its a shrunk down version I forgot to remove all the code that was in the new version. you can choose to ignore it
Getting there... Very slowly ;)

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using a given result

Post by Simon » Sun Jun 09, 2013 7:31 pm

ok thanks guys. a test value would be price = 525000
should read UO %age: 4%
UO: 21000
ITSfee: 9450
VAT: 1890
That's what I get using your stack, except the decimal place in ITSfee and there is no VAT field.
Show me an example of where it goes wrong.
Or is it just a formatting issue, where you want it to read "4%" and "9450"?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply