POST EDITED: I had the wrong code pasted into the post. It has been replaced.
Joel,
As someone who works in cosmology, astronomy and physics, the formula for calculating leap years is a very simple one.
However, looking at your code, why are you using FLOOR instead of MOD? In SmartBASIC, the MOD function is "%". X=405%360 returns X=45, 45 the remainder of 405/360.
MOD function in SmartBASIC's manual has half a sentence, if that, and I missed it many times, until I asked in a post if SB had a MOD function.
I would like to see a small section dedicated to the MOD, and Ton, you can use my code below to use as an example of one way to use it. I can write up more on it with other examples if your wish. Let me know.
The problem with using FLOOR or CEIL or any of the other math functions is you may get rounding errors, and could flag a year as a leap year or one that is a leap year as not one, and your calendar is off.
Here is a simple program using the SB MOD function that can be easily changed to a function, and returning switched values, tell you if a year is a Leap Year or not. No need to provide it with a table of valid Century years as the logic test does this for you:
Code: Select all
'*** Input Year in box at top of the screen
INPUT Y
'*** Perform MOD functions on year divided by 4, 100, and 400
L=Y%4
C100=Y%100
C400=Y%400
'*** Logic test to see if year is a Century, Normal, or not a Leap Year
IF L=0 AND (C100=0 AND C400=0) THEN
PRINT "Year "&Y&" is a Century Leap Year"
GOTO endPROG
ELSE
PRINT "Year "&Y&" is not a Century Leap Year"
IF L=0 AND (C100<>0 AND C400<>0) THEN
PRINT "Year "&Y&" is a normal Leap Year"
ELSE
PRINT "Year "&Y&" is not a Leap Year"
ENDIF
ENDIF
endPROG:
STOP
This takes care of your every four years, your every 100 years (century years), and also determines if a century is really a leap year (by dividing by 100 and 400).
George
Joel wrote: ↑Mon Oct 02, 2017 8:18 pm
Hi there,
For date calculations I needed a program to calculate the number of days since 1/1/1600. This program can also be used to calculate the difference between two dates, for example.
Code: Select all
INPUT year, month,day
PRINT days_1600(year,month,day) 'calculates the number of days since 1600/1/1
'c'
DEF days_1600(year,month,day)
'set leapyear (is this year a leapyear? and count leap years since 1600
leaps=leaps2(year)
IF year=1600 THEN
leapyear=1
ELSE
IF leaps2(year-1)=leaps THEN
leapyear=0
ELSE
leapyear=1
ENDIF
ENDIF
dyear=year-1600
IF NOT num_in_range(month,1,12) THEN RETURN -1'valid input?
IF NOT num_in_range(day,1,31) THEN RETURN -1 'upper limit will be checked later
RESTORE
result=0
FOR days_in_month=1 TO month-1
READ days
result=result+days 'sum up all days of prev month
NEXT days_in_month
result=result+day 'add the input day
READ days 'for validity purpose
IF month=2 AND leapyear THEN days+=1 'days will not longer be used so this is for next line
IF days<day THEN RETURN-1 'invalid date
result=result+dyear*365+leaps 'add days of prev years and number of leap years
IF month<=2 AND leapyear THEN result-=1 'in jan or feb ignore the this year leap status
RETURN result
DATA 31,28,31,30,31,30,31,31,30,31,30,31
END DEF
DEF num_in_range(num, num1,num2)
IF num<num1 OR num>num2 THEN RETURN 0 ELSE RETURN 1
END DEF
DEF leaps2(year)
dyear=year-1600
each4=FLOOR(dyear/4)
each100=FLOOR(dyear/100)
each400=FLOOR(dyear/400)
result=each4-each100+each400+1'1600 is a leap year itself
RETURN result
END DEF