Linked Questions
45 questions linked to/from How does the modulo (%) operator work on negative numbers in Python?
3 votes
4 answers
18k views
How modulo and remainder should work in Scheme?
I'm reading R5RS spec and it shows this: (modulo 13 4) ===> 1 (remainder 13 4) ===> 1 (modulo -13 4) ===> 3 (remainder -13 4) ...
5 votes
3 answers
4k views
The mod of negative numbers in C [duplicate]
In C the following code ... #include<stdio.h> #include<string.h> #include<stdbool.h> #include<stdlib.h> int main() { int result = (-12) % (10); printf("-12 mod 10 = %d\...
0 votes
6 answers
2k views
Print all thursdays between date range
I want to print all Thursdays between these date ranges from datetime import date, timedelta sdate = date(2015, 1, 7) # start date edate = date(2015, 12, 31) # end date What is the best pythonic ...
2 votes
2 answers
2k views
Why is modulo of a negative number positive (-7 % 6 == 5)?
Why is the following true? print(-7 % 6 == 5); This means that -7 % 6 yields 5, but I would expect to see -1 (in JavaScript this is the case). Proof in DartPad.
5 votes
1 answer
1k views
Why does Python's modulo operator (%) not match the Euclidean definition?
The definition of Euclidean division says: Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|, where |b| denotes the absolute value of ...
0 votes
2 answers
248 views
Convert a shifted ASCII value to char inside the abc scope
Background This question is inspired by The Caesar Cipher. Given a modified ASCII value i where i > 122 or i < 97, the goal is to find the char represented by its value. For example, since 'z' ...
-3 votes
2 answers
724 views
conways game of life from automate boring stuff book
i need to create a python conways game of life. i have this code given below from book "how to automate boring stuff with python" i had run this code but the output doesn't seem that the ...
0 votes
1 answer
379 views
Weird Outputs of Modulo Operator in Python3
I'm working on a modular sorter in python3 and have noticed weird behavior to do with the signs of input variables when taking the mod. I intend for it to sort a value by the remainder after dividing ...
0 votes
2 answers
361 views
Question about the modulo operator (%) in Python 3
I get that the modulo operator (%) is used to get the remainder of a division, so for example: 7 % 3 = 1 But, why does 1 % 60 = 1 and not 0.084? (1 - 0.016) Why does -1 % 60 = 59? There must be ...
0 votes
1 answer
144 views
How to fix the output of the following C program?
I have a problem with output of the following C program. It does not get me the correct output.I transform this program from the sum of two big numbers. So I get the output with some "errors" like "...
2 votes
1 answer
147 views
Difference in BigInteger calculations in C# and Python
Hi I'm trying to do some math on some BigInteger. I'm converting some code from Python to C# and the results I'm getting in C# compared to Python is different. Here's the code in C# BigInteger two = ...
3 votes
1 answer
136 views
Difference of two imperial lengths
I have a program that takes two imperial lengths (miles, yards, feet and inches) and outputs their sum and difference. My problem is that when the difference is negative, the output is incorrect. def ...
-1 votes
2 answers
89 views
Why does the same logical equation return 2 different results in python and c++?
I made a prototype code on python to see if the logic works and later coded it in c++. But for some reason the python version and c++ version return different results. I am not able to figure out why ...
1 vote
1 answer
123 views
Does numpy exactly reproduce all C behaviors on usual operations?
I'm designing an algorithm in python and know I'll want to translate it to C later. However, mathematical operations in Python might not yield the same result as in C, for instance 4294967295 + 1 = 0 ...
0 votes
1 answer
60 views
VB.net output is different than python output, why is that?
Why the outputs of python and vb.net are different, the python result is the correct and expected output. Vb.net gives a wrong answer. how to rectify this? option strict on. lm = -...