据我所知,例如,当 float 和整数相乘时,整数会提升为 float ,因此乘积可以有小数部分。另一方面,当将 float 除以 int 时,不会进行提升,并且商的任何小数部分都会被丢弃。造成这种不对称的原因是什么?
float f1 = 13.3;
int i1 = 2;
float product = f1 * i1; // Product will be 26.6
float quotient = f1 / i1; // Quotient will be 6.0
编辑:好吧,我想我错了。 int 在除法中也得到提升。正确的?截断仅适用于两个整数的除法。正确的?这里的网站最佳实践是什么,我应该删除这个问题吗,因为它太不靠谱了?
最佳答案
On the other hand, when dividing a float by an int there's no promotion and any fractional part of the quotient is discarded.
这根本不正确。小数部分被保留。从这个意义上说,乘法和除法运算的工作原理是相同的。
关于c - 为什么 int 在乘法中会被提升而不是在除法中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966759/