C++ — type of the division?

前端 未结 6 1444
陌清茗
陌清茗 2021-02-20 02:05

I want to make sure that my understanding of the return type of C++ division,

int / int => return is int?

float / float => return is which type? float?

d         


        
6条回答
  •  一生所求
    2021-02-20 02:48

    You are correct in all cases. The rules for operations involving at least one floating point type are that if either type is a long double, the result is long double; otherwise, if either type is double the result is double otherwise the result has type float.

    Arithmetic operations between two ints produce an int result.

    The rules between other types are slightly more complex and can be implementation dependent; for almost all operations integer promotions mean that the operands are promoted to at least an int sized types producing at least an int sized result.

提交回复
热议问题