Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What determines the sign of m % n for integers?

The modulo in Python is confusing.

In Python, % operator is calculating the remainder:

>>> 9 % 5
4

However:

>>> -9 % 5
1

Why is the result 1? and not -4?

like image 534
user1864828 Avatar asked Jan 24 '13 04:01

user1864828


People also ask

Where does the symbol for integers come from?

The notation Z for the set of integers comes from the German word Zahlen, which means "numbers". Integers strictly larger than zero are positive integers and integers strictly less than zero are negative integers.

What sign is used for integers?

Integer Symbol The letter (Z) is the symbol used to represent integers. An integer can be 0, a positive number to infinity, or a negative number to negative infinity. One of the numbers …, -2, -1, 0, 1, 2, …. The set of integers forms a ring that is denoted Z.

Is m and n an integer?

The numbers m and n are integers.

How many pairs of positive integers m and n?

How many pairs of positive integers m,n satisfy 1/m + 4/n = 1/12, where n is an odd integer less than 60? Thus n take only three values– 49, 51 and 57. Hence there are three pairs of (m,n) which satisfy the given conditions.


1 Answers

Because in python, the sign matches the denominator.

>>> 9 % -5
-1
>>> -9 % 5
1

For an explanation of why it was implemented this way, read the blog post by Guido.

like image 62
wim Avatar answered Oct 12 '22 13:10

wim