1

If I have 2 floats and add them together, is the result a float? Or is a double? Or is it compiler defined behavior?

9.0f + 8.0f <--- is the result float, double or compiler defined behavior?

A citation of the correct place in the C specifications would be helpful, if possible.

phuclv
  • 37,963
  • 15
  • 156
  • 475
B. Nadolson
  • 2,988
  • 2
  • 20
  • 27
  • 1
    The linked duplicate question is C++, not C, and it therefore references the C++ standard, not the C standard. The corresponding place in the C11 standard is [§6.5.6 ¶4](http://port70.net/~nsz/c/c11/n1570.html#6.5.6p4), which refers to the "usual arithmetic conversions" defined in [§6.3.1.8 ¶1](http://port70.net/~nsz/c/c11/n1570.html#6.3.1.8). This is explained a bit better [here](https://en.cppreference.com/w/c/language/conversion#Usual_arithmetic_conversions). – Andreas Wenzel Sep 30 '21 at 05:05
  • B. Nadolson, for further insight, for you, _why_ is the type important? – chux - Reinstate Monica Sep 30 '21 at 11:08

1 Answers1

3
9.0f + 8.0f 

is the result a float?

Yes, the type is float.

First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.

C23x dr § 6.3.1.8 1


Although the type is float, depending on FLT_EVAL_METHOD, the sum may have been calculated using float, double or long double math. This potentially has arithmetic impact on a larger/different valued expression. This part is "compiler implementation defined behavior".

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256