Re: Numeric Date Validation
Hold on a sec. First of all, your expression (Method 1)
[color=blue]
> ( !( ayear % 4 ) && ( ayear % 100 )) || !( ayear % 400 )[/color]
is saying a year is a leap year iff
((it's divisible by 4) AND (it's not divisible by 100)) OR
(it's divisible by 400).
But that's the same as (Method 2):
a year is a leap year iff
(it's divisible by 4) AND ((it's not divisible by 100) OR
(it's divisible by 400))
In other words, from a logical point of view, you don't
NEED the grouping parentheses. But they DO make
a difference (theoretically, anyway. YOU can do the testing).
Let's just check out how many times each condition gets
executed.
Method 1:
Year not divisible by 4: first and last expressions
Year divisible by 4/ not by 100: first two
Year divisible by 400: all three
Method 2:
Year not divisible by 4: first one only
Year divisible by 4/ not by 100: first two
Year divisible by 400: all three
So in 75% of all cases, Method 2 will have only one test
to two for Method 1.
But wait, there's more. Since you've come to the right
newsgroup, tonight only, we'll throw in a special simplification,
to get rid of not one, but two of those pesky modulo operators.
Who needs extra divisions, after all? So here you have it -
Method 2, simplified:
isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))
From Bit Tiddle City,
Csaba Gabor
PS. If you leave out the != 0 part in the second condition, your result
in the case of leap years not divisible by 400 will be numeric (nonzero -
the result of ayear % 25) instead of boolean true, which would be OK
for most of my purposes.
Hold on a sec. First of all, your expression (Method 1)
[color=blue]
> ( !( ayear % 4 ) && ( ayear % 100 )) || !( ayear % 400 )[/color]
is saying a year is a leap year iff
((it's divisible by 4) AND (it's not divisible by 100)) OR
(it's divisible by 400).
But that's the same as (Method 2):
a year is a leap year iff
(it's divisible by 4) AND ((it's not divisible by 100) OR
(it's divisible by 400))
In other words, from a logical point of view, you don't
NEED the grouping parentheses. But they DO make
a difference (theoretically, anyway. YOU can do the testing).
Let's just check out how many times each condition gets
executed.
Method 1:
Year not divisible by 4: first and last expressions
Year divisible by 4/ not by 100: first two
Year divisible by 400: all three
Method 2:
Year not divisible by 4: first one only
Year divisible by 4/ not by 100: first two
Year divisible by 400: all three
So in 75% of all cases, Method 2 will have only one test
to two for Method 1.
But wait, there's more. Since you've come to the right
newsgroup, tonight only, we'll throw in a special simplification,
to get rid of not one, but two of those pesky modulo operators.
Who needs extra divisions, after all? So here you have it -
Method 2, simplified:
isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))
From Bit Tiddle City,
Csaba Gabor
PS. If you leave out the != 0 part in the second condition, your result
in the case of leap years not divisible by 400 will be numeric (nonzero -
the result of ayear % 25) instead of boolean true, which would be OK
for most of my purposes.
Comment