modulo Questions

6

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?
Avaunt asked 24/1, 2013 at 4:43

2

Solved

I just read this topic (especially the last comments). Then I was wondering, why we actually need this was of giving the remainder. But it seems, that not many people "on google" were interested ...
Mayday asked 31/10, 2014 at 10:4

9

What would be an elegant, efficient and Pythonic way to perform a h/m/s rounding operation on time related types in Python with control over the rounding resolution? My guess is that it would requ...
Copula asked 24/7, 2011 at 11:21

2

Solved

Can somebody please explain this code to me? I understand what it does but I don't understand how it works. # >n 0 d [->+>-[>+>>]>[+[-<+>]>+>>]<<<<...
Sullins asked 12/1, 2015 at 15:51

4

Solved

There are well-known algorithms for cryptography to compute modular exponentiation (a^b)%c (like Right-to-left binary method here : http://en.wikipedia.org/wiki/Modular_exponentiation). But do al...
Ledoux asked 22/3, 2012 at 7:35

5

Solved

I require a function dist( a, b ) // 0 ≤ a,b < 12 which returns the shortest (absolute ie +ve) distance ala clock arithmetic, using modulo 12. So for example, dist( 1, 2 ) = dist( 2, 1 ) = d...
Erb asked 31/5, 2011 at 19:57

1

I'm reading a C++ function that handles timestamps, and there's an expression like this: t % (60 * 60) % 60 I thought this expression was strictly equivalent to: t % 60 However, when I entered th...

21

Solved

I'm embarrassed to ask such a simple question. My term does not start for two more weeks so I can't ask a professor, and the suspense would kill me. Why does 2 mod 4 = 2?
Hols asked 29/8, 2009 at 17:15

7

Solved

In Python and Ruby, signed integer division truncates towards negative infinity, and signed integer modulus has the same sign the second operand: >>> (-41) / 3 -14 >>> (-41) % 3 ...
Purkey asked 6/5, 2009 at 5:2

9

Solved

Is there a library function in c# for the mathematical modulus of a number - by this I specifically mean that a negative integer modulo a positive integer should yield a positive result. edited to...
Pompous asked 22/4, 2010 at 13:10

5

Solved

Is CMD unable to evaluate the modulus of negative numbers using set /a? 90 % 7 correctly equates to 6 in batch, however -90 % 7 gives -6 instead of 1. I thought that it might have been evaluating...
Romp asked 12/1, 2015 at 1:35

14

In a C program I was trying the below operations (Just to check the behavior) x = 5 % (-3); y = (-5) % (3); z = (-5) % (-3); printf("%d ,%d ,%d", x, y, z); It gave me output as (2,...
Strontia asked 30/7, 2012 at 11:31

21

Solved

In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder?
Predicate asked 19/11, 2010 at 18:53

9

I assume that calculating the modulus of a number is a somewhat expensive operation, at least compared to simple arithmetic tests (such as seeing if a number exceeds the length of an array). If thi...
Horizontal asked 24/3, 2013 at 7:54

19

I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is. But what if the first number is smaller than the second? for instance 2 % 5 the ans...
Staphylorrhaphy asked 8/10, 2009 at 4:39

5

Solved

I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error: error: invalid operands of types 'double' and 'd...
Inion asked 4/2, 2012 at 6:2

6

Solved

How can I calculate division and modulo for integer numbers in C#?
Adabelle asked 21/3, 2011 at 20:10

7

In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix this? Only way I can think is a < 0 ?...
Segalman asked 10/12, 2010 at 18:43

12

Solved

Exactly how does the % operator work in Python, particularly when negative numbers are involved? For example, why does -5 % 4 evaluate to 3, rather than, say, -1?
Viral asked 7/10, 2010 at 15:0

1

I have billions of unsigned 64-bit numbers (x) for which I need to find x % m. m is the same for all, but is not a compile-time constant. m is greater than one. Both x and m are equally likely to h...
Sweeten asked 16/12, 2022 at 12:10

4

Solved

I am trying to calculate the cube root of a many-hundred digit number modulo P in Python, and failing miserably. I found code for the Tonelli-Shanks algorithm which supposedly is simple to modify ...
Precedence asked 19/7, 2011 at 18:42

8

Solved

I guess the solution for this is quite simple, but I've been thinking about it for a while and couldn't come up with an elegant solution. I have a range of numbers, e.g. 1..10 = (1,2,3,4,5,6,7,8,9...
Hanford asked 27/9, 2010 at 11:25

4

Solved

Math: If you have an equation like this: x = 3 mod 7 x could be ... -4, 3, 10, 17, ..., or more generally: x = 3 + k * 7 where k can be any integer. I don't know of a modulo operation is def...
Mcgary asked 24/7, 2012 at 11:54

15

Solved

Most of us are familiar with the maximum sum subarray problem. I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M. T...
Maladminister asked 29/6, 2015 at 11:1

3

Solved

I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and ...
Mirna asked 6/5, 2015 at 14:37

© 2022 - 2024 — McMap. All rights reserved.