Members don't see the ad below. Register now!

So I am new to Python. But I am convinced there is something wierdness about % operator. Becuase 3%5 and -3%5 give two different results. So anyway I used abs function and prefixed the sign. But looks like this si so wierd a programming language would behave like this (3%5 gives 3 and -3%5 gives 2)

OK I agree with explanation of "fnenu" on how python is doing this. For a while I thought I have gone insane so I did the same exact thing in C# and Java. Well the answer for -3%5 in java and c# is -3. So that si good at least i have not gone crazy with this basic stuff. Sounds like some differences in the language. Anyway I am not going to labor over this. Accept how python is doing this and move on

Now wonder if Python is weird or other languages are weird :) I will leave it at that

asked 21 Feb, 09:46

harichavali's gravatar image

harichavali
714

edited 21 Feb, 12:43

Gundega's gravatar image

Gundega ♦♦
6.2k219


The mathematics behind it is this. The % is the modulo operator which means the positive remainder when the number before the % is divided by the number after the % sign. In symbols:

a % b ---> r means that

a= qb + r where 0 <= r < a. We don't care what q is for this. As an example,

17% 5 --> 2 because 12 = 3*5 + 2

Now , if the number to the left is negative,

-17 % 5 then we have 17 = -4 * 5 + 3, since the remainder must be positive, so the output is 3.

For your example,

3%5 --> 3 since 3 = 0*5 + 3 but

-3%5--> 2 since -3 = -1*5 + 2

link

answered 21 Feb, 10:06

fnenu's gravatar image

fnenu
81110

edited 21 Feb, 10:08

1

You can think of it as time. 3 hours before midnight is 21:00 but 3 hours afterwards is 03:00. So -3 % 24 = 21 and 3 % 24 = 3.

(21 Feb, 10:11) fnenu fnenu's gravatar image

For completeness: python will give negative 'r' if 'b' is negative, regardless of the sign of 'a'. Tested on python 2.6.5.

(21 Feb, 23:20) hery hery's gravatar image

Now wonder if Python is weird or other languages are weird :) I will leave it at that

There is a nice table here at Wikipedia showing how it works in a large number of languages.

link

answered 21 Feb, 14:47

tzs's gravatar image

tzs
5535

OK I agree with explanation of "fnenu" on how python is doing this. For a while I thought I have gone insane so I did the same exact thing in C# and Java. Well the answer for -3%5 in java and c# is -3. So that si good at least i have not gone crazy with this basic stuff. Sounds like some differences in the language. Anyway I am not going to labor over this. Accept how python is doing this and move on

So I was trying to do sth like this (p[-U:]+p[:-U]).. Anyway solved it by abs value..But AS I saw the solution looks like there is a better way to do this

link

answered 21 Feb, 11:23

harichavali's gravatar image

harichavali
714

edited 21 Feb, 12:12

2

Well as I understood that, it that if you have modulo 5, you have a world where you go like this

-5 -4 -3 -2 -1 0 1 2 3 4 5

0 1 2 3 4 0 1 2 3 4 0

On the right side of the zero it's quite clear - you have 0/5, 1/5. On the left side it's different. But basically as you go on the axis to the left direction, you see that -3 in the decimal numbers maps to 2 in modulo 5

(21 Feb, 12:18) davidpilny davidpilny's gravatar image
1

Yah..Now I am starting to think after all may be python is doing it the cool way and all these other languages have wired me into wrong way anyway..I will leave it at that

(21 Feb, 12:29) harichavali harichavali's gravatar image

So if what you're saying about C# and Java is true, Wikipedia has it wrong:

The notion of modular arithmetic is related to that of the remainder in division. [] The difference is clearest when dividing a negative number, since in that case remainders are negative. [] In computer science, it is the remainder operator that is usually indicated by either "%" (e.g. in C, Java, Javascript, Perl and Python) or "mod" (e.g. in Pascal, BASIC, SQL, Haskell), with exceptions (e.g. Excel). These operators are commonly pronounced as "mod", but it is specifically a remainder that is computed (since in C++ negative number will be returned if the first argument is negative, and in Python a negative number will be returned if the second argument is negative). The function modulo instead of mod, like 38 ≡ 14 (modulo 12) is sometimes used to indicate the common residue rather than a remainder (e.g. in Ruby).

For everything that implies a wraparound, it's clearly the modulo that we want, not the division remainder. Ruby and Javascript use % like Python does.

(21 Feb, 14:57) lacucharita lacucharita's gravatar image

And the Python Language Reference has it wrong too!

The % (modulo) operator yields the remainder from the division of the first argument by the second.

What's going on here?

(21 Feb, 15:15) lacucharita lacucharita's gravatar image

Since I come mostly from PHP world and this is my first close encounter with python, I was also confused when I saw a one-liner code for looking up the position index in the solution :-) But anyway I find it a rather cool feature of python, since mathematically it gives sense - meaning that when using modulo, the world is consisting only of 0 1 2 3 4 series and -3 is mapped to 2 in this world. Where as far as I know in c-style languages you could use at most something like ternary operator. I am kind of curious what other surprises is python going to bring :-D

link

answered 21 Feb, 11:51

davidpilny's gravatar image

davidpilny
2115

Other surprises for a PHP programmer? Django for one ;-)

(21 Feb, 12:34) BayesianHorse BayesianHorse's gravatar image

Well... :-)

(25 Feb, 13:16) davidpilny davidpilny's gravatar image
Members don't see the ad below. Register now!
Your answer
toggle preview

Follow this Question via Email

Once you sign in you will be able to subscribe for any updates here

Q&A Editor Basics

  • to upload an image into your question or answer hit
  • to create bulleted or numbered lists hit or
  • to add a title or header hit
  • to section your text hit
  • to make a link clickable, surround it with <a> and </a> (for example, <a>www.google.com</a>)
  • basic HTML tags are also supported (for those who know a bit of HTML)
  • To insert an EQUATION you can use LaTeX. (backslash \ has to be escaped, so in your LaTeX code you have to replace \ with \\). You can see more examples and info here

powered by OSQA