modulo

What are the practical uses of modulus (%) in programming?

Possible Duplicate: Recognizing when to use the mod operator I wonder, what are the practical uses of modulus? I know what modulo division is. The first scenario which comes to my mind is to using it to find odd and even numbers, and clock arithmetic. But where esle you could use it? ...

Does either ANSI C or ISO C specify what -5 % 10 should be?

I seem to remember that ANSI C didn't specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly? ...

Is the behavior of % with negative operands defined in Perl 5?

Until recently (i.e. C99), the behavior of the modulo operator was implementation defined in C. Since Perl 5 is written in C, is it reliant on the behavior of the C compiler used to build it? ...

What am I missing about using the modulo operator?

This is the second time I have got confusing results from the modulo operator, so I am sure I must be missing something about how it works, what am I doing wrong here that is getting me the wrong answers? This function is supposed to take a number and return its digits as a pointer to an array (where the first element says how many digit...

get output as a vector in R during a loop

How can I get the output as a vector in R? For example, if I want to have for (i in 1:1000) {if i mod 123345 = 0, a = list(i)} a but I would want to find all i that divide evenly into 123345 (i.e., factors), and not just the largest one. ...

How can I modulo when my numbers start from 1, not zero?

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,10), which is circular, meaning the number after the last one is again the first one (next(10)=1). For a given number i>0 in the range, I wo...

Using hardware timer in C

Hi guys, Okay, so I've got some C code to perform a mathematical operation which could, pretty much, take any length of time (depending on the operands supplied to it, of course). I was wondering if there is a way to register some kind of method which will be called every n seconds which can analyse the state of the operation, i.e. what...

negative numbers in python

Hi, everyone I've found some strange behaviour in python regarding negative numbers: >>> a = -5 >>> a % 4 3 Could anyone explain what's going on? ...

Divide and Get Remainder at the same time?

Apparently, x86 (and probably a lot of other instruction sets) put both the quotient and the remainder of a divide operation in separate registers. Now, we can probably trust compilers to optimize a code such as this to use only one call to divide: ( X / 6 ) ( x % 6 ) And they probably do. Still, do any languages (or libraries, but m...

Java Modulo Help

public void turnRight() { int direction=getDirection(); if (direction==3) direction=0; else direction++; this.setDirection(direction); So I have this method that, when called, increments direction by 1. However, the max value should be 3, so if direction is equal to 3 and the meth...

How can I know whether one number is a multiple of other number?

I tried using 6%2, but its always giving the value as 2 and not 0. Why and how can I get a solution to this? ...

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

One of my pet hates of C-derived languages (as a mathematician) is that (-1) % 8 // comes out as -1, and not 7 fmodf(-1,8) // fails similarly What's the best solution? C++ allows the possibility of templates and operator overloading, but both of these are murky waters for me. examples gratefully received. ...

calculating modulo for large number

All, How can I calculate 2^301 mod 77? I did check out the link StackOverflow. But did not understand the step wherein 625 mod 221 = 183 mod 221. How did the conversion take place? ...