What is numeric promotion?

前端 未结 4 406
抹茶落季
抹茶落季 2020-12-11 15:48

Can any one tell what is numeric promotion?

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 16:10

    Numeric promotion is a conversion of an operand (at least one of the numbers involved) to a common type.

    For example:

    int i = 10;
    double d1 = 2.5;
    double d2 = d1 * i;
    

    In this case, i is promoted to double so the calculation can be performed. In some ways, you can think of this is analogous to boxing, but boxing involves moving from a struct to an object (from the stack to the heap). But, using the analogy does give an idea of the fact the integral value is being made into a floating point to perform the calculation.

提交回复
热议问题