c++ - 为什么cout的输出<< 7/9*9;是零吗?

标签 c++ integer-division

为什么下面代码的输出等于0或者serven?

cout << 7/9*9;   //output 0 (zero)   why?

float nine = 9;
float seven = 7;
float i = seven/nine*nine;
cout << i     //output 7   Why?

感谢您的帮助。

最佳答案

7/9*9 将这些数字计算为整数,因此 7/9 计算为 0,并且 0*9 = 0。

当你让它们漂浮时,你正在执行预期的计算。

尝试 7.0/9*9 得到 7,然后你将进行浮点运算。

关于c++ - 为什么cout的输出<< 7/9*9;是零吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1540515/

相关文章:

c++ - 如何在继承类成员函数中访问基类成员的地址?

c++ - 双摆单PID

python - 整数除以小数的结果是什么?

assembly - 纳斯姆错误: invalid combination of opcode and operands

c++ - 整数除法算法

c++ - C++中 `int* const& x`和 `int* const x`之间的区别

c++ - 在 C++ 控制台应用程序中使用 Unicode 字体

c++ - 基于另一个数据成员的类数据成员类型?

python - 负数楼层划分

algorithm - 这是执行整数除法函数的聪明方法还是愚蠢方法?