Page 1 of 2
How to avoid a NAN - Not a Number response
Posted: Sat May 31, 2014 9:45 pm
by dazza the stag
I have the following code
If hilite of button id 1208 = False then
put Field MortgageMonths1Input+(Field MortgageYears1Input*12) into MortgageTerm1
put Field MortgageRate1Input into MortgageRate1
put (MortgageRate1 / 100) /12 into MonthlyMortgageRate1
put Field MortgagePrinciple1Input/annuity( MonthlyMortgageRate1,MortgageTerm1 ) into MortgageAnswer
put round(MortgageAnswer,2) into field Mortgage1Result
put Field Mortgage1Result * MortgageTerm1 into total1
else
put Field MortgageMonths1Input+(Field MortgageYears1Input*12) into MortgageTerm1
put (Field MortgagePrinciple1input * Field MortgageRate1Input)/100/12 into Field Mortgage1Result
put Field Mortgage1Result * MortgageTerm1 + Field MortgagePrinciple1Input into total1 end if
My problem is if the user doesn't put a number into one of the fields then in the Field Mortgage1Result it returns "NAN"
I don't really want to create a message to the user asking them to input a number I just want to ensure that the Field Mortgage1Result shows as a "0.00" instead of NAN as otherwise a calculation further down also cent work.
I have tried putting a line in to the effect "if Field Mortgage1Result = "Nan" then put "0.00" into Field Mortgage1Result but that and various alternatives on the same theme have worked
Is there a command I should be looking at in the dictionary to do this ?
And just as an add on - is there anyway to make the Check Box bigger ? it looks so tiny on the iPhone and when i try to resize it it just creates a bigger space around the box
thank you as always
Re: How to avoid a NAN - Not a Number response
Posted: Sat May 31, 2014 11:22 pm
by sefrojones
Would this work?
Code: Select all
if fld "Mortgage1Result" is not a number then put 0.00 into fld "Mortgage1Result"
--Sefro
Re: How to avoid a NAN - Not a Number response
Posted: Sat May 31, 2014 11:49 pm
by dazza the stag
sefrojonesGAda40 wrote:Would this work?
Code: Select all
if fld "Mortgage1Result" is not a number then put 0.00 into fld "Mortgage1Result"
--Sefro
Thanks Sefro
I tried along those lines but I think i might have been putting it in the wrong place - Its late now and I will try again tomorrow with fresh eyes
Thank you
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 1:30 am
by dunbarx
Sefro had right idea. Perhaps the other argument is not a number?
Step through your handler. Just before the offending line, place two new lines that will extract the values of the two arguments. This is just general practice. Check to see if they are both numbers. DO NOT just read them, beacause all sorts of invisible characters can attach themselves to what seems to be ordinary data.
Craig Newman
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 11:36 am
by Klaus
Hi Dazza,
and do yourself a favour and start putting QUOTES around strings and use SPACEs!
That will make your script even better readable, because STRINGS are easier recognized
than variable names on first glance!
Your script looks as if there are only variables being used and is hard to read!
..
put Field MortgageMonths1Input+(Field MortgageYears1Input*12) into MortgageTerm1
put Field MortgageRate1Input into MortgageRate1
put Field MortgageMonths1Input+(Field MortgageYears1Input*12) into MortgageTerm1
put (Field MortgagePrinciple1input * Field MortgageRate1Input)/100/12 into Field Mortgage1Result
put Field Mortgage1Result * MortgageTerm1 + Field MortgagePrinciple1Input into total1
...
Better:
...
put fld "MortgageMonths1Input" + (fld "MortgageYears1Input" * 12) into MortgageTerm1
put fld "MortgageRate1Input" into MortgageRate1
put fld "MortgageMonths1Input" + (fld "MortgageYears1Input" * 12) into MortgageTerm1
put (fld "MortgagePrinciple1input" * fld "MortgageRate1Input")/100/12 into fld "Mortgage1Result"
put fld "Mortgage1Result" * MortgageTerm1 + fld "MortgagePrinciple1Input" into total1
...
And you will gain speed!
If you do not put quotes around object names etc. the engine will first check if there is a
VARIABLE with that name and THEN evaluate this a an object name!
Use abbreviations if you really need to save some typing:
card = cd
field = fld
button = btn
group = grp
etc...
Best
Klaus
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 12:18 pm
by dazza the stag
Thanks Klaus - that is very informative, I had seen others, some using quotes and spaces and some not, I just thought it was personal preference, I had no idea it made a difference to the speed.
Craig - thank you for that idea, without telling me how to do it as Im getting a little embarrassed at having to keep asking you guys for help, where can I go to learn about how to carry out your suggestion, i.e. I need to understand where do i find the Handler and what commands to put into those to lines to tell me the outputs so far. When I was sat there last night as you say I was just reading and reading through the script trying to calculate manually what the script should have been calculating to see where the error was but its not easy.
I think I got to the point of deciding that if there was no input from the user then clearly it was struggling to perform a calculation i.e. trying to divide by 0 for example. so there must be a way of saying if there isn't an input in a certain field then skip that part of the script. SO I spent ages looking for a command that would check to see what the value of a field was and if it was empty to simply jump to a different line of script in order to miss out the offending calculations - but I couldn't find any reference to being able to do that. something along the lines of
If Field "MortgagePrinciple1Input" = "" then go to line 25 ( casting my mind back to GSCE Computer Studies 25 years ago in BBC Basic language I'm sure you could do that then lol

)
I do find the dictionary difficult because of course if you don't know the name of the command it doesn't help.
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 12:40 pm
by Klaus
Hi Dazza,
I ususally write a little command or function that will check all involved fields for their correct content!
Has saved may nerves over the years, but not my hair unfortuantely
Something like this, will only check for EMPTY, but can of course be modified to also check for NUMBERS or whatever:
Code: Select all
function check4input
put "MyField1,MyField2,MyField3" into tFields
repeat for each item i in tFields
if fld i = EMPTY then
return false
end if
end repeat
# No empty field:
return TRUE
end check4Input
Then BEFORE you do your calcualtions:
Code: Select all
on mouseup
if check4Input() = FALSE then
answer "Please fill out all fields!"
exit mouseup
end if
## Now you can do your calcualtions, you get the picture.
##...
end mouseup
Another speed hint
Accessing FIELDS has some overhead, that you can minimize by putting the content of fields into variables first!
You should do this whenever you need to use the content of a field more than once in any command/calculation,
which is the case in your example.
Best
Klaus
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 1:04 pm
by dazza the stag
O I see so
put Field "MortgagePrinciple1Input" into MortgagePrinciple1
turns that field into a variable and therefore speeds things up for future ref - excellent thank you
Remember what I am trying to get to though is that if a user doesn't complete a certain box or I don't really want to ask him to fill it out, I just want the script to then ignore that calculation so rather than
on mouseup
if check4Input() = FALSE then
answer "Please fill out all fields!"
exit mouseup
end if
## Now you can do your calcualtions, you get the picture.
##...
end mouse up
I would want to replace the line "answer........" with one that simply instructs the app to therefore ignore that line of data and move on - is that doable i.e. can i refer directly to the line numbers in a script and ask it to jump certain lines ? Ive looked at commands that allow it to jump cards but can't find one to jumps lines of script
to give some background the screen shot shows the app, its to calculate a mortgage payment where there might be several different parts to the same mortgage. However there may only be 1 or 2 parts and therefore the user wouldn't want to fill out line 3 4 and 5 or indeed for one reason or another they may fill out 1 2 and 3 but then decide to delete the data in line 2, I would still like the app to perform the calculation without the user having to move the data they have inputted from line 3 to line 2, but rather leaving line 2 blank
I do appreciate your patience
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 1:42 pm
by Klaus
Hi Dazza,
maybe you can us the TRY structure?
This will let you TRY something and react in case of errors!
Something like this:
Code: Select all
...
TRY
put fld "MortgageMonths1Input" + (fld "MortgageYears1Input" * 12) into MortgageTerm1
put fld "MortgageRate1Input" into MortgageRate1
put fld "MortgageMonths1Input" + (fld "MortgageYears1Input" * 12) into MortgageTerm1
put (fld "MortgagePrinciple1input" * fld "MortgageRate1Input")/100/12 into fld "Mortgage1Result"
put fld "Mortgage1Result" * MortgageTerm1 + fld "MortgagePrinciple1Input" into total1
## "catch" eventual ERRORS, which may be cause by empty fields or strings instead of numbers:
CATCH tErr
## And do something in that case:
put "0.00" into total1
END TRY
put total1 into fld "whatever"
...
Best
Klaus
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 6:20 pm
by dazza the stag
Thank you Klaus
That Try Structure certainly seems to be the one I need - I have read up on it and tried to implement into my script. By copying and pasting the script you wrote into mine it works well in regard to changing the error from Nan to 0.00, however you have changed some of the command lines and therefore the actually calculation now doesn't come out with the right answer.
I changed the calculations back again to get the correct result but that creates the NAN again. So clearly you saw something in my script that doesn't work with the TRY command hence why I am guessing you changed it - what was it please ?
This is how i amended mine to include the Try command with the correct calculation but still results in NAN
If hilite of button id 1208 = False then
TRY
put Field "MortgageMonths1Input" + (Field "MortgageYears1Input" * 12) into MortgageTerm1
put Field "MortgageRate1Input" into MortgageRate1
put (MortgageRate1 / 100) /12 into MonthlyMortgageRate1
put Field "MortgagePrinciple1Input"/annuity( MonthlyMortgageRate1,MortgageTerm1 ) into MortgageAnswer
put round(MortgageAnswer,2) into field "Mortgage1Result"
put Field "Mortgage1Result" * MortgageTerm1 into total1
## "catch" eventual ERRORS, which may be cause by empty fields or strings instead of numbers:
CATCH tErr
## And do something in that case:
put "0.00" into Field "Mortgage1Result"
END TRY
else
put Field "MortgageMonths1Input" + (Field "MortgageYears1Input" * 12) into MortgageTerm1
put (Field "MortgagePrinciple1input" * Field "MortgageRate1Input")/100/12 into Field "Mortgage1Result"
put Field "Mortgage1Result" * MortgageTerm1 + Field "MortgagePrinciple1Input" into total1 end if
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 6:23 pm
by Klaus
I only copied some of your script for my example, not neccessarily making sense
And of course you will need put the ELSE case into a TRY structure, too!
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 7:17 pm
by dazza the stag
ok - i don't like to admit it but this one has beat me.
I accept the bit about putting the Try structure around the Else command but I know that this bit isn't being used at the moment so its not the else command that is creating the NAN
The only difference between your bit of script that is working ( i.e. no NAN ) but is generating the wrong calculation result and my script that is creating the right result but if there is a blank field a NAN is that mine has the "annuity" command in there and slightly different formulas. Apart from that they look the same in structure.
So my guess is that the Try command somehow doesn't work with the annuity function - could that be an issue ?
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 9:28 pm
by [-hh]
..........
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 9:58 pm
by dazza the stag
[-hh] wrote:You multiply by infinity or divide by zero respectively.
Annuity requires a positive interest rate (= a positive number) and a positive integer for the number of periods. If one of these is not true, then it doesn't make sense to compute it.
Tip: Start with field values that make sense, this is not in all situations empty.
For example if the amount required is 0 then the monthly payment is 0, no matter what years, months or interest rate are.
Or if every monthly payment is 0 then the total payment is 0, the total interest is 0 and the total payable is 0. You don't need a computation for this, just fill in.
And now *compute* the payment if and only if all conditions are given. Check them, one after the other. Especially allow an amount required > 0.00 if and only if the other fields are correctly filled.
Hi HH
I think I understand what you are saying and I do realise why I'm getting the Nan since working through previous replies. I think where I am struggling now is if for instance someone does leave a field blank how I can get a "0.00" result to go into the Mortgage1Result Field instead of the Nan message
Klaus has been very patient with with and told me about the Try command, which seems to work on the script he provided but as soon as I included the annuity command it reverted back to giving the Nan response. I don't really want to just give the user a message asking for input I just want to give that calculation line a "0.00" output
Does that make sense ?
Re: How to avoid a NAN - Not a Number response
Posted: Sun Jun 01, 2014 10:29 pm
by trags3
Here is how I insure there is a number in my number input fields. The code for the field :
on keyDown theKey
if theKey is not a number then beep
else pass keyDown
end keyDown
For fields that might have a decimal point in them I use this:
on keyDown theKey
if "." is not in field "commissionp"
then
if theKey is not a number and theKey is not "." then beep
else pass keyDown
end if
if theKey is not a number then beep
else pass keyDown
end keyDown
This allows 1 decimal point but not more than one.
on closeField &/or exitField you could also check to see if the field is empty & if so put 0 into it.
It works for me.
Tom