Tuesday, January 21, 2014

HP Prime and HP 32SII: Day Number of the Year

HP Prime and HP 32S: Day Number of the Year

This formula calculates the day number of any date (given month and day). A 365 day year is used. If you are operating with a leap year with dates after February 28, add 1.

Hence in non-leap years, January 1 is Day 1, February 1 is Day 32, etc..., December 1 is 335, and December 31 is 365.

Derivation

Formula Used: Gregorian Calendar

Let: Y = year, D = day, and M = month. Then:

N = IP(365T) + IP(T/4) - IP(T/100) + IP(T/400) + D + IP((153X + 8)/5)

where IP represents the integer portion of number and:

if M≤2, T=Y-1, X=M+12,
otherwise T=Y and X=M.

Source: Dr. Math ( http://mathforum.org/library/drmath/view/66857.html )

To find the day number, use the above formula to get the "Gregorian" day number (better term?) for the date in question and subtract the "Gregorian" day number for January 1. This gives the number of days between January 1 and said date. Since the days between dates does not include January 1, we need to include January 1 by adding 1.

For derivation purposes, let's calculate the days between January 1, 1 (AD) and target month and date of year 1. Add 1 to include January 1. I arbitrary chose 1 as the year for ease of calculation and the "year 1" would have been a 365 day year.

Day Number from January 1 to February 28 (29 for leap years)

Let M = month number (1 for January, 2 for February), D = date number

Day Number: (N_(M,D) - N_(1,1)) + 1

Since M≤2, T=1-1=0 and X=M+12

(IP(365*0) + IP(0/4) - IP(0/100) + IP(0/400) + D + IP((153(M+12)+8)/5)
- (IP(365*0) + IP(0/4) - IP(0/100) + IP(0/400) + 1 + IP((153(1+12)+8)/5)) + 1

= (D + IP((153M+1836+8)/5) - (1 + IP(1997/5))) + 1

= (D + IP(30.6M + 368.8) - (1 + 399)) + 1

= D + IP(30.6M + 368.8) - 399

Day Number for March 1 to December 31

Since M>2, T=Y and X=M. Then, again assuming Y=1:

(IP(365*1) + IP(1/4) - IP(1/100) + IP(1/400) + D + IP((153M+8)/5))
- (IP(365*0) + IP(0/4) - IP(0/100) + IP(0/400) + 1 + IP((153(1+12)+8)/5)) + 1

= (365 + D + IP(30.6M + 1.6)) - (1 + IP(1997/5)) + 1

= (365 + D + IP(30.6M + 1.6) - 399

= D + IP(30.6M + 1.6) - 34

For leap years, add 1.


Summary:

Day Number, 365 Day Year, M≤2:
D + IP(30.6M + 1.6 + 367.2) - (34 + 365) = D + IP(30.6M + 368.8) - 399

M>2:
D + IP(30.6M + 1.6) - 34


HP Prime Program DAYNO

EXPORT DAYNO(M,D)
BEGIN
IF M≤2 THEN
RETURN IP(30.6*M+368.8)+D-399;
ELSE
RETURN IP(30.6*M+1.6)+D-34;
END;
END;


Here it is in RPN format. I broke up some of the larger numbers into an addition of two smaller ones and used flags to keep the program within one label.


HP 32SII

N01 LBL N
N02 CF 0 \\clear flag 0 (any user flag will do)
N03 INPUT M
N04 2
N05 x≥y?
N06 SF 0 \\set flag 0
N07 R-down \\bring M to the X stack
N08 30.6
N09 ×
N10 1.6
N11 +
N12 FS? 0
N13 367.2
N14 FS? 0
N15 +
N16 IP
N17 INPUT D
N18 +
N19 34
N20 -
N21 FS? 0
N22 365
N23 FS? 0
N24 -
N25 CF 0
N26 RTN


Eddie


This blog is property of Edward Shore. 2014

No comments:

Post a Comment

HP Prime: Area by Quadratic Splines

HP Prime:  Area by Quadratic Splines Introduction The program QUADSUM calculates the area under the curve described by the set o...