The remainder of the quotient of two numbers (usually integers).
Questions tagged [modulus]
847 questions
13
votes
5 answers
Mod with Doubles
Am I doing something wrong or does the VBA Mod operator actually not work with floating point values like Doubles?
So I've always sort of assumed that the VBA Mod operator would work with Doubles based on the VB documentation, but in trying to…

Blackhawk
- 5,984
- 4
- 27
- 56
12
votes
6 answers
Is there a reason some languages allow a negative modulus?
I am curious about these languages (Java, C ...) which ignore mathematical definition of modulus operation.
What is the point of returning negative values in a module operation (that, by definition, should allways return a positive number)?

eversor
- 3,031
- 2
- 26
- 46
11
votes
3 answers
Is there a standard Cyclic Integer Class in C++?
I have a problem that is quite common in the code that I am writing at the moment whereby I want to have an integer that can only exist inside a certain range where the range is [start, end). Basically I want to be able to do something like the…

Robert Massaioli
- 13,379
- 7
- 57
- 73
11
votes
2 answers
Why is anyValue % 1 "silly math" in Sonar when anyValue is a double?
SonarQube raises the major violation Silly math should not be performed in my code. The description says
Certain math operations are just silly and should not be performed
because their results are predictable.
In particular, anyValue % 1 is…

ire_and_curses
- 68,372
- 23
- 116
- 141
11
votes
4 answers
How to select odd or even items from a row in SQL?
I need only even or odd items, so I find modulus operation and this doesn't works
SELECT * FROM table ORDER BY id WHERE MOD (num, 2) = 1 ASC;
Please help me, I'm noob in sql, as I haven't done much in it.

Ronalds Mazītis
- 323
- 2
- 5
- 18
10
votes
5 answers
Determine if $x is divisible evenly by $y in PHP
I simply want to know if $x is evenly divisible by $y. For example's sake assume:
$x = 70;
$y = .1;
First thing I tried is:
$x % $y
This seems to work when both numbers are integers but fails if they are not and if $y is a decimal less than 1…

But those new buttons though..
- 21,377
- 10
- 81
- 108
10
votes
2 answers
How does Python implement the modulo operation?
I'm curious in regards to the time and space complexities of the % operator in Python. Also, does Python use a bitwise operation for % 2?
Edit:
I'm asking about Python 2.7's implementation, just in case it differs slightly from that of Python 3

sgarza62
- 5,998
- 8
- 49
- 69
9
votes
4 answers
Modulus power of big numbers
I am trying to implement the SAFER+ algorithm. The algorithm requires finding the modulus of a power function as follows:
pow(45, x) mod 257
The variable x is a byte, and thus can range from 0 to 255. Accordingly, the result of the power function…

Eng. Aws
- 169
- 1
- 2
- 7
9
votes
1 answer
Why does the modulus operator behave differently in Perl and PHP?
I've this PHP function which does not work for negative numbers:
function isOdd($num)
{
return $num % 2 == 1;
}
but it works for positive number.
I have this Perl routine which does the exact same thing and works for negative number also
sub…

user640527
- 93
- 3
9
votes
3 answers
Is fmod faster than % for integer modulus calculation
Just found the following line in some old src code:
int e = (int)fmod(matrix[i], n);
where matrix is an array of int, and n is a size_t
I'm wondering why the use of fmod rather than % where we have integer arguments, i.e. why not:
int e =…

bph
- 10,728
- 15
- 60
- 135
9
votes
4 answers
Sorting a list alphabetically with a modulus
I don't have any trouble grabbing a list of elements and sorting them alphabetically, but I'm having difficulty understanding how to do it with a modulus.
### UPDATE ###
Here's the code working 'my way', however, I like the re-usability of the…

S16
- 2,963
- 9
- 40
- 64
9
votes
5 answers
Is there any way to write "mod 31" without modulus/division operators?
Getting the modulus of a number can be easily done without the modulus operator or divisions, if your operand is a power of 2. In that case, the following formula holds: x % y = (x & (y − 1)). This is often many performant in many architectures. Can…

MaiaVictor
- 51,090
- 44
- 144
- 286
9
votes
4 answers
Getting place values of a number w/ modulus?
I need to get the place value of a random number submitted by a user.
This number can by anything from 0-1000000000000000 (zero to one
trillion).
I think this can be achieved by using the JavaScript modulus %
operator. The problem, I don't really…

Matthew
- 2,158
- 7
- 30
- 52
9
votes
5 answers
A clever homebrew modulus implementation
I'm programming a PLC with some legacy software (RSLogix 500, don't ask) and it does not natively support a modulus operation, but I need one. I do not have access to: modulus, integer division, local variables, a truncate operation (though I can…

Ben Mordecai
- 685
- 2
- 8
- 19
9
votes
6 answers
`java (0 % 2 != 0) == false`
The part I keep getting stuck on is
boolean(0 % 2 !=0)
== false. I mean if 2 goes into 0, 0 times then the remainder would be 2, and 2 does not equal 0. So it should be true. Yet but when I put the boolean in my java program it is treating it…

Focus Otter
- 359
- 1
- 4
- 10