C++
我是C ++的新手,我尝试了此简单的代码:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
但是结果是0。正如我所知,双重应与实数一起使用,因此结果不应该是1/6或0.1666666吗?谢谢你!
I am new to C++ and I tried this simple code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
But the result is 0. As I understood, double should work with real numbers, so shouldn't the result be 1/6 or 0.1666666? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在表达式
1/6 < / code>中,两个数字都是整数。这意味着该部门将执行整数部门,从而导致
0
。要进行double
除法,例如,一个数字必须是双重:1.0/6
。In the expression
1 / 6
, both numbers are integers. This means that this division will perform integer division, which results in0
. To do adouble
division, one number has to be a double:1.0 / 6
for example.整数文字
1
和6
具有类型int
。因此,在表达式中使用了整数算术,结果等于0。
将至少一个操作数用作浮动文字。例如
或
或
Integer literals
1
and6
have typeint
. Thus in the expressionthere is used the integer arithmetic and the result is equal to 0.
Use at least one of the operands as a floating literal. For example
or
or