posted 11 years ago

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 ...

% 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 ...

posted 11 years ago

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.

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.

posted 11 years ago

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

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 and Instructor, my book

posted 11 years ago

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

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

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

Consider Paul's rocket mass heater. |