Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2006-09-08 12:03:29

mikau
Member
Registered: 2005-08-22
Posts: 1,504

negative integer division and the modulus operator

I just started college this week. FINALLYYYYY!!! Majoring in computer science as some of you might recall. Turns out I'll be learning java as my first language.

Anyways, I'm reading my java text and everythings going fine, except they briefely mentioned a few things, namely integer division with negative numbers, the modulus operator with negative and decimal numbers, all of which I am not familiar with.

I know how to perform integer division with positive numbers and how to divide when one or more negative numbers appear, but not how divide negative numbers (positive by negative or vice versa) to get an integer quotient and a remainder.

Lastly, I thought modulus  was used only for integer division, but it turns out 6.5 % 3.2 has an answer. And I'll bet  -6.5 % 3.2 has an answer as well.

Some explanations for the algorithms used in integer division involving negatives and modulus involving decimals   would be very much appreciated.


A logarithm is just a misspelled algorithm.

Offline

#2 2006-09-08 16:08:22

pi man
Member
Registered: 2006-07-06
Posts: 251

Re: negative integer division and the modulus operator

dividend / divisor = quotient with a remainder

Just do your division as if both numbers (dividend and divisor) where positive.   When you're done, if both numbers were positive, the quotient is positive.   If both numbers were negative, the quotient is positive.   If one (and only one) of the two is negative, then the quotient  is negative.   

So -45 / 7 would be -6 with a remainder of 3.   Actually, now that you brought it up, it probably should be a remainder of -3 since [quotient * divisor + remainder = dividend].   

-6 * 7 + (-3) = 45.   

Or if the remainder is always supposed to be positive, the answer could be -7 with a remainder of 4:   -7 * 7 + 4 = 45.    Bottom line is I thought I knew the answer but I guess I don't.   It's been too long since I studied that I guess.

Offline

#3 2006-09-08 16:22:24

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: negative integer division and the modulus operator

yeah me too. Well actually I never studied this particular thing.


A logarithm is just a misspelled algorithm.

Offline

#4 2006-09-08 17:18:36

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: negative integer division and the modulus operator

When programming in Java, or really any high level language, it's not so much important to understand how things work.  That only starts to make sense when you get down to the assembly level.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#5 2006-09-09 07:25:04

krassi_holmz
Real Member
Registered: 2005-12-02
Posts: 1,905

Re: negative integer division and the modulus operator

Ricky wrote:

When programming in Java, or really any high level language, it's not so much important to understand how things work.  That only starts to make sense when you get down to the assembly level.

I don't agree. If you have some very big messy program, for example, and if it uses many modulus in it, and if it doesn't do what it's supposed to do, then you should know what's happening around the %, because the mistake may be there. If you don't know, you might pass the error, without noticyng it and this can cost you a big headache. dizzy


IPBLE:  Increasing Performance By Lowering Expectations.

Offline

#6 2006-09-09 08:07:10

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: negative integer division and the modulus operator

so is anyone going to tell me how? Lol.

I'm not interested in how the modulus operation works physically, but how I figure out the answer to the problems mathematically so I know where I can use them in my program or what they are doing when they appear.


A logarithm is just a misspelled algorithm.

Offline

#7 2006-09-09 08:13:41

krassi_holmz
Real Member
Registered: 2005-12-02
Posts: 1,905

Re: negative integer division and the modulus operator

For this you will need a definition of module over the reals.
For example:


But this depends. You should see some java help for the java %.

Last edited by krassi_holmz (2006-09-09 08:16:14)


IPBLE:  Increasing Performance By Lowering Expectations.

Offline

#8 2006-09-09 08:19:44

krassi_holmz
Real Member
Registered: 2005-12-02
Posts: 1,905

Re: negative integer division and the modulus operator


IPBLE:  Increasing Performance By Lowering Expectations.

Offline

#9 2006-09-09 08:22:21

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: negative integer division and the modulus operator

thanks, krassi!


A logarithm is just a misspelled algorithm.

Offline

#10 2006-09-09 08:24:40

krassi_holmz
Real Member
Registered: 2005-12-02
Posts: 1,905

Re: negative integer division and the modulus operator

So I've helped!
VVV
o_O
LLLJ

Last edited by krassi_holmz (2006-09-09 08:25:24)


IPBLE:  Increasing Performance By Lowering Expectations.

Offline

#11 2006-09-09 12:57:39

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: negative integer division and the modulus operator

Ah, maybe I misunderstood.  Mikau, are you looking to find out how the computer implements modulus with negatives or what modulus does with negatives?


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#12 2006-09-10 03:10:01

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: negative integer division and the modulus operator

I think what modulus does with negatives. I mean, with positves you use integer division.   5%2. How many times does 2 fit into five? Twice. So 2 * 2 = 4, subtract 4 from 5 and you get the remainder 1, which is the returned value from this expression. So I can figure out 5%2 without using the computer. How do I figure out 5%-2 or -5%2? That was my question.


A logarithm is just a misspelled algorithm.

Offline

#13 2006-09-10 05:14:48

krassi_holmz
Real Member
Registered: 2005-12-02
Posts: 1,905

Re: negative integer division and the modulus operator

I gave you a formula!


Last edited by krassi_holmz (2006-09-10 05:20:07)


IPBLE:  Increasing Performance By Lowering Expectations.

Offline

#14 2006-09-10 05:58:58

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: negative integer division and the modulus operator

Yes I know, Krassi, I was just trying to explain to Ricky what my question WAS.


A logarithm is just a misspelled algorithm.

Offline

#15 2006-09-10 07:07:08

krassi_holmz
Real Member
Registered: 2005-12-02
Posts: 1,905

Re: negative integer division and the modulus operator

Oh sorry.
Now I misunderstood.
smile wink smile


IPBLE:  Increasing Performance By Lowering Expectations.

Offline

#16 2013-11-22 02:31:39

Atiq
Guest

Re: negative integer division and the modulus operator

pi man wrote:

dividend / divisor = quotient with a remainder

Just do your division as if both numbers (dividend and divisor) where positive.   When you're done, if both numbers were positive, the quotient is positive.   If both numbers were negative, the quotient is positive.   If one (and only one) of the two is negative, then the quotient  is negative.   

So -45 / 7 would be -6 with a remainder of 3.   Actually, now that you brought it up, it probably should be a remainder of -3 since [quotient * divisor + remainder = dividend].   

-6 * 7 + (-3) = 45.   

Or if the remainder is always supposed to be positive, the answer could be -7 with a remainder of 4:   -7 * 7 + 4 = 45.    Bottom line is I thought I knew the answer but I guess I don't.   It's been too long since I studied that I guess.

dizzyrofloldizzy
SINCE:
           dividend = quotient (multiplier * divisor) + remainder

such that
              dividend > quotient

so remainder = 4

Board footer

Powered by FluxBB