在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
然后ui
是unsigned 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.