在算术表达式中会将double隐式转换为unsigned int吗?

时间:2019-03-11 18:51:14

标签: c++ type-conversion

在Stanley B. Lippman的 C ++ Primer 中,“隐式转换”部分指出:

  int ival; 
  unsigned int ui;
  float fval;

  fval = ui - ival * 1.0;
     

ival转换为double,然后乘以1.0。结果是    转换为unsigned int,然后减去ui。结果是      转换为float,然后分配给fval

但是我不这样认为:我认为实际上ival转换为两倍然后乘以1.0然后uiunsigned int类型的转换为double并非相反,然后从转换为double ui的值中减去乘法结果。最后将最终的double值转换为float并将其分配给fval

为了确保我说的话:

ival = 5; 
ui  = 10;
fval = 7.22f;
dval = 3.14;

std::cout << typeid(ui - ival * 1.0).name() << std::endl; // double

std::cout << (ui - ival * 1.7) << std::endl; // 1.5 this proves that the unsigned int ui is converted to double not the contrary that is because C++ preserves precision. otherwise the decimal part is truncated.

0 个答案:

没有答案