正如您提出的那样,这个问题没有意义。
bar是整数类型,所以1000000/bar肯定会小于1000000,可以用double1精确表示,所以用整数算术执行计算不可能提供更好的精度 - 实际上,你会得到整数除法,在这种情况对于 的任何值都不太bar精确,因为它会截断小数部分。在这里的转换中遇到问题的唯一方法是long转换为,但如果它超出除法的最终结果的范围,则为 0,就像在整数算术中一样。doublebardoubledouble
仍然:
1000000/bar
在 s之间进行除法long: 1000000 是 anint或 a long,取决于平台,bar是 a long;如果需要,第一个操作数被提升为 a long,然后执行整数除法。
1000000.0/bar
double在s:之间执行除法1000000.0是double文字,因此在除法之前bar被提升double。
(long)1000000/bar
相当于第一个:强制转换优先于除法,并且强制1000000(是 along或 an int)成为 a long;bar是 a long,执行 s 之间的除法long。
(long)1000000.0/bar
等效于前一个:1000000.0is a double,但您将其转换为 along然后执行整数除法。
- The C standard, to which the C++ standard delegates the matter, asks for a minimum of 10 decimal digits for the mantissa of
doubles (DBL_DIG) and at least 10**37 as representable power of ten before going out of range (DBL_MAX_10_EXP) (C99, annex E, ¶4).