Hi
Im using codewarrior 6.3
and a MC9s08QE32
When I do the following
cX = cM * 2;
cY = cX + (((cM+1)*3)/5);
cX = cY + ((sTime.cYear-i)/4);
cY = cX - ((sTime.cYear-i)/100);
cX = cY + ((sTime.cYear-i)/400);
cY = (cX + (sTime.cYear-i));
cX = cY + 2;
cX %= 7;
sTime.cDayofWeek = (char)cX;
where cY,cX and stime.cYear are all interger (unsigned for cyear)
Im using debugger with a openbdm
those two line doesnt change variable content
cY = (cX + (sTime.cYear-i));
cX = cY + 2;
but cX %=7 does work (but the final result is wrong due to 2 addition failing to add
can someone point me what im doing wrong there?
Solved! Go to Solution.
answered myself
declared long int and it work now
int is not 16 bit???
BTW this is zeller's congruence
cM = sTime.cMonth;
//Day of week
if(cM == 1)
{
cM = 13;
i++;
}
else if(sTime.cMonth == 2)
{
cM = 14;
i++;
}
cX = sTime.cDay;
cX += (long int)(((cM+1)*26)/10);
cX += sTime.cYear-i;
cX += (long int)((sTime.cYear-i)/4);
cX += (long int)(((sTime.cYear-i)/100)*6);
cX += (long int)((sTime.cYear-i)/400);
cX %= 7;
sTime.cDayofWeek = (char)cX;
it could be a little more compressed but it work and easy to debug
BigMac:
i is not much of a problem since the lowest value it can get is -1
I use i when we are month jan and feb they are counted as month 13 and 14 of the year before
like you said you can use cY to put it in
Im using one of the formula in wiki
the lowest one they are more easy to me since we use the actual year instead of K and J
http://upload.wikimedia.org/math/d/b/7/db7eff99d21c5d7a2949c5fe68e6b771.png
each "part" of the formula is set into interger to remove
Your formala seem good (and much compressed)
im just a little incertain about
cX = (cDay + (cM + 1)*26/10 + K + K/4 + J/4 - 2*J) % 7;
Im not sure how cw will do it, will it add each floating poit one after the other if so you might get an error at the end
the long int casting remain from when stime.cyear was still a regular int
I can remove them now
But the original problem was really
with the debugger when I was cheking
cY = (cX + (sTime.cYear-i));
cX = cY + 2;
cY didn t change at all...
both defined as int
but as long int it work so ill leave it like this
where cX at first was something low like 500... 16-bit is up to 65536 (/2 if signed)
Thanks
Hello,
For an espression such as
cX = (cDay + (cM + 1)*26/10 + K + K/4 + J/4 - 2*J) % 7;
since all variables are integer, the result of any intermediate calculation will also be an integer value (with any remainder simply truncated). Floating point values are only of consideration if one or more of the variables or constants is a floating point type.
Regards,
Mac
answered myself
declared long int and it work now
int is not 16 bit???
BTW this is zeller's congruence
cM = sTime.cMonth;
//Day of week
if(cM == 1)
{
cM = 13;
i++;
}
else if(sTime.cMonth == 2)
{
cM = 14;
i++;
}
cX = sTime.cDay;
cX += (long int)(((cM+1)*26)/10);
cX += sTime.cYear-i;
cX += (long int)((sTime.cYear-i)/4);
cX += (long int)(((sTime.cYear-i)/100)*6);
cX += (long int)((sTime.cYear-i)/400);
cX %= 7;
sTime.cDayofWeek = (char)cX;
it could be a little more compressed but it work and easy to debug
Hello,
I am not familiar with the algorithm you are using. The earlier code seems to differ somewhat from the later code, so my comments refer to the latter.
Firstly, an int is a signed 16-bit variable in CW. I can see no particular reason why the code should overflow. The only issue would be if sTime.cYear were unsigned and the subtraction of i caused a negative result.
With the following expression:
cX += (long int)(((sTime.cYear-i)/100)*6);
may be simplified to:
cX += (sTime.cYear-i)/100*6;
For integer calculation, this will produce a different result than the following expression:
cX += (sTime.cYear-i)*6/100;
The initial expression will retain the same value for a whole century, and then change by 6. Whereas the modified expression will increment each 16 or 17 years. It all depends on what the algorithm requires.
Regards,
Mac
Hello,
I decided to satisfy my curiosity about Zeller's congruence for the day of the week calculation. Actually I cannot see where some of your code fits the formula given within the Wiki reference (calendars.wikia.com/wiki/Zeller's_congruence). This includes the code previously mentioned by me.
Firstly, with the months of January and February considered to be the months 13 and 14 of the previous year, and assuming that the parameter i represents the "number of years ago", incrementing this negative parameter to provide the adjustment was, at least, confusing to me.
Applying the formula given a little more directly, I might arrive at the following code.
#define cYear sTime.cYear
#define cMonth sTime.cMonth
#define cDay sTime.cDay
#define cDOW sTime.cDayofWeek
int cM, cY, cX, J, K;
if (cMonth <= 2) {
cM = cMonth + 12;
cY = cYear - 1;
}
else {
cM = cMonth;
cY = cYear;
}
J = (cY - i) / 100; // Century
K = (cY - i) % 100; // Year of century (0-99)
cX = (cDay + (cM + 1)*26/10 + K + K/4 + J/4 - 2*J) % 7;
cDOW = (byte)cX; // 0 = Saturday -> 6 = Friday
Regards,
Mac