Is it guaranteed that in C++11, (-x) % m is negative and equal to (-x % m), where x and m are positive?
I know it’s right on all machines I know.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In addition to Luchian‘s answer, this is the corresponding part from the C++11 standard:
Which misses the last sentence. So the part
Is the only reference to rely on, and that implies that
a % bwill always have the sign ofa, given the truncating behaviour of/. So if your implementation adheres to the C++11 standard in this regard, the sign and value of a modulo operation is indeed perfectly defined for negative operands.