Why Does C++ Output Negative Numbers When Using Modulo

How I can solve C++ output negative numbers when using modulo?

From cppreference.com:

double fmod (double numer, double denom);

The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.

The returned value has the same sign as x and is less than y in magnitude.

In your case it is -10 - (-10)/11 * 11 = -10 - 0 * 11 = -10, which is correct for that implementation of fmod. If you need another answer, you should implement your own version, as modulo is defined in different ways for negative numbers.

Why does C++ output negative numbers when using modulo?

On x86 (and other processor architectures), integer division and modulo are carried out by a single operation, idiv (div for unsigned values), which produces both quotient and remainder (for word-sized arguments, in AX and DX respectively). This is used in the C library function divmod, which can be optimised by the compiler to a single instruction!

Integer division respects two rules:

  • Non-integer quotients are rounded towards zero; and
  • the equation dividend = quotient*divisor + remainder is satisfied by the results.

Accordingly, when dividing a negative number by a positive number, the quotient will be negative (or zero).

So this behaviour can be seen as the result of a chain of local decisions:

  • Processor instruction set design optimises for the common case (division) over the less common case (modulo);
  • Consistency (rounding towards zero, and respecting the division equation) is preferred over mathematical correctness;
  • C prefers efficiency and simplicitly (especially given the tendency to view C as a "high level assembler"); and
  • C++ prefers compatibility with C.

Modulo with a negative integer in C++

One needs to do

(-26 % 10 + 10) % 10

to get 4.

For future reference :

int mod(int a, int b) { return (a % b + b) % b; }

modulo operation on negative numbers

As a general rule, the modulo and division should satisfy the equation

b * (a/b) + a%b == a

For positive numbers, it is obvious that this means that a%b must be a positive number. But if a/b is negative, then the result is rounded towards zero.

So take for instance a = -4, b = 3. We know that a/b = -1.3333, which rounded towards zero becomes a/b == -1. From the equation above, we have that b * (-1) + a%b == a. If we insert a and b, we get -3 + a%b == -4, and we see that a%b must be -1.

Mod with negative numbers gives a negative result in Java and C

The % operator is treated as a remainder operator, so the sign of the result is the same as that of the dividend.

If you want a modulo function, you can do something like this:

int mod(int a, int b)
{
int ret = a % b;
if (ret < 0)
ret += b;
return ret;
}

I am using modulo operator, but it still giving me a negative number

The code makes two implicit assumptions:

  • int is at least 32 bit (otherwise the 1,000,000,007 for mod will not fit)
  • long is bigger than int (to avoid overflows in the multiplication)

Neither of these assumptions are guarantee by the standard https://en.cppreference.com/w/cpp/language/types

I don't have access to the same platform in the question, but I can reproduce the output exactly if I remove the cast to long in the assignment of temp1 and temp2 (effectively simulating a platform were sizeof int and long is both 4).

You can verify if the second assumptions hold in your platform checking the sizeof(int) and sizeof(long).

The mod of negative numbers in C

The C11 standard says:

6.5.5:6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.(105) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a; otherwise, the behavior of both a/b and a%b is undefined.

with footnote 105:

105) This is often called ‘‘truncation toward zero’’.

Another way to define division is to round towards -oo. This is called Euclidean division. Python appears to use yet another definition, according to user3307862's link.

The % operator, properly called “remainder”, is defined with respect to the corresponding division, so it either is always in [0..b) or in (-b..b) depending on the definition of /.



Related Topics



Leave a reply



Submit