• Post Reply Bookmark Topic Watch Topic
  • New Topic

% symbol, question?  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the symbol "%" means remainder. But what is actually going on?

EX

int num1 = 0;

num1 = 10 % 6;

System.out.println(num1);

Prints 4

How? whats going on?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10 |_6__
4 1

It�s what the % symbol does.


[ August 30, 2006: Message edited by: Cristiano Mariano ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divide it out as an integer. You would get 1 remainder 4.
[ August 30, 2006: Message edited by: Rusty Shackleford ]
 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks

 
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

% is Modulo Operator. It will give remainder value of int operations.

Remember % is not like C or C++. In java it operates on double and float values too.

6 X 1 =6 and 10 -6 = 4 ( remainder ) for int.

Check with double ...
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is probably better to call the % the remainder operator rather than modulo because it might be confused with the modulus of a number, ie its value without a + or - sign.

I may be mistaken, but I think that the division part of most computer chips produces two results, a quotient and a remainder. So the / and % operators both call the same part of the chip and the same process, they simply get their hands on different outputs from the same thing.

It is worthwhile printing out 7 / 3, -7 / 3, 7 / -3 and -7 / -3.
Then print out 7 % 3, -7 % 3, 7 % -3, and -7 % -3. Then remember which combinations print out positive and negative results.

If you look up the java.util.Formatter class in the API, you will find a different use for the % symbol.
 
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember these:
With %, the result of m % n is always less than the divisor n. To calculate m % n with integers m and n:

m % n == m - (m / n) * n

Modulus Operator Shortcuts:

Ignore the signs of the operands, calculate the remainder, and negate the remainder if the dividend (first number) is negative

Thus, with modulus the result is the same sign of the dividend.

If the dividend is smaller than the divisor, the result will be the dividend

JC
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
It is probably better to call the % the remainder operator rather than modulo because it might be confused with the modulus of a number, ie its value without a + or - sign.


You are right about modulus, but modulo also has a specific meaning in mathematics, which is not too far from the meaning in computer languages: http://en.wikipedia.org/wiki/Modulo
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!