Contributed by Sam Cox:
Given a positive integer numerator and positive integer denominator, the DIVMOD subroutine returns the integer quotient in variable [Div] and the integer remainder in variable [Mod]. The calling syntax is:
SetVar "[Args]" "Numerator,Denominator"
Gosub "DivMod"
Example:
SetVar "[Num]" "2001"
SetVar "[Denom]" "4"
SetVar "[Args]" "[Num],[Denom]" ... or just SetVar "[Args]" "2001,4"
Gosub "DivMod"
AlertBox "DivMod Example" "[Num] Div [Denom] = [Div]|[Num] Mod [Denom] = [Mod]"
Here is the DIVMOD subroutine:
CODE
IVMOD
StrParse "[Args]" "," "[Arg]" "[Argc]"
Math "[Arg1]/[Arg2]" "9" "[DivModTemp]"
Math "[DivModTemp]-0.499999999999999" "0" "[Div]"
Math "[Arg2]*[Div]" "0" "[DivModTemp]"
Math "[Arg1]-[DivModTemp]" "0" "[Mod]"
Return
Let's use the DIVMOD subroutine to write an ISLEAP subroutine (below) to determine whether a four-digit year such as 2001 is a leap year. The answer ("True" or "False") is returned in variable [isLeap]. The calling syntax is:
SetVar "[Args]" "FourDigitYear"
Gosub "isLeap"
Example:
SetVar "[SomeYear]" "1946"
SetVar "[Args]" "[SomeYear]" ... or just SetVar "[Args]" "1946"
Gosub "isLeap"
AlertBox "isLeap Example" "Is [SomeYear] a leap year? --- [isleap]"
Recall that in our modern western calendar, a four-digit year is a leap year if it is evenly divisible by 4 unless it also evenly divisible by 100 (turn of the century). But every fourth turn of the century is a leap year, so year 2000 is a leap year but years 1900 and 2100 are not.
Here is the ISLEAP subroutine.
CODE
:ISLEAP
StrParse "[Args]" "," "[Arg]" "[Argc]"
SetVar "[isLeapYYYY]" "[Arg1]"
SetVar "[Args]" "[isLeapYYYY],100"
Gosub "DivMod"
If "[Mod]" "=" "0"
SetVar "[Args]" "[isLeapYYYY],400"
Gosub "DivMod"
Else
SetVar "[Args]" "[isLeapYYYY],4"
Gosub "DivMod"
Endif
If "[Mod]" "=" "0"
SetVar "[isLeap]" "True"
Else
SetVar "[isLeap]" "False"
Endif
Return