I coded a days between dates function...

Bugs in ND1 (report'em, get'em fixed)

I coded a days between dates function...

Postby d_motto » Wed Apr 18, 2012 11:15 am

Sometimes it works, and other times it doesn't work.

Here is what it looks like:
Code: Select all
function ageme() {
  s2 = Date.parse ( Date() )
  s1 = Date.parse ( "April 13, 1960 13:01:00 GMT-0400 (EDT)" )
  dayDiff = Math.abs(Math.round((s2-s1)/86400000))
return dayDiff;
}


When it doesn't work, it puts the program on level 1 of the stack. I am using the modern interface, and have the latest download of the software and downloadable packages.

I have a lot of experience coding for HP, TI, CASIO, SHARP and other calculators in various languages, but no experience in JavaScript or C. Do I need to declare a variable type or something?

Sometimes, when the program doesn't work, I recall it, store it again and it works. Other times it doesn't help. If I force-quit ND1, it might work after that (although the previous stack is restored, which might indicate another bug?).

Thank you for any input on why this might happen.
David Motto
d_motto
 
Posts: 35
Joined: Tue Apr 17, 2012 9:19 am

Re: I coded a days between dates function...

Postby d_motto » Wed Apr 18, 2012 8:00 pm

OK, I think I might know a little better what is happening when this situation occurs (it also affects the program I wrote that uses the factors function): If I upload my workspace, the Java program is put on line one instead of executing. Then, I can recall and re-store the function, and it will probably work. If it doesn't work, I can force-quit ND1, and when I bring it back up (it will probably be in another workspace and/or the stack will contain old values), it will probably work.

If I download the workspace after I upload it, the function will not work. So, the process of preparing it for upload is probably corrupting something.

One other think I have noticed about the Shared Folders: Every time I get in there it wants to download the Examples again.

I am running ND1 version 1.4.1.
Thank you,
David Motto
d_motto
 
Posts: 35
Joined: Tue Apr 17, 2012 9:19 am

Re: I coded a days between dates function...

Postby d_motto » Thu Apr 19, 2012 10:30 am

Now I don' believe that this is strictly due to uploading the workspace. I left the ND1 program in focus overnight, and when I tried it this morning, it did not work again. Recalling thee function and restoring it worked this time. Could this be a memory leak?

I may have noted is, but when the date difference function fails to run, the eval key doesn't work on it. Also, the factors utility returns the factors in the stack instead of as an array or vector. Fixing the function also makes the factors function operate correctly, which is weird.

More information... Running on an iPod touch 4th generation, 64 gig, about 4 gig free, iOS version 5.0.

David Motto
d_motto
 
Posts: 35
Joined: Tue Apr 17, 2012 9:19 am

Re: I coded a days between dates function...

Postby oliver » Thu Apr 19, 2012 11:09 am

Hi David,

Congrats on your first function!

Please try the following:
Code: Select all
function (year, month, day) { 
   var referenceDate = new Date(year, month, day);
   var today = new Date();
   return ((today-referenceDate)/(60*60*24*1000));
}


You fell into a quite usual trap with Date. It's important to use it with the new operator.
See
https://developer.mozilla.org/en/JavaSc ... jects/Date
and note the Note box.

Also, note:
    don't name your function in code; just type function() {...}, which makes it an anonymous function (and it's less typing); it will get its name when you store it
    use var to define local variables; if you just assign a variable they'll be created as part of the global window object; and they'll persist across invocations, which is not what you want
    if you want the function to take arguments, put them in as function arguments; they'll automatically be pulled from the stack; so the function above expects three arguments on the stack; if you don't specify any args, the function takes no inputs

I'm not following exactly on your symptoms but I assuming right now that these have been caused by a combination of slightly issues with the original code. Let me know how it goes.

If you have a specific issue with uploading code (from your computer; if I understand that part correctly), let me know which of the three upload methods in http://naivedesign.com/ND1/Computer_Link.html you're using.

Thanks for pointing out the issue with Examples. I had updated the version but not the examples folder yet. Corrected. (Due to server-side caching; it may take an hour or so before it takes effect.)

Happy coding!

Oliver
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: I coded a days between dates function...

Postby oliver » Fri Apr 20, 2012 7:20 am

Ah, I see now what's going on.

While "workspace" would be a good word, I didn't quite get what you did to get the problem you described. I now understand that you were uploading/downloading your "folder" (in ND1 speak).

Ok, here's your corrected code:
Code: Select all
function() {
  var s2 = Date.parse ( Date() )
  var s1 = Date.parse ( "April 13, 1960 13:01:00 GMT-0400 (EDT)" )
  var dayDiff = Math.abs(Math.round((s2-s1)/86400000))
return dayDiff;
}


Notes:
    There's no function name.
    var is used to define each local variable used.
(These are basically the same notes as above.)

You hit a real bug that only occurs if you name your function. That is, don't name your function and everything will be fine.
Uploading had nothing to do with the bug, but switching back to the calculator had.

If you're curious, here's what happens internally and an explanation for what you saw: when you store a function, it's parsed and made live in the calculator run-time. The parsing ignores any given function name. Also, the original function text is stored into a database.
Whenever you switch back to the calculator (and it doesn't matter, if you're coming from My Data, Definition, Help, or doing a fresh start of the calculator), the contents of the database are made live in the run-time. Now, the code that does that, does not ignore a given function name. Instead, it considers the given definition as faulty and just installs it as text. That's why the text of the function is shown to you when you attempt to execute it. (No error msg is given during database-to-run-time conversion, because if you had many "bad" functions like that, you'd be swamped with messages.)

So, what I'll do for the next version is flag an error when a named function is entered. That way no named functions can make it into the database.

Thank you for reporting this! I can see now how this can easily happen to people (incl. very experienced JavaScript programmers, who might consider it a good idea to name their function when they write it).

Unnamed (=anonymous) functions underlie JavaScript's concept of functions as data. This is a very neat and powerful concept, as it allow you to have functions that operate on functions.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: I coded a days between dates function...

Postby d_motto » Fri Apr 20, 2012 7:29 pm

First of all, I have been remiss in stating how great your program is. I've had a lot of fun learning to program some simple things on it, and I really love it.

Second, thanks for looking at my code and explaining what I was doing wrong. I have updated it to use "var" and drop the function name, and will keep tweaking it until I find exactly what I want.

One thing - I was a bit curious about the code you gave me to try out:
Code: Select all
function (year, month, day) {
   var referenceDate = new Date(year, month, day);
   var today = new Date();
   return ((today-referenceDate)/(60*60*24*1000));
}


I found that for dates a certain distance in the past, the number returned for days between dates was less than what it should be by around 30 days. I tried it for January 1, 1971 and it gave me 15054 and some fraction, instead of 15085 (from April 20, 2012). Is this a problem with Java? I am still learning!

Thank you for the link, as well!

David Motto
d_motto
 
Posts: 35
Joined: Tue Apr 17, 2012 9:19 am

Re: I coded a days between dates function...

Postby oliver » Sat Apr 21, 2012 11:33 am

Thank you, David, for your kind feedback! I'm very glad to hear you like ND1 and programming it.

I repeated your test with 1971, 1, 1 and, too, scratched my head over the result.
Turns out JavaScript expects the month to be a number from 0 to 11! (Yes, that's somewhat surprising.)

So, I guess the proper fix would be to adjust the function's second parameter by subtracting one from it.

(As an aside, array access in ND1's RPL+ is one-based, the default of math systems such as Mathematica and HP calculators (but not most programming languages), but there's a command to give you zero-based access as well.)

I moved your other topic into a newly-created section, Programming. I hope you don't mind.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: I coded a days between dates function...

Postby oliver » Wed Apr 25, 2012 4:38 pm

Something I forgot to mention:

Instead of writing
Code: Select all
 var dayDiff = Math.abs(Math.round((s2-s1)/86400000))

you can write
Code: Select all
  var dayDiff = abs(round((s2-s1)/86400000, 0))

Any real-valued ND1 function is available in JavaScript without specifying a namespace. So, a function like sin or ln can just be typed like that.
There are more functions than in Math, and some are improved. For example, Math.sin(0) doesn't return 0, while sin(0) does.
(Note that round() takes a parameter (the number of after point digits to round to), whereas Math.round() doesn't.)

If you want to use a function for a different data type, you do have to provide a namespace.
For example, vector for all Array functions, as in
Code: Select all
var sumOfPrimes = vector.total(primes(100))

Cheers.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm


Return to Bugs

Who is online

Users browsing this forum: No registered users and 1 guest

cron