How a floating point literal is treated either double or float in Visual C++?

后端 未结 2 606
梦谈多话
梦谈多话 2020-12-21 11:41

Suppose float a = (1.5 * b) where b is float then how is this expression evaluated? Is 1.5 treated as double or float?

相关标签:
2条回答
  • 2020-12-21 12:29

    1.5 is a floating point literal, a double value. C++03 2.13.3 Floating literals has this to say:

    A floating literal consists of an integer part, a decimal point, a fraction part, an e or E, an optionally signed integer exponent, and an optional type suffix. ... The type of a floating literal is double unless explicitly specified by a suffix.

    Section 13.3.3.1 Standard conversion sequences defines the way in which conversions are handled but it's a little dry to repeat here. Suffice to say that floating point promotion is done and section 4.6 Floating point promotion states that:

    An rvalue of type float can be converted to an rvalue of type double. The value is unchanged.

    Hence the float b is promoted to a double to perform the multiplication.

    Then the calculation is performed using (effectively) a temporary double and the result is shoe-horned back into the float a.

    So, effectively:

    float b = something;
    
    double xyzzy0 = 1.5;
    double xyzzy1 = (double)b;
    double xyzzy2 = xyzzy0 * xyzzy1;
    float a = xyzzy2;
    

    That last step may be problematic. Section 4.8 Floating point conversions (which doesn't include the safer promotions like float to double) states:

    An rvalue of floating point type can be converted to an rvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values. Otherwise, the behavior is undefined.

    The conversions allowed as floating point promotions are excluded from the set of floating point conversions.

    In other words, if the multiplication results in a value outside the range of a float, all bets are off. This is likely to happen if b is about at 67% of the maximum absolute value of a float (positive or negative, doesn't matter).

    0 讨论(0)
  • 2020-12-21 12:30

    1.5 is double, use 1.5f for float, what it actually does:

    float a = (float)(1.5 * (double)b)

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