C++ — type of the division?

前端 未结 6 1447
陌清茗
陌清茗 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 03:02

    operator/ for basic data types (just like most, if not all, operators for basic types) returns the strongest type of its two operands.

    The answer to all of your questions is thus yes.


    In general, floating point types are stronger than integer ones and unsigned are stronger than signed...

    Defining > as "stronger than", we can say that:

    long double > double > float > unsigned long > long > unsigned int > int > unsigned short > short > unsigned char > char

提交回复
热议问题