Division and Modulus operators in Python

Page 1 of 1

6 Replies - 4796 Views - Last Post: 01 April 2010 - 08:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=30282&amp;s=a43cfb39456d4199ec8882db959948b9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mocqueanh

Reputation: 0
• Posts: 1
• Joined: 25-June 07

Division and Modulus operators in Python

Posted 11 July 2007 - 12:58 AM

>>> 10/4
2
>>> -10/4
-3

Why is it ? I think -10/4 = -2

And now, the Modulus in Python:

>>> 10%4
2
>>> -10%4
2

The operator -10/4 has remainder is -2, not 2

Next:
>>> 10%-4
-2

The operator 10/-4 has remainer is 2, not 2

Next:
>>> 11%4
3
>>> -11%4
1
>>> 11%-4
-1

I cant understand this case.

Is This A Good Question/Topic? 0

Replies To: Division and Modulus operators in Python

#2 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

Re: Division and Modulus operators in Python

Posted 16 July 2007 - 10:47 PM

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

Re: Division and Modulus operators in Python

Posted 17 July 2007 - 12:53 PM

hahaha...same question on two forums

#4 k.sangeeth

Reputation: 0
• Posts: 62
• Joined: 27-July 07

Re: Division and Modulus operators in Python

Posted 31 July 2007 - 03:17 AM

Although I have been using python for last four months .. I never noticed this ..
very interesting though.

#5 kuratkull

Reputation: 0
• Posts: 12
• Joined: 01-May 07

Re: Division and Modulus operators in Python

Posted 27 August 2007 - 05:25 AM

use:
10.0/4

Reputation:

Re: Division and Modulus operators in Python

Posted 29 March 2010 - 11:43 PM

>>> 10%4
2
>>> -10%4
2

Yes, The operator -10/4 has remainder is -2
In this case you have to type in this way
>>> -(10%4)

Next:
>>> 11%4
3
>>> -11%4
1
>>> 11%-4
-1

In this, The remainder is
4 )11(3 , then you get remainder is -1
12

4 )-11 (3 , then you get remainder is 1
12

#7 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

Re: Division and Modulus operators in Python

Posted 01 April 2010 - 08:00 PM

Thanks for your answer, but this topic is nearly 3 years old!

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }