How to avoid a NAN - Not a Number response

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

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

How to avoid a NAN - Not a Number response

Post by dazza the stag » Sat May 31, 2014 9:45 pm

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

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: How to avoid a NAN - Not a Number response

Post by sefrojones » Sat May 31, 2014 11:22 pm

Would this work?

Code: Select all

   if fld "Mortgage1Result" is not a number then put 0.00 into fld "Mortgage1Result"

--Sefro

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: How to avoid a NAN - Not a Number response

Post by dazza the stag » Sat May 31, 2014 11:49 pm

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

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

Re: How to avoid a NAN - Not a Number response

Post by dunbarx » Sun Jun 01, 2014 1:30 am

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

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

Re: How to avoid a NAN - Not a Number response

Post by Klaus » Sun Jun 01, 2014 11:36 am

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! 8)
..
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...
:D


Best

Klaus

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: How to avoid a NAN - Not a Number response

Post by dazza the stag » Sun Jun 01, 2014 12:18 pm

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 :D )

I do find the dictionary difficult because of course if you don't know the name of the command it doesn't help.

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

Re: How to avoid a NAN - Not a Number response

Post by Klaus » Sun Jun 01, 2014 12:40 pm

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 8)

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 :D
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

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: How to avoid a NAN - Not a Number response

Post by dazza the stag » Sun Jun 01, 2014 1:04 pm

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
Screen Shot 2014-06-01 at 12.59.58.png
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

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

Re: How to avoid a NAN - Not a Number response

Post by Klaus » Sun Jun 01, 2014 1:42 pm

Hi Dazza,

maybe you can us the TRY structure?
This will let you TRY something and react in case of errors! :D

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

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: How to avoid a NAN - Not a Number response

Post by dazza the stag » Sun Jun 01, 2014 6:20 pm

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

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

Re: How to avoid a NAN - Not a Number response

Post by Klaus » Sun Jun 01, 2014 6:23 pm

I only copied some of your script for my example, not neccessarily making sense 8)
And of course you will need put the ELSE case into a TRY structure, too!

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: How to avoid a NAN - Not a Number response

Post by dazza the stag » Sun Jun 01, 2014 7:17 pm

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 ?

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

Re: How to avoid a NAN - Not a Number response

Post by [-hh] » Sun Jun 01, 2014 9:28 pm

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

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: How to avoid a NAN - Not a Number response

Post by dazza the stag » Sun Jun 01, 2014 9:58 pm

[-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 ?

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: How to avoid a NAN - Not a Number response

Post by trags3 » Sun Jun 01, 2014 10:29 pm

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

Post Reply