Adding 2 floats in C

496 views Asked by At

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.

1

There are 1 answers

0
chux - Reinstate Monica On
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".