P
Peng Yu
Hi,
Please see the following code and the output. It seems that if one of
%'s oprand is unsigned, the other will also be converted to unsigned.
There is a operator precedence table in wikipedia. I'm wondering what
is the general rule in type conversions.
Thanks,
Peng
#include <iostream>
int main() {
std::cout << -10 % 3 << std::endl;
std::cout << -10 % static_cast<unsigned>(3) << std::endl;
std::cout << static_cast<unsigned>(-10) % static_cast<unsigned>(3)
<< std::endl;
std::cout << 10 % 3 << std::endl;
}
$./main-g.exe
-1
0
0
1
Please see the following code and the output. It seems that if one of
%'s oprand is unsigned, the other will also be converted to unsigned.
There is a operator precedence table in wikipedia. I'm wondering what
is the general rule in type conversions.
Thanks,
Peng
#include <iostream>
int main() {
std::cout << -10 % 3 << std::endl;
std::cout << -10 % static_cast<unsigned>(3) << std::endl;
std::cout << static_cast<unsigned>(-10) % static_cast<unsigned>(3)
<< std::endl;
std::cout << 10 % 3 << std::endl;
}
$./main-g.exe
-1
0
0
1