c - 为什么 int 在乘法中会被提升而不是在除法中?

标签 c types casting

据我所知,例如,当 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/

相关文章:

c - 如何获取方向键扫描码?

c - 在shm_open()之后使用fallocate()会导致在shm_unlink()之后内存没有被释放

types - 使用具有高阶函数的 GADT

python - GraphQL - 'either/or' 用于多个必填输入字段

ios - 从返回 CGImageRef 的 C 函数将 UnsafeRawPointer 转换为 CGImage 会使 Swift 4 崩溃

c++ - 如何避免在非构造函数上进行隐式转换?

c - 在 protobuf-c 中,可选的 uint32 变量可以有值 0

c - 我用 pow(10,2) 和 pow(10,j), j=2; 得到了不同的结果;

c# - .net 中的 AutoClass 是什么?

c++ - 从 float 转换为 int