Page 1 of 2

Using a given result

Posted: Thu Jun 06, 2013 6:19 pm
by ninjabunny14
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...

Re: Using a given result

Posted: Thu Jun 06, 2013 6:31 pm
by Klaus
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

Re: Using a given result

Posted: Thu Jun 06, 2013 6:36 pm
by jacque
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.

Re: Using a given result

Posted: Fri Jun 07, 2013 7:46 am
by icouto
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. :)

Re: Using a given result

Posted: Fri Jun 07, 2013 8:26 am
by Simon
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

Re: Using a given result

Posted: Fri Jun 07, 2013 10:05 am
by Klaus
AHA! Thanks for clarification!

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

Re: Using a given result

Posted: Fri Jun 07, 2013 6:01 pm
by dunbarx
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

Re: Using a given result

Posted: Fri Jun 07, 2013 8:17 pm
by Simon
Thanks Craig,
Now I have written both down FWIW. :)

Simon

Re: Using a given result

Posted: Sat Jun 08, 2013 11:03 am
by ninjabunny14
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 :(

Re: Using a given result

Posted: Sat Jun 08, 2013 8:42 pm
by Simon
Hi nija,
Any chance you can post your stack?
Too many controls for me, to replicate what you are seeing.

Simon

Re: Using a given result

Posted: Sat Jun 08, 2013 9:53 pm
by jacque
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.

Re: Using a given result

Posted: Sun Jun 09, 2013 11:30 am
by ninjabunny14
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 :) :)

Re: Using a given result

Posted: Sun Jun 09, 2013 12:35 pm
by Klaus

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?

Re: Using a given result

Posted: Sun Jun 09, 2013 4:07 pm
by ninjabunny14
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

Re: Using a given result

Posted: Sun Jun 09, 2013 7:31 pm
by Simon
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