The sign of the modulo operator result? [duplicate] - c

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Problem using modulo with negative numbers in decryption program
(2 answers)
Closed 4 years ago.
I have couple line of C code testing the modulo operator as follows:
// line 1
printf("%d\n", 5 % (-3)); => output: 2
// line 2
printf("%d\n", -5 % 3); => output: -2
I know that the sign of the modulo depends on the sign of the numerator, but I am curious why not otherwise?

5/(-3) = -1;
(-5)/3 = -1;
If that is agreed then let's calculate the remainder
Remainder = Dividend - ( Divisor * Factor)
5 - (-3 * -1) = 2
-5 - (3 * -1) = -2

Related

Why is the result of -8%-5 equal to -3 [duplicate]

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 5 years ago.
I have the following code
#include <stdio.h>
int main(void)
{
printf("%d\n", -8%5);
printf("%d\n", 8%-5);
printf("%d\n", -8%-5);
return 0;
}
The output I get is
-3
3
-3
How is it that -8%5 and 8%-5 have different signs, especially when the results of -8/5 and 8/-5 have the same output ?
Also why does -8%-5 give -3 as the output, when -8/-5 gives it's output as 1 ?
If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.

c programming division not behaving as expected [duplicate]

This question already has answers here:
How to get fractions in an integer division?
(2 answers)
Closed 7 years ago.
I'm not getting the expected result when I do division. Can someone please tell me what I'm doing wrong?
int page_list_size = 20;
int page_fault_counter = 0;
double failure = 0.0;
double success = 0.0;
failure = page_list_size / page_fault_counter;
success = 1 - failure;
printf("failure Is %lf\n",failure);
printf("success Is %lf\n",success);
failure Is 1.000000
success Is 0.000000
Should be some decimal number between 0 and 1, and they should add up to 1.
You are not allowed to divide by 0. If the denominator is not 0, dividing ints results in an int, so you need to use floats or doubles to get a number between 0 and 1.

how - 5%3 is equal to - 2? [duplicate]

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 7 years ago.
I'm learning C basics right now. I have a question which is confusing me little bit.
My question is how the below program's output is - 2 ?
#include<stdio.h>
int main()
{
printf("%d", -5%3);
return 0 ;
}
The % operator is gives you the remainder left after of integer division.
Then -5/3 = -1 with -2 as remainder of division as 3*(-1)=-3 and -5-(-3)=-5+3=-2.

explicit MOD in C? [duplicate]

This question already has answers here:
How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers
(16 answers)
Closed 9 years ago.
Ok So I know and understand the difference between MOD and REM. I also am aware that C's % operation is a REM operation. I wanted to know, and could not find online, if there is some C library or function for an explicit MOD.
Specifically, I'd like (-1)%4 == 3 to be true. In C (-1)%4 = -1 since it is a remainder. And preferably I'd like to avoid using absolute values and even better would be to utilize some built in function that I can't seem to find.
Any advice will be much appreciated!
The best option I can think of is to compute:
((-1 % 4) + 4 ) % 4
Here you may replace -1 with any value and you will get MOD not REM.
The most common way to do what you expect is:
((a % b) + b ) % b
It works because (a % b) is a number in ]-b; b[ so (a % b) + b is positive (in ]0; 2 * b[) and adding b did not changed the mod.
Just do,
int mod(int a, int b)
{
int res = a % b;
return(res < 0 ? (res + b) : res);
}
Every negative res content after MOD operation is added to b to get modulus of a & b.

What is the meaning of the percent character in this code?

In this case, what does the percentage refers to?
int myInt = 27 % 10;
myInt = 7;
What does the % mean in this code?
% means remainder, when 27 is divided by 10 leaves a remainder 7
EDIT:
My 2 cents about all the discussion about difference between modulo & remainder
Take a % b
1. When both +ve, Modulo & Remainder are one and the same
2. When a is -ve, they are not the same
For example;
a = -10, b = 3
Remainder of -10 % 3 = -1
for Modulo, add a greater multiple of 3 to your 'a' and calculate the remainder.
-10 + 12 = 2
2 % 3 = 2 is your answer
the % is modulus operator, not percentage. For percentage, you just do regular math. 50% is to multiply by .5... etc.
For future reference, the objective c mathematical operations are documented many places, including here.
Note the % is called "Modulo" operator.
% is a operator to find the remainder of a division.
The "%" in this code is called the modulus operator. This causes the processor to perform a division operation, and it returns the remainder of the division.
For example:
8 % 10 = 8
5 % 4 = 1

Resources