简体   繁体   中英

how the output of cout<<4%-8; is 4?

int main() {
    cout<<4%-8;
    return 0;
}

Why the compiler is ignoring - or + sign and how the output is 4?? And why cout<<4%*8; and cout<<4-%8; are illegal syntax?

how the output of cout<<4%-8; is 4?

The operation is defined as follows:

[expr.mul]

if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a ;

(4/-8)*(-8) + 4%-8 == 4
              4%-8 == 4 - (4/-8)*(-8) 
              4%-8 == 4 - (0)*(-8)
              4%-8 == 4

Why the compiler is ignoring - or +

If you look at the definition, you may find the only part where the right hand operand is used: (a/b)*b , is either zero, or same sign as left and operand and never greater than left. The result always has the same sign as the left hand operand if non-zero.

And why cout<<4%*8; and cout<<4-%8; are illegal syntax?

Because the right hand operands of the top operator (that operator being % in first, - in second), which are *8 and %8 are not valid expressions. This is because neither * nor % are unary operators for integers ( * is a unary operator for pointers and can be overloaded for classes). 8 is an integer.

4%-8 = 4% (-8) - 除法后剩余。

4%-8 means 4 mod -8 . 4%*8 means 4 mod times 8 which is not something you can do so you get an error. With 4-%8 that means 4 minus mod 8 which is also meaningless and is an error.

Compiler is not ignoring anything.

A statement like

  cout<<4%-8;

is the same as

  cout << 4 % (-8);

which has a result 4.

On the other hand, statement like

 cout<<4%*8;

does not make sense, because,

 cout<<4% (*8);
          ^^^^---- invalid syntax (argument).

Same as with cout<<4-%8; , in cout<<(4-) % (8);

The statement cout<<4%-8; is grouped as cout<<(4%(-8)); . Here - is the unary negation operator .

4 % (-8) is 4 . (Think of % as being a remainder operator rather than the mathematical modulus .)

4%*8 is grouped as 4%(*8) which is meaningless since 8 is not a pointer type, but an int .

4-%8 is meaningless since % is never a unary operator.

Reference for the remainder arithmetic operator: https://en.cppreference.com/w/cpp/language/operator_arithmetic

Answer to the first part of your question.

Why the compiler is ignoring - or + sign ?

First of all the compiler is not at all ignoring - or + sign.
Its just simple Maths that the compiler do. Whats the answer of 4 X (-8) its -8 right.
Similary the compiler is including that - or + sign with 8 taking it as a positive or negative Integer and considering that it further solves your mathematical expression.

Answer to the second part of your Question

Why the Output is 4 ?

You are using % (modlus or remainder) which gives the remainder of the two operands.
In your case you are calculating the remainder when 4 is divided by 8 which is 4 obviously.
I think instead you wanted to find out the opposite ie remainder when 8 is divided by 4 which will be 0 .
The syntax for the same will be.

cout<<(8%4);  

When you divide 4 by -8 the result is -1/2. If you round that toward 0, the result of the division is 0 and the remainder is 4. If you round down (toward negative infinity), the result of the division is -1, and the remainder is -4.

When you write 4%-8 in your code you're asking for the remainder (not modulus, despite the common misuse of that name) when those numbers are divided. It used to be that either of those approaches to the calculation was allowed by the standard, and the remainder could be 4 or -4, typically depending on how the hardware handled division by negative numbers. Lately the requirement has been changed, and the required answer is that the quotient is 0 and the remainder is 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM