View previous topic :: View next topic
|
Author |
Message |
arvind.m
Active User
Joined: 28 Aug 2008 Posts: 205 Location: Hyderabad
|
|
|
|
Hi All,
The below logic is to check for leap year. but there is some problem in the below problem. even for 2007, the logic is going to "NOT ON SIZE ERROR" which is wrong. I'm using Enterprise COBOL. is the version of cobol has to do anything with the above mentioned problem.
Code: |
05 WS-TEST-YEAR PIC 9(04) VALUE ZEROS.
***
***
IF WS-MED-DATE-MM = 02
COMPUTE WS-TEST-YEAR = WS-MED-DATE-YYYY
/ 4
ON SIZE ERROR
MOVE 28 TO WS-MED-DATE-DD
NOT ON SIZE ERROR
MOVE 29 TO WS-MED-DATE-DD
END-COMPUTE
|
|
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Code is behaving the way it was told to, from the Manuals:
Quote: |
ON SIZE ERROR statement1
Statement1 is replaced by the statement(s) that are to be executed whenever a SIZE ERROR condition occurs (i.e., whenever the integer portion of the result of the arithmetic operation exceeds the largest value that can be contained in the integer portion of the recieving field). |
In your case WS-MED-DATE-YYYY = 2007 so WS-MED-DATE-YYYY / 4 = 501.75, integer part is less than 9999 (pic 9(4)), so there is NO ON SIZE ERROR. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Said slightly differently, you have chosen an incorrect way to check for leap year.
With only a simple divide, there are conditions that are not met when determining leap year.
Suggest you check out the "rules" for determining what is a leap year. Hint - century years and millenium years need special consideration. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Quote: |
the logic is going to "NOT ON SIZE ERROR" which is wrong. |
This is not the correct logic to find out a leap-year, so if the control is going to "NOT ON SIZE ERROR" that's not wrong.
Just for fun, here is a C Code :
Code: |
#include <stdio.h>
#include <conio.h>
void main()
{
int year,leap;
clrscr();
printf("enter the year:- ");
scanf("%d",&year);
if ((year%100)==0)
leap=(year/400)*400;
else
leap=(year/4)*4;
if (leap==year)
printf("
%d is a leap year.",year);
else
printf("
%d is not a leap year.",year);
getch();
} |
|
|
Back to top |
|
|
arvind.m
Active User
Joined: 28 Aug 2008 Posts: 205 Location: Hyderabad
|
|
|
|
Hi Anuj/Dick,
Thank you for you time and help. Actually the code was developed by my client. we have changed the logic after searching in the internet. The issue is resolved but wanted to know the reason.
Quote: |
Leap Year Calculation
In general terms the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
|
|
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
A very informal definition is, in the Gregorian calendar, current standard calendar in most of the world, most years that are divisible by 4 are leap years. But in leap year, the month of February has 29 days instead of 28. One extra day is added to compensate for the fact that a period of 365 days is shorter than a solar year by almost 6 hours.
When you dig into a bit more, every year has 365.242374 days which are rounded to 365.2425. Decimal part .2400 is rounded to one day (approx.) in every four years and Decimal part .0025 is rounded to one day in every 400 years. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
Back to top |
|
|
|