Does one double promote every int in the equation to double?

后端 未结 4 1944
粉色の甜心
粉色の甜心 2020-12-06 09:13

Does the presence of one floating-point data type (e.g. double) ensure that all +, -, *, /, %, etc math operations assume double operands?

If the story

4条回答
  •  离开以前
    2020-12-06 09:46

    Does one double promote every int in the equation to double?

    No. Only the result of a single operation (with respect to precedence).

    double result1 = a + b/d + c; // equal to 4 or to 4.5?
    

    4.5.

    double result2 = (a + b)/d + c; // equal to 3 or to 3.75?
    

    3.75.

    double result3 = a/b + d; // equal to 4 or to 4.5?
    

    4.

提交回复
热议问题