About comparing an integer and a float/double in C/C++

前端 未结 2 1899
孤街浪徒
孤街浪徒 2021-01-14 01:50

I have the following piece of code (feel free to change the float with double):

class A
{
    public:
        void setValueFloat(float v) {
              m_f         


        
2条回答
  •  抹茶落季
    2021-01-14 02:19

    The usual rules on type promotion in binary operations apply. To quote the Standard (chapter 5. expressions)

    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 scoped enumeration type (7.2), no conversions are performed;

    if the other operand does not have the same type, the expression is ill-formed.

    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

提交回复
热议问题