Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

% symbol, question?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks

 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79528
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic