简体   繁体   English

C ++中的算术运算

[英]Arithmetic Operation in C++

#include"iostream"
using namespace std;

int main(){

    float arithmetic_operation = (4+5)+9*2-4+2/5+1-13;
    cout<< arithmetic_operation << " <--The Result." << endl;

    return 0;
}

I am getting 11 <--The Result. 我得到11 <-结果。 But actually the result is 11.4, Can someone please help me to understand the point please. 但是实际上结果是11.4,请有人可以帮助我理解这一点。

You are doing integer arithmetic . 您正在执行整数运算 All operands are integers, which meand all operations will be done using integer operations. 所有操作数都是整数,这意味着所有操作都将使用整数运算来完成。 And for integer division 2/5 is equal to zero. 对于整数除法2/5等于零。

Use floating point value all over instead: 改用浮点值:

double arithmetic_operation = (4.+5.)+9.*2.-4.+2./5.+1.-13.;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM