以下内容来自VCK周星星-在此感谢,以下情况皆不考虑运算符重载

float / double = NaN的时候,例子暂时还没找到

什么时候 n == –n

1. n = 0自不必说

2. 设n为一个整型变量,那么当n取到该整型的负值最小值时,n == -n便成立了。为了避免类型提升,对负值做了一次类型转换。

char ch = -128 ;
if (ch == (char)-ch)
{
    cout << "equal" << endl ;
}

short s = -32768;
if (s == (short)-s)
{
    cout << "equal" << endl ;
}

int i = -2147483648;
if (i == -i)
{
    cout << "equal" << endl ;
}

结果都输出equal

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2021-06-29
  • 2021-07-22
  • 2022-12-23
  • 2021-04-19
  • 2021-05-24
  • 2022-12-23
猜你喜欢
  • 2021-06-28
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2021-11-02
  • 2022-12-23
相关资源
相似解决方案