Stephen-Austin Murphy

Greenhorn

Posts: 10

posted 4 years ago

- 1

I do agree with Henry here,

How do you think a modulus operator function as?

Once you are able to answer this, I'm certain you would be able to answer the question that you have posted too.

Cheers.

How do you think a modulus operator function as?

Once you are able to answer this, I'm certain you would be able to answer the question that you have posted too.

Cheers.

Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.

My Blog on Learning Analytics

Matthew Brown

Bartender

Posts: 4568

9

Campbell Ritchie

Marshal

Posts: 56576

172

posted 4 years ago

- 1

It is actually called the remainder operator; even the name might help to explain its purpose.

When I did maths, which was a long time ago, we used modulus (|

The remainder operation is very closely related to the division operator; in fact on many computers the two use the same part of the chip and the circuit produces the quotient on one output and the remainder on another output. It is classified by Java as a multiplicative operation and you can read about it in the Java Language Specification, but it may be difficult to understand. You might do well to read only those parts relating to integer arithmetic.

When I did maths, which was a long time ago, we used modulus (|

*x*|) to mean the absolute value of a number, so |-1| = 1. In most non‑English‑speaking countries it is called modulo and means remainder. A lot of people use modulo to mean remainder in Britain, too.The remainder operation is very closely related to the division operator; in fact on many computers the two use the same part of the chip and the circuit produces the quotient on one output and the remainder on another output. It is classified by Java as a multiplicative operation and you can read about it in the Java Language Specification, but it may be difficult to understand. You might do well to read only those parts relating to integer arithmetic.

Stephen-Austin Murphy

Greenhorn

Posts: 10

posted 4 years ago

Thanks everyone who replied.

<red-face>OMG, I can't believe I had forgotten some basic math.</red-face>

My understanding of the % operator is that it returns the remainder after division.

Here was my reasoning that led to my post:

---------------------------------------------------------------------------------------------------------------

So 1 mod 10 is the remainder of 1 divided by 10.

1 divided by 10 is .1 with no remainder.

---------------------------------------------------------------------------------------------------------------

But just now I actually did the long division:

1 divided by 10 is .1 with .9 as a remainder. So Java rounds to 1.

Similarly, 2 divided by 10 is .2 with 1.8 as a remainder. So Java rounds to 2.

Is the above correct?

Thanks.

<red-face>OMG, I can't believe I had forgotten some basic math.</red-face>

My understanding of the % operator is that it returns the remainder after division.

Here was my reasoning that led to my post:

---------------------------------------------------------------------------------------------------------------

So 1 mod 10 is the remainder of 1 divided by 10.

1 divided by 10 is .1 with no remainder.

---------------------------------------------------------------------------------------------------------------

But just now I actually did the long division:

1 divided by 10 is .1 with .9 as a remainder. So Java rounds to 1.

Similarly, 2 divided by 10 is .2 with 1.8 as a remainder. So Java rounds to 2.

Is the above correct?

Thanks.

Matthew Brown

Bartender

Posts: 4568

9

posted 4 years ago

I think you may still be getting mixed up between integer division and floating-point division.

With real numbers (represented by floating point) there is no such thing as a remainder. We simply have exact division. So 1.0/10.0 = 0.1, and 2.0/10.0 = 0.2.

Remainders only come into play when you're dealing with integers only. In integer arithmetic 1 divided by 10 is zero, with a remainder of 1. There's no rounding going on.

- 1

Stephen-Austin Murphy wrote:

But just now I actually did the long division:

1 divided by 10 is .1 with .9 as a remainder. So Java rounds to 1.

Similarly, 2 divided by 10 is .2 with 1.8 as a remainder. So Java rounds to 2.

Is the above correct?

I think you may still be getting mixed up between integer division and floating-point division.

With real numbers (represented by floating point) there is no such thing as a remainder. We simply have exact division. So 1.0/10.0 = 0.1, and 2.0/10.0 = 0.2.

Remainders only come into play when you're dealing with integers only. In integer arithmetic 1 divided by 10 is zero, with a remainder of 1. There's no rounding going on.

posted 4 years ago

- 1

To make the understanding simple, here is how I think of it.

Here 1 is the dividend, 10 is the divisor so in the world of integers 1/10 = 0 and so is 2/10...

Now dividend = divisor x quotient + remainder

So

1 = 10 x 0 + 1

2 = 10 x 0 + 2

and so on.

If you have noticed, the % operator is showing the remainder here, which is the number after the + sign.

Now if the dividend is fully divisible, we will get zero.

For example

100 = 10 x 10 + 0

6 = 3 x 2 + 0

So 100%10 and 6%3 are both 0.

Here 1 is the dividend, 10 is the divisor so in the world of integers 1/10 = 0 and so is 2/10...

Now dividend = divisor x quotient + remainder

So

1 = 10 x 0 + 1

2 = 10 x 0 + 2

and so on.

If you have noticed, the % operator is showing the remainder here, which is the number after the + sign.

Now if the dividend is fully divisible, we will get zero.

For example

100 = 10 x 10 + 0

6 = 3 x 2 + 0

So 100%10 and 6%3 are both 0.

SCJP, SCWCD.

|Asking Good Questions|

posted 4 years ago

Since everyone is helping the OP get clearer, I get to throw a monkey wrench into the works...

The % operator actually works with floating points... for example ...

4.4 % 1.4 = 0.2

Henry

- 1

Matthew Brown wrote:

I think you may still be getting mixed up between integer division and floating-point division.

With real numbers (represented by floating point) there is no such thing as a remainder. We simply have exact division. So 1.0/10.0 = 0.1, and 2.0/10.0 = 0.2.. In integer arithmetic 1 divided by 10 is zero, with a remainder of 1. There's no rounding going on.

Remainders only come into play when you're dealing with integers only

Since everyone is helping the OP get clearer, I get to throw a monkey wrench into the works...

The % operator actually works with floating points... for example ...

4.4 % 1.4 = 0.2

Henry

posted 4 years ago

- 1

In floating point division, convert both divisor and dividend into integer values. (By multiplying 10^x where x is and integer. ^ means power.)

Then do the long division.

Convert the remainder into a float by dividing it by 10^x..!

Don't do long division too much long. Get only the Integer answer. Don't divide remainder again..!!

Then do the long division.

Convert the remainder into a float by dividing it by 10^x..!

Don't do long division too much long. Get only the Integer answer. Don't divide remainder again..!!

Ramesh-X

Campbell Ritchie

Marshal

Posts: 56576

172

Stephen-Austin Murphy

Greenhorn

Posts: 10

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |