Javascript test %4

C

Cogito

I'm trying to understand some Javascript code from a certain site.
Could you please tell me what does this test mean?
if ( j%4 == 0 )
What does %4 test for and is there a web site that explains it and,
presumably, also %1, %2 etc.
 
R

Randy Webb

Cogito said the following on 3/6/2007 11:52 PM:
I'm trying to understand some Javascript code from a certain site.
Could you please tell me what does this test mean?
if ( j%4 == 0 )
What does %4 test for and is there a web site that explains it and,
presumably, also %1, %2 etc.

The statement above means if the remainder of dividing j by 4 is 0.
You can change the 0 to anything smaller than j.
 
E

Evertjan.

Randy Webb wrote on 07 mrt 2007 in comp.lang.javascript:
Cogito said the following on 3/6/2007 11:52 PM:

The statement above means if the remainder of dividing j by 4 is 0.
You can change the 0 to anything smaller than j.

There is no such limitation.

;-)

But to be useful:
Anything? Also any fraction or any negative integer?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Randy Webb wrote on 07 mrt 2007 in comp.lang.javascript:


There is no such limitation.

;-)

But to be useful:
Anything? Also any fraction or any negative integer?

Yes, yes.

ISTM that, unless the code is an obfuscation of false, the right hand
side must be, or have a chance of being, smaller in absolute value than
the divisor is or may be. And, generally, all three values need to be
such as can be represented exactly by an IEEE Double, including NaN, if
the statement is to be reliably usable.

The OP could easily have answered his question by searching the PDF of
the Standards (FAQ cites one of them, IIRC) for "%".

The sections of the standards dealing with the % operator need
rewriting; at present, they use "floating-point" to mean "non-integer".
All Numbers are floating point currently.

It's a good idea to read the newsgroup and its FAQ. See below.
 
R

Randy Webb

Evertjan. said the following on 3/7/2007 5:06 AM:
Randy Webb wrote on 07 mrt 2007 in comp.lang.javascript:


There is no such limitation.

;-)

Tis true, but the results are pretty useles if k is smaller than j in j%k.
But to be useful:
Anything? Also any fraction or any negative integer?

Yes. Or at least testing doesn't throw up on fractions or negative
integers.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Evertjan. said the following on 3/7/2007 5:06 AM:

Tis true, but the results are pretty useles if k is smaller than j in j%k.

That is certainly not true, and neither is the converse. But if j is
*always* smaller than k, the operation is pointless.
Yes. Or at least testing doesn't throw up on fractions or negative
integers.

Testing has its uses; but the standards are sufficiently clear. For
example, -6.875%1.25 has a meaning, and the operation gives it.
 
R

Randy Webb

Dr J R Stockton said the following on 3/7/2007 11:48 AM:
Testing has its uses; but the standards are sufficiently clear. For
example, -6.875%1.25 has a meaning, and the operation gives it.

Yeah, the "standards" are sufficiently useless also. We all know how
abundantly clear the "standards" are with regards to toFixed() now don't we?

Keep the "standards", give me testing.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top