C++ — type of the division?

前端 未结 6 1479
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2021-02-20 02:56

    Considering only three types (float, double and int):

    • If any of the operand is double, then the result will be double.
    • Else if any of the operand is float, then the result will be float.
    • Else the result will be int .
    0 讨论(0)
  • 2021-02-20 02:58

    All of those are correct. Here's what the C++03 standard says (§5/9):

    Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

    • If either operand is of type long double, the other shall be converted to long double.
    • Otherwise, if either operand is double, the other shall be converted to double.
    • Otherwise, if either operand is float, the other shall be converted to float.
    • Otherwise, the integral promotions (4.5) shall be performed on both operands.
    • Then, if either operand is unsigned long the other shall be converted to unsigned long.
    • Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
    • Otherwise, if either operand is long, the other shall be converted to long.
    • Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

    [Note: otherwise, the only remaining case is that both operands are int]

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-20 03:12

    Roughly speaking, in C++, in any scenario, both operands are converted to the "largest" type of the two operands' types before the operation is executed. See MSDN Standard Arithmetic Conversions.

    0 讨论(0)
  • 2021-02-20 03:13

    The result will be typed (if rule exists) for the assignment. If you have int x = dY + iZ; Then promotion will cause the addition result to be double, but it will be converted to an int when its assigned to x. Google "variable promotion in c++" for more details.

    0 讨论(0)
提交回复
热议问题