Questions tagged [modulus]

The remainder of the quotient of two numbers (usually integers).

847 questions
4
votes
3 answers

Why does Java's % operator give different results than my calculator for a negative dividend?

How come on a calculator -1 mod 26 = 25, but in C or Java -1 % 26 == -1. I need a program which solves it like the calculator. Is there a difference between the two?
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
4
votes
3 answers

C++ equivalent of Java's Math.floorMod()?

In Java, we have two different modulo (and remiander) operations, % and Math.floorMod(). The difference is in the area of the mapping destination, which can either depend on the sign of the first or the sign of the second operand. This page explains…
Daniel S.
  • 6,458
  • 4
  • 35
  • 78
4
votes
4 answers

fast increase number to be mod 16 in C

what is the best way to to get the closest, non-smaller number that is divisible by 16? the method I came up with doesn't look very elegant or fast int non_smaller_int_divisible_by_16(int x) { return x + ((16 - (x % 16)) % 16); } the expected…
Aviad Rozenhek
  • 2,259
  • 3
  • 21
  • 42
4
votes
3 answers

Compute C's `%` using Python's `%`?

How do I compute C's % using Python's %? The difference between the two is in the way they handle the case of negative arguments. In both languages, the % is defined in such a way that this relationship (// being integer division) holds: a // b * b…
norok2
  • 25,683
  • 4
  • 73
  • 99
4
votes
1 answer

Alternative to Modulus in Lua

In Lua, as you may know, arrays start with index 1. In other languages I will often use modulus to make a value oscillate through the members of an array, for example: i = (i + 1) % array.length return array[1] How can I do this in Lua, where…
Dillon
  • 151
  • 1
  • 4
4
votes
1 answer

Modulus and unsigned integer

I found the following behavior surprising: int a = -2; int b = 5; uint c = 5; std::cout << a%b << '\n'; std::cout << a%c << '\n'; Output: -2 4 When comparisons are involved, mixing signed and unsigned is problematic - is there a hidden comparison…
stafusa
  • 195
  • 1
  • 15
4
votes
1 answer

Best way to correct the modulus error in R?

The core R engine has a serious flaw with the way it expresses output from the Modulus operation: ceiling((1.99 %% 1) * 100) Returns: 99 (correct) ceiling((2.99 %% 1) * 100) Returns: 100 (incorrect) The behavior will manifest in any integer value N…
4
votes
3 answers

Why is a modulo operator (%) result implicitly cast to the left side rather than the right side in C#?

Take the following code: long longInteger = 42; int normalInteger = 23; object rem = longInteger % normalInteger; If rem is the remainder of longInteger / normalInteger, shouldn't the remainder always be bounded by the smaller sized "int", the…
jasonsirota
  • 4,003
  • 3
  • 17
  • 15
4
votes
2 answers

Modulus operation for array index

I got a tutorial about using a button for switching some images, and here is the code public class MainActivity extends AppCompatActivity { private static ImageView andro; private static Button buttonswitch; int current_image_index = 0; int[]…
Anthony Lauly
  • 319
  • 2
  • 5
  • 17
4
votes
0 answers

How modulus works with negative numbers in R?

How modulus works with negative integers in R: -90 %% 360 90 %% -360 -400 %% 360 400 %% -360 outputs: 270 -270 320 -320 what is the rationale behind this? The answer from @JimSlonder: a <- c(-90,90,-400,400) b <- c(360,-360,360,-360) modulus <-…
Farid Cheraghi
  • 847
  • 2
  • 12
  • 23
4
votes
2 answers

C rand() dice issue

I'm new to C and I'm reading a book about it. I just came across the rand() function. The book states that using rand() returns a random number from 0 to 32767. It also states that you can narrow the random numbers by using % (modulus operator) to…
Piet
  • 71
  • 1
  • 3
4
votes
3 answers

Modulus operator changes

$5.6/4 in C++03 states- "If both operands are nonnegative then the remainder is nonnegative;if not, the sign of the remainder is implementation-defined74). where Note 74 is According to work underway toward the revision of ISO C, the…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
4
votes
4 answers

F#: integer (%) integer - Is Calculated How?

So in my text book there is this example of a recursive function using f# let rec gcd = function | (0,n) -> n | (m,n) -> gcd(n % m,m);; with this function my text book gives the example by executing: gcd(36,116);; and since the m = 36 and not 0…
Nulle
  • 1,301
  • 13
  • 28
4
votes
1 answer

C# equivalent of IEEE 754 remainder()?

In C#, is there an exact equivalent of C99 / IEEE 754's remainder() function? The C# language specification says that operator %(double x, double y) is "analogous to that used for integer operands, but differs from the IEEE 754 definition (in which…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
4
votes
1 answer

Strange bug in this time calculation script?

Basically this script will subtract StartTime from EndTime, using a jQuery plugin the html form is populated with Start and End Time in the format HH:MM, an input field is populated with the result, it works except for one issue: If Start Time is…
Ezugo C
  • 59
  • 1
  • 4