分配给浮点数的积分文字除法 - 为什么结果是错误的?

Integral literal division assigned to float - why is the result wrong?

本文关键字:为什么 结果是 除法 错误 文字 浮点数 分配      更新时间:2023-10-16
#include <iostream>
using namespace std;
int main (void) {
float a=5/9;
cout << a;
}

此代码打印 0 而不是 0.555 - 有什么问题?任何帮助表示赞赏。

5/9

是两个整数的除法,其结果为0。如果您希望在float秒内完成操作,那么至少一个操作数必须是float

5/9.f