Identify whether value is integer in IF statement [message #91148] |
Tue, 09 June 2015 06:49  |
jecca.baker
Messages: 12 Registered: October 2014
|
Junior Member |
|
|
I'm trying to automate identification of leap years.
I was thinking something like:
FOR YR=0, 10-1 DO BEGIN
YEAR=2001+YR
IF (YEAR/4) EQ ?an integer? THEN BEGIN
*
*
*
ENDIF
ENDFOR
How do I identify whether YEAR/4 is an integer (ie a leap year)?
Thanks.
|
|
|
|
Re: Identify whether value is integer in IF statement [message #91150 is a reply to message #91148] |
Tue, 09 June 2015 07:05   |
Helder Marchetto
Messages: 520 Registered: November 2011
|
Senior Member |
|
|
On Tuesday, June 9, 2015 at 3:49:27 PM UTC+2, Jess wrote:
> I'm trying to automate identification of leap years.
>
> I was thinking something like:
>
> FOR YR=0, 10-1 DO BEGIN
> YEAR=2001+YR
> IF (YEAR/4) EQ ?an integer? THEN BEGIN
> *
> *
> *
> ENDIF
> ENDFOR
>
> How do I identify whether YEAR/4 is an integer (ie a leap year)?
>
> Thanks.
One way would be to use the isa() function with the /integer keyword. But...
when you calculate year/4 you will always get an integer because an integer divided by an integer is still an integer:
IDL> for yr=0,9 do help, (2001+yr)/4
<Expression> INT = 500
<Expression> INT = 500
<Expression> INT = 500
<Expression> INT = 501
<Expression> INT = 501
<Expression> INT = 501
<Expression> INT = 501
<Expression> INT = 502
<Expression> INT = 502
<Expression> INT = 502
What you really want is probably is to check if 4 is a divisor of year. To check for this condition, you can use the "mod" operator (see http://www.exelisvis.com/docs/Mathematical_Operators.html).
Then you can check with
if (YEAR mod 4) eq 0 then ...
Cheers,
Helder
PS: Ups, too late, Heinz was faster...
|
|
|
|