tags:

views:

182

answers:

7

if counter % 2 == 1 I am trying to decode this line -its rails project and I am trying to figure out what the % does in this if statement.

+8  A: 

% is the modulo operator. The result of counter % 2 is the remainder of counter / 2.

n % 2 is often a good way of determining if a number n is even or odd. If n % 2 == 0, the number is even (because no remainder means that the number is evenly divisible by 2); if n % 2 == 1, the number is odd.

Daniel Vandersluis
You *certain* about that last claim there? That's true in Ruby but not in other languages!
Eric Lippert
@Eric what's not true? I didn't say it's the only or best way of determining if a number is even or odd...
Daniel Vandersluis
What I meant was that the test n%2==1 *does not actually determine if a number is odd* in many languages. In C# for instance it determines if a number is *both positive and odd*, which is different.
Eric Lippert
@Eric well I can't speak about languages I haven't used, and this question is specifically about Ruby...
Daniel Vandersluis
Why would % in C# differentiate between positive and negative in mod 2? Since -a == a (mod 2) (this is trivial - take -a == -a (mod 2), and add 0 == 2a (mod 2)), I see no reason for the difference. Obviously, we don't have this restriction in other moduli. (1 == -2 (mod 3)).
mathepic
+1  A: 

Its the modulo operator.

http://en.wikipedia.org/wiki/Modulo_operation

Yann Ramin
+3  A: 

That's the modulo operator. It gives the remainder when counter is divided by 2.

For example:
3 % 2 == 1  
2 % 2 == 0
GWW
+1  A: 

It is the modulo operator, which is a fancy way of saying it's the remainder operator.

So if you divided a number by two, and the integer remainder of that number is one, then you know the number was odd. Your example checks for odd numbers.

Often this is done to highlight odd number rows with a different background color, making it easier to read large lists of data.

Edwin Buck
+1  A: 

To give a few ways to say it:

  • Modulo operator
  • Remainder operator
  • Modular residue

Strictly speaking, if a % b = c, c is the unique constant such that

a == c (mod b) and 0 <= c < b

Where x == y (mod m) iff x - y = km for some constant k.

This is equivalent to the remainder. By some well known theorem, we have that a = bk + c for some constant k, where c is the remainder, which gives us a - c = bk, which obviously implies a == c (mod b).

(Is there a way to use LaTeX on Stackoverflow?)

mathepic
A: 

This is a very basic question. % is the modulo opereator if counter % 2 == 1 results to true for every odd number and to false for every even number.

If you're learning ruby you should learn how to use irb, there you can try things out and perhaps answer the question yourself.

try to enter

100.times{|i| puts "#{i} % 2 == 1 #=> #{i % 2 == 1}"}

into your irb irb console and see the output, than it should be clear what % does.

And you really should take a look at the rails api documentation (1.9, 1.8.7, 1.8.7), there you would have found the answer two your question % (Fixnum) with a further link to a detailed description of divmod (Numeric):

Returns an array containing the quotient and modulus obtained by dividing num by aNumeric. > If q, r = x.divmod(y), then

q = floor(float(x)/float(y))
x = q*y + r

The quotient is rounded toward -infinity, as shown in the following table:

 a    |  b  |  a.divmod(b)  |   a/b   | a.modulo(b) | a.remainder(b)
------+-----+---------------+---------+-------------+---------------
 13   |  4  |   3,    1     |   3     |    1        |     1
------+-----+---------------+---------+-------------+---------------
 13   | -4  |  -4,   -3     |  -3     |   -3        |     1
------+-----+---------------+---------+-------------+---------------
-13   |  4  |  -4,    3     |  -4     |    3        |    -1
------+-----+---------------+---------+-------------+---------------
-13   | -4  |   3,   -1     |   3     |   -1        |    -1
------+-----+---------------+---------+-------------+---------------
 11.5 |  4  |   2,    3.5   |   2.875 |    3.5      |     3.5
------+-----+---------------+---------+-------------+---------------
 11.5 | -4  |  -3,   -0.5   |  -2.875 |   -0.5      |     3.5
------+-----+---------------+---------+-------------+---------------
-11.5 |  4  |  -3,    0.5   |  -2.875 |    0.5      |    -3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | -4  |   2    -3.5   |   2.875 |   -3.5      |    -3.5

Examples

 11.divmod(3)         #=> [3, 2]
 11.divmod(-3)        #=> [-4, -1]
 11.divmod(3.5)       #=> [3, 0.5]
 (-11).divmod(3.5)    #=> [-4, 3.0]
 (11.5).divmod(3.5)   #=> [3, 1.0]
jigfox
A: 

Regardless of how it works, the modulo operator is probably not the best code for the purpose (even though we are not given much context). As Jörg mentioned in a comment, the expression if counter.odd? is probably the intent, and is more readable.

If this is view code and used to determine (for example) alternating row colors, then you may be able to do without the counter altogether by using the built-in Rails helper cycle(). For example, you can use cycle('odd','even') as a class name for a table row, eliminating the counter and surrounding if/then logic.

Another thought: if this is within an each block, you may be able to use each_with_index and eliminate the extraneous counter variable.

My refactoring $0.02.

Mark Thomas