![]() |
Days between dates - another solution - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Days between dates - another solution (/thread-154318.html) |
Days between dates - another solution - Bart (UK) - 08-13-2009 In the recent challenge a formula was provided for days beteen dates, but didn't work in some instances. Actually it won't work when dates include any century years not divisible by 400. Quote: A single failure, ie a single time that it does not work means the program's broken. OK, so flame me for being pedantic. However, I offer a solution. I thought to base my solution on finding the difference between Julian Days using the Julian Day formulae: a = INT[(14-month)/12] y = year+4800-a m = month + 12*a - 3 For a date in the Gregorian (current) calendar: JD = day + INT[(153*m+2)/5] + y*365 + INT[y/4] – INT[y/100] + INT[y/400] - 32045 (this is for whole days) Observations: As the JD numbering starts in 4800BC, and assuming that we're only interested in 0 AD onwards, discard the 4800 in the "y" formula. Also, as we're interested in the difference between 2 JD's and "-32045" is a common factor, discard it too. My first implementation is on the 32S, A01 LBL A B21 + B47 - This is 5 registers, 2 labels and 76 lines. I tried a mix of registers & code and this gives me the best combination (as numbers in lines seem to use lots of memory). 178 bytes calculated as (free mem before code and with cleared registers) subtract (free mem after coding, execution and leaving registers un-cleared). I thought a challenge would be implementing it on a 20S. Being algebraic, it takes more steps to do things. It has a fixed 99 step memory and the register memory is separate. Unfortunately I could not find a solution without having to pre-enter data on some registers (6 to 9 see below listing). 01 LBL A 34 IP 67 RCL 3 Pre-program the following numbers: 100 STO 9 153 STO 8 365 STO 7 400 STO 6 Executing: D1 INPUT D2 XEQ A Now I do not have an 11C, nor emulator, but I Imagine the code would look like this: 01 LBL A 30 5 59 RCL 3 It would get a score of 119 (87lines + 7*4registers + 2*2labels). I could probably have saved on registers but was worried about program length as I think the 11C has max 99 lines (not sure about this as it dynamically re-allocates memory between registers?). Closing observations: a = INT[(14-month)/12] gives 1 if month is <=2 and 0 if >2. Perhaps using flags could save some lines. I am not a great programmer, so the above could probably be optimised. It would be particularly useful to see solutions for the 20S not requiring pre-entering data in some registers. Bart
Re: Days between dates - another solution - Don Shepherd - 08-13-2009 Bart, good work. The challenge on the other thread noted that the algorithm in the 12c manual does not handle century years correctly, but since Katie and I had already developed 11c programs for that algorithm, we wanted to limit the entries to implementations of that specific algorithm. Here is a link to another algorithm that does work for all years. Katie found this one and it is relativey easy to implement.
Thanks for your interest. I wonder how many dbd algorithms out there acutally don't handle the century years correctly.
Re: Days between dates - another solution - hugh steers - 08-13-2009 Hi, you can simplify your formula a bit more. set m = month + 12*a + 1, then lose the 2/5 in the JD formula. This is what i did for the original problem, except i also chopped the y/100 and y/400 terms so as to get the wrong answer! -- hugh.
Re: Days between dates - another solution - Palmer O. Hanson, Jr. - 08-13-2009 I noted that the program for days between dates in my HP-12C correctly handles the leap year for years divisible by 100 with one exception -- the year 4000 when the modified Gregorian calendar calls for that year not to be a leap year. The program in ML-20 of the Master Library module for the TI-59 operates in the same way. I compared the equations for days between dates on pages 208-209 of the HP-12C manual with the equations on page 76 of the manual for the TI-59 Master Library module and found that they were the same except that the TI-59 version has an additional term to take care of the years divisible by 100. For March through December the additional term is INT(.75(INT(YYYY/100)+1)) For January and February the additional term is the same except that YYYY has been decreased by one. You can look at the ML-20 documentation at either Viktor Toth's or Joerg Woerner's site. I did down-load the ML-20 program, altered it to bypass the last terms and verified that the omission did cause a problem at years divisible by 100 but not divisible by 400. While looking at all of this I noted that both the HP-12C and TI-59 programs implement error trapping of the input such as not allowing the number of months to be greater than 12, etc. I don't know how much memory is used in the HP-12C for input error trapping. The entire TI-59 program for days between dates is 172 steps where 68 of those steps are used for input error trapping. Bart wrote "Flame me for being pedantic." Here's an example of really being pedantic. Way back when, some purist discovered that input error trapping in the TI-59 program limits the number of days of any month to 31, but does not limit the days of the months that do not have 31 days to the lesser number. Thus, the TI-59 program will return the number of days between January 1, 1900 and March 3, 1900 as 61 days, but will also return the number of days between January 1, 1900 and February 31, 1900 as 61 days. I suppose someone, but not me, could argue that February 31, 1900 is really the same as March 3, 1900. The HP-12C program will indicate "Error 8" if a user tries to enter a February 31 date.
Palmer
Re: Days between dates - another solution - Don Shepherd - 08-14-2009 Hi Palmer. Regarding the year 4000 not being a leap year, apparently that idea was never officially adopted by whomever it is that makes such rules. Reference. It seems that the Earth is slowing down somewhat, which may make that adjustment unnecessary by the time 4000 rolls around. Of course, none of us will be here to have to worry about it.
On the other hand, today's date (August 14) is a VERY special day: it's my birthday!
Re: Happy birthday Don - Bart (UK) - 08-14-2009 Happy 59th birthday! Re: Days between dates - another solution - Bart (UK) - 08-14-2009 Thank you all for your responses. Definitely some more food for thought - and options to try in programs. Re: Happy birthday Don - Don Shepherd - 08-14-2009 Thanks Bart. I wondered, "how did he know 59" and then I remember the dbd example I did in the other thread. The interesting thing about the 12c dbd algorithm in the manual is that it differs from the actual implementation, because the real 12c does handle years like 1900 and 1700 correctly, so it doesn't exactly follow the algorithm in its manual. In fact, the manual (in later versions) has a disclaimer beneath the algorithm: "Additional tests are performed in order to ensure that the century (but not millenium) years are not considered leap years." Unfortunately, the disclaimer is not exactly correct either; some century years are leap years (2400), and some are not (2300), and some millenium years are leap years (2000), and some are not (3000). I guess if I had to choose for either the code to be correct or the documentation, I'd choose the code. I wish HP had documented the actual algorithm they used in the manual, however.
I teach math (and social studies sometimes), and it's always fun to ask the kids why we even have leap years. I bring in a lamp (the Sun), put it in the middle of the room, turn out the lights, and then carry a globe in a circle around the room, rotating it as I go, to illustrate what really happens in the world with the southern and northern hemispheres. And I note the tilt of the Earth on its axis, which is the reason we have seasons and is the practical reason that leap years are necessary (because I want to be surfing in June, not building snowmen!).
Re: Happy birthday Don - Palmer O. Hanson, Jr. - 08-14-2009 Don: First, I will add my wishes for a happy birthday. I note that the TI-59 code does provide the desired result where years divisible by 100 are not leap years unless they are also divisible by 400 in which case the years are leap years. I have no idea whether or not the additional terms in the TI-59 implementation are optimized, and I can't recall an effort in the TI community to do so. Since the solution was in a module where memory was essentially not a limitation there would have been no real incentive to reduce keystrokes. Why not extend the challenge that Katie and you issued to include a complete solution for the HP-11C? Can such a solution be more efficient than the one that Bart proposed?
Palmer
Re: Happy birthday Don - Don Shepherd - 08-14-2009 Quote: That's a good idea. I'll start a new thread for that purpose. Bart, you can put a link to your 11c program in this thread in the new thread. Let's see what folks can do with a "correct" algorithm, that handles century years correctly. I may not post something myself, since I tend to use more registers and I don't like to keep a close check on what's in the stack. But we'll use the same rules for evaluation: lines of code + 7*registers + 2*labels.
Bart, I think in the 11c you can actually have up to 203 program lines, but you would have no registers to work with except the index register I!
Re: Happy birthday Don - Paul Dale - 08-16-2009 Quote: Yes, the 11c is limited to 203 lines. The 34c can goto 210. In both cases, you'd only have I left as a register. The reason for the difference? Most likely because the 11c has a random number generator which uses a register for its seed.
- Pauli
|