JS Operator precedence?

N

newbie

I have a script given to me by a co-worker to convert into VB; I can get
the same results but I cannot fully understand the logic here... can
someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 + Math.floor(y
/ 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

I am happy with the major part of the maths, with the mod4, the "then"
and "else" expressions so its just the "if" part I need to "explained"...

(d += m < 3 ? THEN : ELSE, MATH_EXPRESSION) mod4;

Does the "if" part say:

d = d + m;
if(d < 3, then, else)

How does the operator precedence work here? Isn't greater-than,
less-than a higher precedence than += ? In which case:

d = d + (m < 3);
if(d, then, else)

???? or am I missing something ????
 
J

Jeremy

newbie said:
I have a script given to me by a co-worker to convert into VB; I can get
the same results but I cannot fully understand the logic here... can
someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 + Math.floor(y
/ 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

I am happy with the major part of the maths, with the mod4, the "then"
and "else" expressions so its just the "if" part I need to "explained"...

(d += m < 3 ? THEN : ELSE, MATH_EXPRESSION) mod4;

Does the "if" part say:

d = d + m;
if(d < 3, then, else)

How does the operator precedence work here? Isn't greater-than,
less-than a higher precedence than += ? In which case:

d = d + (m < 3);
if(d, then, else)

???? or am I missing something ????


You really have several *separate* expressions here, which I think is
what's confusing you.

"(expression1) ? (expression2) : (expression3)" is a language construct,
so it has the highest precedence here in terms of "operators" (which
it's not, really).

First, m < 3 gets evaluated. If it's true, y-- gets evaluated, and
assigned to a temporary register (call it "temp"). If not, that other
giant expression gets evaluated and assigned to temp. Then, d is
incremented by the value in temp.

Language constructs come first - other than that, operator precedence is
pretty much what you would expect. Just remember that separate
expressions get evaluated first.

Also, I hope you never use "y" again after this line, because in one
branch you affect it and in the other you don't.

Jeremy
 
D

Dr J R Stockton

In comp.lang.javascript message <472a70b7$0$17189$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Fri, 2 Nov 2007 09:35:05, newbie
I have a script given to me by a co-worker to convert into VB; I can
get the same results but I cannot fully understand the logic here...
can someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 +
Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;


That looks very much like something near the end of <URL:http://www.mer
lyn.demon.co.uk/zeller-c.htm> - "rh"'s version of Mike Keith's Day-of-
Week Congruence. It would be faster if Math.floor(X) were replaced by
((X)|0). But the mod-4 is unexpected; mod-7 might well work better.

The correct translation to VBS is something like :
WeekDay(DateSerial(Y, M, D))
though the output value will need adjustment for exact agreement. And
in Javascript :
new Date(Y, M-1, D).getDay()
(but use UTC methods for speed).
 
N

newbie

Jeremy said:
You really have several *separate* expressions here, which I think is
what's confusing you.

"(expression1) ? (expression2) : (expression3)" is a language construct,
so it has the highest precedence here in terms of "operators" (which
it's not, really).

First, m < 3 gets evaluated. If it's true, y-- gets evaluated, and
assigned to a temporary register (call it "temp"). If not, that other
giant expression gets evaluated and assigned to temp. Then, d is
incremented by the value in temp.

Language constructs come first - other than that, operator precedence is
pretty much what you would expect. Just remember that separate
expressions get evaluated first.

Also, I hope you never use "y" again after this line, because in one
branch you affect it and in the other you don't.

Jeremy

All,

Thanks for the responses...

I have re-figured the code to be "simpler" in my view to see what is
happening:

if (m<3){
d = d + (y--) //affects the value of y
}else{
d = d + (y-2) //does not change y (???)
}

var x =
(Math.floor(23*m/9)+d+4+Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400))
% 4;

return x;


I don't know why one part of the function changes the value of y and the
other doesn't. As I said earlier, this is for a co-worker and she's
happy with the result. We don't need to go into how the thing works,
she's just happy she has it working, and I learned a little more about
'operator' precedence in JS.

Thanks again.
 
N

newbie

Dr said:
In comp.lang.javascript message <472a70b7$0$17189$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Fri, 2 Nov 2007 09:35:05, newbie



That looks very much like something near the end of <URL:http://www.mer
lyn.demon.co.uk/zeller-c.htm> - "rh"'s version of Mike Keith's Day-of-
Week Congruence. It would be faster if Math.floor(X) were replaced by
((X)|0). But the mod-4 is unexpected; mod-7 might well work better.

The correct translation to VBS is something like :
WeekDay(DateSerial(Y, M, D))
though the output value will need adjustment for exact agreement. And
in Javascript :
new Date(Y, M-1, D).getDay()
(but use UTC methods for speed).


Thanks for the response Dr Stockton. Yes the use of the pipe is faster,
so I am now using:

if (m<3){
d = d + (y--) //affects the value of y
}else{
d = d + (y-2) //does not change y (???)
}

var x = ( ((23*m/9)|0) + d + 4 + ((y/4)|0) - ((y/100)|0) + ((y/400)|0) )
% 4;

return x;


The mod4 is part of some cycle thing for scheduling. Teams work in
cycles with so-many teams working for so many days, she needs to find
the start of each four day cycle period for the team rotations, (or
something like that). I don't work shifts!

I dont know where this code comes from, I suggested using the standard
Julian Day mod4, but for some reason that wasnt sufficient? She's happy
now as she has something that works.

Thanks for your time.
 
D

Dr J R Stockton

In comp.lang.javascript message <472bcf55$0$17167$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Sat, 3 Nov 2007 10:31:05, newbie
Dr J R Stockton wrote:
The mod4 is part of some cycle thing for scheduling. Teams work in
cycles with so-many teams working for so many days, she needs to find
the start of each four day cycle period for the team rotations, (or
something like that). I don't work shifts!

I dont know where this code comes from,

I indicated it as quoted above. Wikipedia search for "Mike Keith" leads
easily to <http://users.aol.com/s6sj7gt/mikecal.htm>, which my page also
links to. My page notes that LF & MK have since shortened the
expression.
I suggested using the standard Julian Day mod4,

The standard Julian Date changes at Noon UTC, which is unlikely to be
wanted.
but for some reason that wasnt sufficient? She's happy now as she has
something that works.

The formula was developed for day-of-week, with % 7 at the end. The
month term ignores the first 28 days of the months that have passed,
since that is a multiple of 7. It also happens to be a multiple of 4,
and the modified formula works for you. But ISTM that if there is a
change to, say, 3 or 5 shifts, then greater change will be needed.


The following method is faster in IE6, and easier to understand and to
modify.

Date.UTC(y, m-1, d) / 864e5 % 4

VBS? DateSerial(y, m, d) % 4

In each case one should be able to adjust the phase by adding a constant
to d.

=======

Firefox 2.0.0.9 retains the Date.UTC(y, m, 0) error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top