I'm useless at maths, could somebody help?
I need to check if a year is a leap year, within a BASIC program. It's actually Liberty BASIC v4.01 but the syntax is more or less standard BASIC. In fact a coded example is probably not neccessary, I just need to know mathematically how to do it.
Thanks for any help with this. My brain hurts after a lot of coding today & my mind has gone completely blank.
There's some VB code at http://www.freevbcode.com/ShowCode.asp?ID=391
but I'm not sure what it's on about, can anyone tell me what it is doing (just the guts that do the actual determination of leap year/not leap year?)
Eh up. here's a QBASIC example which is closer to Liberty BASIC's syntax: (Liberty is basically QBASIC with a GUI)
- Code: Select all
C. LEAP YEAR:
To determine if a year is leap year, is actually a simple matter.
The rule is as folows:
If the year is evenly divisible by 4
and not divisible 100,
or if the year is evenly divisible by 400,
then it's a leap year.
Sample usage of ISLEAPYEAR function below:
Assume we looked up the maximum number of days per month in a table.
Now, if the month is February and the year is a leap year,
we want to add 1 to the maximum days per month.
IF MM=2 AND ISLEAPYEAR(YYYY) THEN MAXDAYS=MAXDAYS+1
The following is the function:
Note: It returns -1 when true (leap year), or 0 if false.
FUNCTION IsLeapYear (Z) STATIC
IsLeapYear = (Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)
I guess my question boils down to, what does:
- Code: Select all
(Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)
...mean in english ? (can someone explain the math terms..)
..guess this further boils down to, what is the MOD term?
There doesn't seem to be a MOD operator in Liberty BASIC
These are the math functions I have available in Liberty:
Arithmetic operators are as follows:
print 2 + 3 'addition
print 6 - 2 'subtraction
print 4 * 7 'multiplication
print 9 / 3 'division
print 2 ^ 3 'power - (two to the third power)
print (4+2)*6^2 'multiple expressions are evaluated according to the following rules of order:
Expressions are evaluated in this order:
( ) expressions within parentheses are evaluated first
^ exponents are evaluated next
* / multiplication and division are evaluated next
+ - addition and subtraction are evaluated last
Found some info here http://www.asp101.com/articles/steven/mod/default.asp
about 'MOD' (??) but could still do with some help putting it all together, if someone would know how to do it
I wonder if I could use Liberty's INT function:
This function removes the fractional part of "n" (a number), leaving only the whole number part behind. The fractional part is the part of the number after the decimal point.
input "Enter an integer number>"; i
if i<>int(i) then bell: print i; " isn't an integer! Re-enter.": goto [retry]
i.e. to check a year is 'evenly divisible', could I do the calculation, then compare the result with the "INT'ed" result, and if they're the same, it has
been an 'even' division.. oh I dunno, am I making it too complicated.
Any math experts your opinion appreciated