分配给浮点数的积分文字除法 - 为什么结果是错误的?
Integral literal division assigned to float - why is the result wrong?
#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
相关文章:
- 为什么这不是解密
- 为什么这个程序的结果是3 "born"?和 4 死
- OpenGL,为什么这不是一个立方体?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 为什么输出是整数而不是双精度?
- 通过指针调用模板类成员函数 [为什么这是有效的 c++]?
- 分配给浮点数的积分文字除法 - 为什么结果是错误的?
- 为什么双算的结果是错误的?
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 根据 [basic.def.odr]/2,'A::a[0]' 的潜在结果集为空.为什么它是空的?
- 为何分配给基本呼叫的结果,为什么不是编译器错误
- 谁能告诉我为什么结果是 7 7 7 而不是 7 7 10
- 为什么这个结果是62
- 为什么这个函数的结果是-30
- 为什么这个程序的结果是三个十进制数"597, 295, 295,"输入两个十六进制数"255 127<enter>"
- 为什么最后一行的运行结果是"Segmentation fault (core dumped)"
- boost::addable2的使用-为什么从operator+返回的结果是相反的
- 为什么~x在int上的结果是-(x+1) ?
- 为什么unsigned long和负数比较结果是false ?
- 为什么我的执行结果是正确的,但观察列表结果是错误的